0

I'm creating an android application in which I would like to send some values to the server and in response the server gives me a json response. The values I pass through is of Json post method type.

I would like to know how can I pass the following values to the Json post url:

{
    "CPSProfileInfo": 
    {
        "cpsName": "auto",
        "userId": "0", 
        "cpsAddress": "0@0India", 
        "searchLimit": "10@0", 
        "searchFlag": "0"
    }
}

throught my application so far, I've been using

params.put("cpsName","auto");

But I don't know how to proceed with this type as I don't know how to send the value for CPSProfileInfo

Kindly help me solving this issue!!!

Ziem
  • 6,059
  • 7
  • 49
  • 83
Parthiban M
  • 1,096
  • 1
  • 9
  • 29
  • http://stackoverflow.com/questions/6218143/how-to-send-post-request-in-json-using-httpclient this should help you :) – Klawikowski Apr 24 '15 at 09:15

3 Answers3

0

You can go through the tutorial android-json-parsing-tutorial...

First know how json format looks!

micky
  • 623
  • 5
  • 17
0

To send Json to server, you can try my code:

HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
HttpClient httpclient = new DefaultHttpClient(myParams);


HttpPost httppost = new HttpPost("*your server addresss and query*");
httppost.setHeader("Content-type", "application/json");
//if needed for authorization:
httppost.setHeader("Authorization", "Bearer " + "*Your string token retrieved from server*");

StringEntity se = new StringEntity("*your json here*");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);

HttpResponse response = httpclient.execute(httppost);

If you have any questions, please ask.

Daniel Krzyczkowski
  • 2,342
  • 2
  • 15
  • 26
0

Checkout Retrofit, it is an awesome networking library by Square.

When posting in retrofit you can just create a class for the object:

@POST("/users/new")
void createUser(@Body User user, Callback<User> cb);

This is posting to the url 'yourdefinedapi.com/users/new with an User object, which is just a POJO object :)

See detailed instructions here: http://square.github.io/retrofit/

alliannas
  • 304
  • 3
  • 5