2

I have a sample url like this:

http://www.sample.com/mobile.cgi?action=login&json_request={"user":{"name":"a","pass":"123","gender":"m","age":"25"}}

I can basically use webview.loadurl to send data to the server, but the point is...I cant get response from the server. I'm new to json. Is there any way that I can post json using the regular way? like HttpPostmaybe? and be able to get response properly.

Thanks!!

user1865027
  • 2,915
  • 4
  • 26
  • 56

1 Answers1

1

If you just wish to post JSON using HTTP and get a response back, there are many posts of stackoverflow which will help you answer that. Check How to send POST request in JSON using HTTPClient? question. I think this question answers what you are trying to say. Hope this helps you. If you have any specific concern you can always comment.

Update As you said that you already have keys and corresponding values in addition to URL.

The first step would be to create a JSON Object. Convert it to string and then you can send it using HTTPClient and get the response back. Something like:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/");

try {
    // Add your data

    JSONObject user = new JSONObject();
    user.put("Name", "a");
    user.put("pass", "123");

    // Create StringEntity
    StringEntity se = new StringEntity( user.toString());  
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    httppost.setEntity(se);

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

You can check links like this to see further exact format. I wanted to tell you the method as to how you can proceed. Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 24,785
  • 8
  • 89
  • 115
  • i found many useful answers on StackOverFlow, but the thing is that I don't know how to use them from the url I got. I know name, pass, gender are keys, and a, 123, 25 are values. How can I transform my url to that format? should I include user as the name somewhere? should I include json_request? or the {}?? – user1865027 May 22 '13 at 22:14
  • long story short...that url above is the only thing I got. I have no clue how to post & get response by just having that url – user1865027 May 22 '13 at 22:16
  • Check the updated answer. You can follow the steps. See the added link for more info. – Shobhit Puri May 22 '13 at 22:29
  • i tried this code earlier from other posts, but still cant figure out...the 2nd line is already not working bcz eclipse complains that HttpPost doesnt have such constructor. – user1865027 May 22 '13 at 23:19
  • Which class are you importing for `HttpPost`? This kind of constructor has been defined for `HttpPost`. See the constructors defined in official [HttpPost](http://developer.android.com/reference/org/apache/http/client/methods/HttpPost.html#HttpPost%28%29) android document. – Shobhit Puri May 23 '13 at 00:03
  • I found several tutorial online regarding how to use json, but still cant figure out how to implement that to my app. In your example, you have "http://www.yoursite.com/" in your HttpPost. but what about in my case? should I put "http://www.sample.com/mobile.cgi?" or "http://www.sample.com/mobile.cgi?action=login&json_request=" ? Also, I tried the example u posted, but got error on this line `HttpResponse response = httpclient.execute(httppost);` – user1865027 May 23 '13 at 06:53
  • thank you so much for the help. I really can't figure out how to do post and get response by just having that url... – user1865027 May 23 '13 at 06:55
  • Why don't you use `HTTP GET`? You should use GET when the interaction is more like a question and your are expecting a response. See [Here](http://www.w3.org/2001/tag/doc/whenToUseGet.html). It allows you to use parameters within the `HttpGet` request string. See [here](http://stackoverflow.com/a/2959431/1306419). If you want to do a post with parameters see [this](http://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request) thread. – Shobhit Puri May 23 '13 at 14:10
  • I found someone had the exact same question from your link [here](http://stackoverflow.com/questions/2959316/how-to-add-parameters-to-a-http-get-request-in-android/2959431#2959431) – user1865027 May 23 '13 at 17:41
  • @Yog Guru was asking the same thing, but no one answered. I found another way to make it work without getting response from json! thanks for all the help – user1865027 May 23 '13 at 17:43
  • Great to hear that you got it working.What did you do? #Just Curious – Shobhit Puri May 23 '13 at 17:45