-5

I am passing url to server with some data like this,

     HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("firstname",firstname));
            nameValuePairs.add(new BasicNameValuePair("lastname", lastname));

           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);

it gives response like this

{
      "message": "Successful",
    "data": {
        "user_id": 32,
        "firstname": "myname",
        "lastname": "lastname"
    }
 }

how can i get the user_id from above response or is there any other way. Please tell me how can i solve this issue.Thanks in advance

  • @selvin :i know how to get the data from json.JSONObject obj1 = response.getJSONObject("data"); obj1.getJSONObject("user_id");. are you understand my question – user3814997 Dec 01 '14 at 11:19
  • as you can see, no, and not only me ... `how can i get the user_id from above response` <= this question is the only one from the question body and it is duplicate ... `how to store server response and how to get specific data in my application` <= the questions from title are too broad/not clear – Selvin Dec 01 '14 at 11:29
  • All of you please understand the question first. He wants to STORE data first and then wants to retrieve. You can use MAP data structure for KEY and VALUE to store and retrieve. – Chitrang Dec 01 '14 at 11:31

3 Answers3

0

Like this:

try {
    JSONObject obj = new JSONObject(response.getBody());
    JSONObject objData = obj.getJSONObject("data");
    Integer userId = objData.getInt("user_id");
} catch (JSONException jsEx) {
    jsEx.printStackTrace();
}
reixa
  • 6,903
  • 6
  • 49
  • 68
0

do like this,

first use this method to get response data,

public String getResponseBody(final InputStream instream) throws IOException, ParseException {
        if (instream == null) {
            return "";
        }
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "utf-8"));

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
        } finally {
            instream.close();
            reader.close();
        }
        return buffer.toString();
    }

then,

JSONObject results = new JSONObject(getResponseBody(httpEntity.getContent()));
JSONObject user = results.getJSONObject("data");
String userid = user.getString("user_id")
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0

Add this after your code

HttpEntity httpEntity = response.getEntity();
String responseJson = EntityUtils.toString(httpEntity);

Here you will get your json as string in response. And your have to parse this json to get the desired value like this.

try {
    JSONObject obj = new JSONObject(responseJson);
    JSONObject objData = obj.getJSONObject("data");
    Integer userId = objData.getInt("user_id");
} catch (JSONException jsEx) {
    jsEx.printStackTrace();
}

Hop this makes things clear for you :)

Nitesh
  • 3,868
  • 1
  • 20
  • 26
  • Hey assuming you are new here, You can accept the answers that properly fits for your questions. If one of the answers below answered your question, the way this site works works, you'd "accept" the answer, more here:http://stackoverflow.com/help/someone-answers . But only if your question really has been answered. – Nitesh Dec 01 '14 at 11:45
  • i don't have sufficient reputaion. Thats why am not voted – user3814997 Dec 01 '14 at 11:50