0

I'm trying to parse a JSON from an url and already found some solutions that worked, but only with HttpClient - and since that isn't supported anymore I want (and need) to do it with HttpUrlConnection but I can't get it to work with that.

My JSON data looks like this

[
    {
        "Id":12345,
        "Abc":
            {
                "Name":"Testname"
            },
        "Start":"2015-08-28T10:07:00",
        "End":"2015-08-28T10:08:00",
    },
    {
        "Id":23456,
        "Abc":
            {
                "Name":"Testname2"
            },
        "Start":"2015-08-28T10:07:00",
        "End":"2015-08-28T10:08:00",
    }
]

What's the simplest way to parse the JSON data into a JSON Object? (I want to show the data in a ListView later)

4
  • The fact it is JSON should be irrelevant. The problem it seems you are having, is understanding how to use HttpUrlConnection. Vogella has a wonderful tutorial on this. Commented Sep 1, 2015 at 9:50
  • stackoverflow.com/questions/14185338/… You can use a JSON Library (Jackson, Gson, ...) to get objects from the json. This is no big deal and has nothing to do with HttpUrlConnection. Commented Sep 1, 2015 at 9:51
  • Also consider looking at using Gson to parse Json into usable objects :) Commented Sep 1, 2015 at 9:53
  • show us what you tried so far. and if you are getting this response means there is no problem with httpUrlConnection. Commented Sep 1, 2015 at 9:54

2 Answers 2

3

Try this:

public String getJSON(String url, int timeout) {
    HttpURLConnection c = null;
    try {
        URL u = new URL(url);
        c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setRequestProperty("Content-length", "0");
        c.setUseCaches(false);
        c.setAllowUserInteraction(false);
        c.setConnectTimeout(timeout);
        c.setReadTimeout(timeout);
        c.connect();
        int status = c.getResponseCode();

        switch (status) {
            case 200:
            case 201:
                BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line+"\n");
                }
                br.close();
                return sb.toString();
        }

    } catch (MalformedURLException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
    } finally {
       if (c != null) {
          try {
              c.disconnect();
          } catch (Exception ex) {
             Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
          }
       }
    }
    return null;
}

And then parse the JSON with some JSON library like for example GSON doing something like this:

JsonElement jelement = new JsonParser().parse(getJSON(url, timeout));
JsonObject  jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("data");
Sign up to request clarification or add additional context in comments.

Comments

0

If your JSON data is like you say, the parser can fail to parse it, because it's not a valid JSON.

"End":"2015-08-28T10:08:00",

Removing the coma on the last field of an object can help you.

Edit (response to Andreu Rodríguez i Donaire comment): org.json native library provided by Android can also do the job.

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.