1

I would like to thank all the users in this community for helping me get as far as I am in my project today.

I now need your help once again. So far, I am able to establish a connection in my project from this JSON link (REMOVED FOR PRIVACY CONCERNS)

The problem is I am only able to parse one string, (firstName)

Here is my code:

public class JSONActivity extends Activity { 
static TextView http; 
HttpClient client; 
JSONObject json; 

    final static String URL = "REMOVED FOR PRIVACY CONCERNS

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        http = (TextView) findViewById(R.id.http); 
        client = new DefaultHttpClient(); 
        new Read().execute("firstName"); 

    } 

    public JSONObject getpw(String password) throws ClientProtocolException, 
            IOException, JSONException { 
        StringBuilder url = new StringBuilder(URL); 
        url.append(password); 

        HttpGet get = new HttpGet(url.toString()); 
        HttpResponse r = client.execute(get); 
        int status = r.getStatusLine().getStatusCode(); 
        if (status == 200) { 
            HttpEntity e = r.getEntity(); 
            String data = EntityUtils.toString(e); 
            JSONObject getname = new JSONObject(data); 

            return getname; 
        } else { 
            Toast.makeText(JSONActivity.this, "error", Toast.LENGTH_SHORT); 
            return null; 
        } 
    } 

    public class Read extends AsyncTask<String, Integer, String> { 

        @Override 
        protected String doInBackground(String... arg0) { 
            // TODO Auto-generated method stub 
            try { 
                json = getpw("trustme"); 
                return json.getString("firstName"); 
            } catch (ClientProtocolException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (JSONException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 

            return null; 
        } 

        @Override 
        protected void onPostExecute(String result) { 
            // TODO Auto-generated method stub 
            http.setText(result); 
        } 
    } 
} 

My question is, how can I parse multiple strings rather than just "firstName"?

8
  • Can't you parse everything in doInBackground and return as an array of strings or via a field? Commented Mar 13, 2012 at 18:15
  • Thats what I also assumed, but it seems like in my code where it says " return json.getString("firstName"); " it is only allowing me to return one. Commented Mar 13, 2012 at 18:51
  • I believe you can have your Read class derived from AsyncTask<String, Integer, ArrayList<String> > then your doInBackground will have to return ArrayList<String> which you can populate and return then as you need Commented Mar 13, 2012 at 19:14
  • Would you care to show me an example, Alex? Commented Mar 13, 2012 at 19:25
  • I believe you can just replace the 3rd String template argument in your AsyncTask with HashMap<String> and also replace String return type of doInBackground with the same. Then you create new HashMap<String> result variable in doInBackground() populate it with the parsed values something like res.put("firstName", yourFirstNameData) and other such puts and return the hashmap. Commented Mar 13, 2012 at 20:22

1 Answer 1

1

You can get it all by doing the following:

String firstname = json.getString("firstName");
String lastname = json.getString("lastName");
int checkedIn = json.getInt("checkedIn");
int updated = json.getInt("checkedindatetime");

JSONObject address = json.getJSONObject("address");
String streetaddress = address.getString("streetAddress");
String city = address.getString("city");
etc...

JSONArray phoneNumbers = json.getJSONArray("phoneNumber");
String type = phoneNumbers.getJSONObject(0).getString("type");
etc...

Hope this helps.

A good resource for looking at json, is this validator.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, coder. I guess my question really is, how do i set it up so I can view all the strings in a textview. would it be for example, String firstName = textview.getText().toString(); ?
Why don't you make an object that has everything you need from the json, add each of the string to the respective values in the object, then return the object? That way you would have all your values store ed that you could access.
sounds so simple to do, but easier said than done lol. I'll poke around for a bit and give your suggestion a try. Thanks!

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.