15

I want to send the JSON text {} to a web service and read the response. How can I do this from android? What are the steps such as creating request object, setting content headers, etc.

My code is here

public void postData(String result,JSONObject obj) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);

    String json=obj.toString();

    try {

        HttpPost httppost = new HttpPost(result.toString());
        StringEntity se = new StringEntity(obj.toString()); 
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se); 

        HttpResponse response = httpclient.execute(httppost);
        String temp = EntityUtils.toString(response.getEntity());
        Log.i("tag", temp);


    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    }
}

what mistake i have done plz correct me because it shows me an bad request error but when i do post in poster it shows me status as Successfull 200 ok

3
  • Try setting this type entity.setContentType("application/json;charset=UTF-8"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8")); Commented Feb 22, 2012 at 6:21
  • @Soni No its not working fine for me , i tried it but it shows me Bad Request error Commented Feb 22, 2012 at 6:29
  • @Soni the only mistake had done is im not setHeader like this in below post httppost.setHeader("Content-type", "application/json"); Commented Feb 22, 2012 at 6:31

3 Answers 3

14

I do this with

httppost.setHeader("Content-type", "application/json");

Also, the new HttpPost() takes the web service URL as argument.

Sign up to request clarification or add additional context in comments.

Comments

1

In the try catch loop, I did this:

         HttpPost post = new HttpPost(
         "https://www.placeyoururlhere.com");
         post.setHeader(HTTP.CONTENT_TYPE,"application/json" );
         List<NameValuePair> nameValuePairs = new
         ArrayList<NameValuePair>(1);
         nameValuePairs.add(new BasicNameValuePair("json", json));
         post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

         HttpClient client = new DefaultHttpClient();
         HttpResponse resp = client.execute(post);
         HttpEntity entity = resp.getEntity();

         response = EntityUtils.toString(entity);

You can add your nameValurPairs according to how many fields you have. Typically the JSON might become really huge, which I will then suggest gzipping it then sending, but if your JSON is fairly small and always the same size the above should work for you.

Comments

1

If it is a web service and not RestAPI call then, you can get the WSDL file from the server and use a SOAP Stub generator to do all the work of creating the Request objects and the networking code for you, for example WSClient++

If you wish to do it by yourself then things get a little tricky. Android doesn't come with SOAP library. However, you can download 3rd party library here: http://code.google.com/p/ksoap2-android/

If you need help using it, you might find this thread helpful: How to call a .NET Webservice from Android using KSOAP2?

If its a REST-API Call like POST or GET to be more specific then its is very simple Just pass a JSON Formatted String object in you function and use org.json package to parse the response string for you.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.