Skip to content Skip to sidebar Skip to footer

Android Json Parser From Url (non Depecreated)

Okay I'm coming close to my wits end here. I have been following code samples and reading threads on how to hit a URL and read in the JSON object following that web request. But th

Solution 1:

From this sample project from this book, here is a thread that loads the latest Stack Overflow questions using HttpURLConnection and parses them using Gson:

/***
  Copyright (c) 2013-2014 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */package com.commonsware.android.hurl;

import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.Gson;
import de.greenrobot.event.EventBus;

classLoadThreadextendsThread {
  staticfinal String SO_URL=
      "https://api.stackexchange.com/2.1/questions?"
          + "order=desc&sort=creation&site=stackoverflow&tagged=android";

  @Overridepublicvoidrun() {
    try {
      HttpURLConnection c=
          (HttpURLConnection)newURL(SO_URL).openConnection();

      try {
        InputStream in=c.getInputStream();
        BufferedReader reader=
            newBufferedReader(newInputStreamReader(in));
        SOQuestions questions=
            newGson().fromJson(reader, SOQuestions.class);

        reader.close();

        EventBus.getDefault().post(newQuestionsLoadedEvent(questions));
      }
      catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Exception parsing JSON", e);
      }
      finally {
        c.disconnect();
      }
    }
    catch (Exception e) {
      Log.e(getClass().getSimpleName(), "Exception parsing JSON", e);
    }
  }
}

Here, SOQuestions is the Gson-annotated class for the Stack Exchange questions API, and I am using greenrobot's EventBus to get the results over to a fragment. Of course, you would replace the Stack Exchange API URL with your own and would need to parse whatever its JSON output is.

I'm not aware of many Android developers using JSONObject anymore, given much better options in Gson, Jackson, etc., but you would need to read in the InputStream into a String to pass to the appropriate JSONObject constructor.

You might also consider using higher-order approaches. For example, this sample app is a clone of the first that uses Square's Retrofit library for contacting the Stack Exchange API Web service, rather than HttpUrlConnection.

Post a Comment for "Android Json Parser From Url (non Depecreated)"