0

I'm new to the android side of development, and one thing that I know that I wanted to try out was how to use HTTP Get. I've got the whole method setup so that the text is sent and my results come back in a string, but what I wanted to know was how to I take that string and extract just the parts I really need. for example the string comes back with

{"id":"124343","name":"somename" }

and if I just wanted to get the id part of that start how would I do that in android. I'm searching through the docs, but so far I haven't really found anything and then most of what I do find revolves around using JSON as well.

Below is the code I'm currently using (which I've compiled together from several different posts) but maybe I need to switch to using JSON for parsing purposes and I'm just not sure where to make that change

     //This class is called after a button is pressed and the HTTPGet string is compiled from text within a textview and a static string (this already works)
     private class LongRunningGetIO extends AsyncTask<Void, Void, String> {
    protected String getASCIIContentFromEntity(HttpEntity entity)
            throws IllegalStateException, IOException {
        InputStream in = entity.getContent();
        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n > 0) {
            byte[] b = new byte[4096];
            n = in.read(b);
            if (n > 0)
                out.append(new String(b, 0, n));
        }
        return out.toString();
    }

    @Override
    protected String doInBackground(Void... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        String question = questionText.getText().toString();
        String newString = httpPath + "?userText=" + question;
        Log.i("Message", newString);
        HttpGet httpGet = new HttpGet(newString);
        String text = null;
        try {
            HttpResponse response = httpClient.execute(httpGet,
                    localContext);
            HttpEntity entity = response.getEntity();
            text = getASCIIContentFromEntity(entity);


        } catch (Exception e) {
            return e.getLocalizedMessage();
        }
        return text;
    }


    protected void onPostExecute(String results) {
        if (results != null) {
            Log.i("String", results);
        }
    }
}

2 Answers 2

2

This is how to extract data from JSON string

JSONObject obj =  new JSONObject(YourJSONString);
String id = obj.getString("id");
String name = obj.getString("name");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the tip, right now I'm not using JSON so is there another way to do this without JSON or should I switch over to using JSON for parsing purposes? I'll update the post with my code
This is what I needed to do, just turn the result string into a JSON object since it was already formatted that way and then I was able to extract what I needed. Thanks for the help
1

Is this what you want::

HttpGet httpGet = new HttpGet(newString);
String text = null;
    try {
          HttpResponse response = httpClient.execute(httpGet, localContext);
          InputStream content = response.getEntity().getContent();
          BufferedReader buffer = new BufferedReader(new InputStreamReader(content));

          while ((text = buffer.readLine()) != null) {

              //Work with "text" here...
             //Split the string as you wish with "text.split();" function..

             }
         } catch (Exception e) {
            return e.getLocalizedMessage();
         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.