Wednesday, 1 February 2012

How to send Json data to Server in android.



In this part we are sending data to the server(which is developed in node.js) as json String .

The response is responseString.


import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.MultipartPostMethod;

public class HttptRequest {

public String requestServerForData(String JSONString)
{

String responseString = null;
 HttpClient client = new HttpClient();
     MultipartPostMethod mPost = new MultipartPostMethod("ServerURL");
     client.setConnectionTimeout(10000);  
     mPost.addParameter("jsonstring", JSONString);
     try {
int statusCode1 = client.executeMethod(mPost);
responseString=mPost.getResponseBodyAsString();
System.out.println("status response=========" + mPost.getResponseBodyAsString());
mPost.releaseConnection();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseString;
}

No comments:

Post a Comment