14

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

Sachin Gurnani
  • 2,444
  • 7
  • 36
  • 45
  • Try setting this type `entity.setContentType("application/json;charset=UTF-8"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));` – MKJParekh Feb 22 '12 at 06:21
  • @Soni No its not working fine for me , i tried it but it shows me Bad Request error – Sachin Gurnani Feb 22 '12 at 06:29
  • @Soni the only mistake had done is im not setHeader like this in below post httppost.setHeader("Content-type", "application/json"); – Sachin Gurnani Feb 22 '12 at 06:31

3 Answers3

13

I do this with

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

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

curioustechizen
  • 10,572
  • 10
  • 61
  • 110
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.

Maurice
  • 6,413
  • 13
  • 51
  • 76
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.

Community
  • 1
  • 1