Click here to Skip to main content
15,886,018 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My android side code
Java
String from=et1.getText().toString();
        String to=et2.getText().toString();
        String id=et3.getText().toString();
        String inputxele="ReportData FromDate='"+from+"' ToDate='"+to+"' UserId='"+id+"'";
        String                  url="http://10.0.2.2/JsonWcfService/GetEmployees.svc/?";
        String resp = null;
        InputStream instream =null;
        try
        {
            List<namevaluepair> postParams = new ArrayList<namevaluepair>();
            postParams.add(new BasicNameValuePair("spName", "USP_ListingReport"));
            postParams.add(new BasicNameValuePair("inputxele", inputxele));
            postParams.add(new BasicNameValuePair("transType", "GetMobileData"));
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
            HttpConnectionParams.setSoTimeout(httpParameters, 20000);
            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost(url);
            httppost.setHeader("Content-Type", "application/json");
            httppost.setEntity(new UrlEncodedFormEntity(postParams,HTTP.UTF_8));
            HttpResponse response;
            response = httpclient.execute(httppost);
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK)
            {
                HttpEntity entity = response.getEntity();
                instream = entity.getContent();
                resp = convertStreamToString(instream);
                instream.close();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
            resp = null;
        }
        return resp;
    }

MY WCF SERVICE CODE
C#
[OperationContract]
[WebInvoke(UriTemplate = "?spName={spName}&inputxele={inputxele}&transType={transType}",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    Method = "POST",
    RequestFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
string ValidateCredentials(string spName, string inputxele,string transType);

Using this code i am getting an error "methodnotallowed" exception.

Any help will be appreciated.
Thanks in advance
Posted
Updated 4-Dec-14 2:27am
v3

1 solution

If I understood it correctly then remove that ? from the UriTemplate. Make it this way,
C#
[OperationContract]
[WebInvoke(UriTemplate = "spName={spName}&inputxele={inputxele}&transType={transType}",
    BodyStyle = WebMessageBodyStyle.Wrapped,
    Method = "POST",
    RequestFormat = System.ServiceModel.Web.WebMessageFormat.Json)]
string ValidateCredentials(string spName, string inputxele,string transType);


You don't need to explicitly define that, see this example (POST method):
VB
[OperationContract]
        [WebInvoke(UriTemplate = "AddPost",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        string AddPost(string title, string description, string startTime, string endTime, string noticeTypeId, string groupId, string userId, string approvalStatus);




-KR
 
Share this answer
 
v3
Comments
Raghu Golla 5-Dec-14 1:09am    
Then what is the url i have to pass in httppost from android
Raghu Golla 5-Dec-14 1:32am    
I had used String url="http://10.0.2.2/JsonWcfService/GetEmployees.svc"; and
String url="http://10.0.2.2/JsonWcfService/GetEmployees.svc/";

Now i am getting reponse "Not Found".
Krunal Rohit 5-Dec-14 2:47am    
okay, it should be like this, "GetData?spname={spName}" and so on..
Try this.

-KR
Raghu Golla 5-Dec-14 2:51am    
Would you please tell me what i have to write in UriTemplate and what i have to pass in HttpPost.

As i am very new to wcf services and android.






Krunal Rohit 5-Dec-14 2:56am    
Wait, I made a mistake, See the updated answer.

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900