Search code examples
androidweb-serviceswcf-data-services

calling the wcf service POST from the android


my url is http://example.com/WcfService/MasterService.svc

method name is "SearchOrganizations";

String url = URL1 + "/" + METHOD_NAME_SEARCH_ORGANIZATION + "/";

so my url will be http://example.com/WcfService/MasterService.svc/SearchOrganizations/

service type is POST. here is my code to call this service from android

JSONObject jsonObjSend = new JSONObject();
        try {
            jsonObjSend.put("data", params[0]);
            jsonObjSend.put("CurrentUserId", params[1]);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<NameValuePair> paramswithkey = new ArrayList<NameValuePair>();
        paramswithkey.add(new BasicNameValuePair("data", params[0]));
        paramswithkey
                .add(new BasicNameValuePair("CurrentUserId", params[1]));
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(url);

        // httpPostRequest.setHeader("Accept", "application/soap+xml");
        httpPostRequest.setHeader("Content-Type",
                "application/x-www-form-urlencoded; charset=utf-8");
        UrlEncodedFormEntity se = null;
        try {
            // se = new StringEntity(jsonObjSend.toString());
            se = new UrlEncodedFormEntity(/*
                                         * jsonObjSend.toString().getBytes(
                                         * "UTF8")
                                         */paramswithkey);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        httpPostRequest.setEntity(se);
        HttpResponse response = null;
        try {
            response = client.execute(httpPostRequest);
        } catch (ClientProtocolException e) {
            Toast.makeText(getApplicationContext(),
                    "Please check your internet connection",
                    Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        logI("BEFOR response METHOD_NAME_SEARCH_ORGANIZATION :" + response);
        BasicResponseHandler responseHandler = new BasicResponseHandler();
        String strResponse = null;
        if (response != null) {
            try {
                strResponse = responseHandler.handleResponse(response);
            } catch (HttpResponseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        logI("AFTER response METHOD_NAME_SEARCH_ORGANIZATION :"
                + strResponse);

I have used this code using the JsonObject as well as NameValuePair but i am getting the exception of following

 org.apache.http.client.HttpResponseException: Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.

.Net side they have called the service using the x-www-form-urlencoded but i am getting the expected type should be text/xml; charset=utf-8

How could i manage to call the POST service. I have tried with the different ways to call this service but failed to get the response

http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

but i couldn't be able to get the response.


Solution

  • My code is also working fine but there is a problem of url from the webservice side that was given to me.

    http://example.com/WcfService/MasterService.svc/web/SearchOrganizations/
    

    Thanks for helping me user1621629...