2

Possible Duplicate:
How to send a JSON object over Request with Android?

As I am new to Android development, I struck up with a problem sending requests to a web service in the form of JSON. Googling, I found the following code for sending requests using parameters. Here is the Java class we are sending parameters in the form of:

Main.java

RestClient client = new RestClient(LOGIN_URL);
client.AddParam("Email", _username);
client.AddParam("Passwd", _password);

try {
  client.Execute(RequestMethod.POST);
} catch (Exception e) {
  e.printStackTrace();
}
String response = client.getResponse();

But here I want to send parameters in the form of JSON, like for example I want to send parameters in this form:

{
  "login":{
    "Email":_username,
    "Passwd":_password,
  }
}

So, can anyone help me? How can I send parameters in the form of JSON?

Community
  • 1
  • 1
  • 3
    I haven't looked at the link yet, but I would be wary of an article that uses PascalCase for methods when Java developers universally use camelCase. – Abdullah Jibaly Nov 25 '12 at 05:53

1 Answers1

3

The example you are posting uses a 'library' put together by someone as a wrapper around Apache's HttpClient class. It's not a particularly good one. But you don't need to use that wrapper at all, the HttpClient itself is dead simple to utilize. Here's a code sample you can build on:

final String uri = "http://www.example.com";
final String body = String.format("{\"login\": {\"Email\": \"%s\", \"Passwd\": \"%s\"}", "me@email.com", "password");

final HttpClient client = new DefaultHttpClient();
final HttpPost postMethod = new HttpPost(uri);
postMethod.setEntity(new StringEntity(body, "utf-8"));

try {
    final HttpResponse response = client.execute(postMethod);
    final String responseData = EntityUtils.toString(response.getEntity(), "utf-8");
} catch(final Exception e) {
    // handle exception here
}

Note that you would most likely be using a JSON library to serialize a POJO and create the request JSON.

Perception
  • 79,279
  • 19
  • 185
  • 195
  • Here client.execute(postMethod) means it will call execute method in HTTPClient class.. Can you post the code of HTTPClient class... –  Nov 25 '12 at 06:57
  • `HTTPClient` is an interface in the Apache HttpCommons library. It comes bundled with your Android distro. This is a helpful reference: http://developer.android.com/reference/org/apache/http/client/HttpClient.html – Perception Nov 25 '12 at 06:59
  • @Perception.. Thanks for the code. But if i am executing with the following code i am not getting the full response from the webservice. It's breaking and getting only 50% response. Simply to say if I am having 200 names of data to receive from webservice i am receiving only 50 names.. Not receiving remaining name. I am not getting any idea what is the problem. Can you help me with this.. –  Nov 25 '12 at 07:39
  • No problem, if the answer helped you then please feel free to accept my answer. Also, post your other issue as a separate question. – Perception Nov 25 '12 at 07:47
  • @Perception.. Can you please check the following new question posted by me.. http://stackoverflow.com/questions/13549289/unable-to-receive-full-response-from-webservice-in-android –  Nov 25 '12 at 07:57
  • @Perception : I want your small help , related above code – Nirav Ranpara Feb 18 '13 at 12:27
  • @NiravRanpara - if you have a question, it might help to post it as such, or create a chat and invite me to it. – Perception Feb 18 '13 at 13:27
  • @Perception: Come in chat pls – Nirav Ranpara Feb 18 '13 at 13:31