Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

JIHOON JUNG
JIHOON JUNG
10,927 Points

JSON

JSON DATA

{ stories: [ { description: "NBA mock draft 8.0: Towns or Okafor to Minnesota?", guid: [ { isPermalink: "true", value: "http://sportswire.usatoday.com/2015/05/20/2015-nba-mock-draft-8-0-minnesota-timberwolves-lottery-emanuel-mudiay-karl-anthony-towns-new-york-knicks-jahlil-okafor-frank-kaminsky-los-angeles-lakers-sam-dekker-boston-celtics-2/" } ], link: "http://sportswire.usatoday.com/2015/05/20/2015-nba-mock-draft-8-0-minnesota-timberwolves-lottery-emanuel-mudiay-karl-anthony-towns-new-york-knicks-jahlil-okafor-frank-kaminsky-los-angeles-lakers-sam-dekker-boston-celtics-2/", pubDate: "Wed, 20 May 2015 04:01:05 GMT", title: "NBA mock draft 8.0: The post-lottery edition" }, { description: "She is 45, looks like this, and we are very, very jealous.", guid: [ { isPermalink: "true", value: "http://entertainthis.usatoday.com/2015/05/20/jennifer-lopez-is-smoldering-in-sexy-cutout-bathing-suit/" } ], link: "http://entertainthis.usatoday.com/2015/05/20/jennifer-lopez-is-smoldering-in-sexy-cutout-bathing-suit/", pubDate: "Wed, 20 May 2015 05:56:02 GMT", title: "J. Lo. smolders in sexy cutout bathing suit" }, { description: "Melissa McCarthy is full of wisdom.", guid: [ { isPermalink: "true", value: "http://entertainthis.usatoday.com/2015/05/20/melissa-mccarthy-ellen-sexist-criticism-we-tear-down-women-in-this-country/" } ], link: "http://entertainthis.usatoday.com/2015/05/20/melissa-mccarthy-ellen-sexist-criticism-we-tear-down-women-in-this-country/", pubDate: "Wed, 20 May 2015 02:21:37 GMT", title: "Melissa McCarthy: 'We tear down women'" }, { description: "The Washington Post reports that Fox News, which hosts the first debate in Cleveland on Aug. 6, has devised a criteria for participation.", guid: [ { isPermalink: "true", value: "http://onpolitics.usatoday.com/2015/05/20/fox-news-debate-republicans/" } ], link: "http://onpolitics.usatoday.com/2015/05/20/fox-news-debate-republicans/", pubDate: "Wed, 20 May 2015 05:09:14 GMT", title: "Fox News to cap first GOP debate based on polls" }, { description: "Sometimes a little plastic surgery can be a good thing.", guid: [ { isPermalink: "true", value: "http://entertainthis.usatoday.com/2015/05/20/jillian-michaels-my-nose-job-was-life-changing/" } ], link: "http://entertainthis.usatoday.com/2015/05/20/jillian-michaels-my-nose-job-was-life-changing/", pubDate: "Wed, 20 May 2015 04:50:50 GMT", title: "Jillian Michaels: Nose job was life-changing" }, { description: "Late-night host takes his final sprint across the stage.", guid: [ { isPermalink: "true", value: "http://entertainthis.usatoday.com/2015/05/20/watch-letterman-makes-his-grand-entrance-to-final-show/" } ], link: "http://entertainthis.usatoday.com/2015/05/20/watch-letterman-makes-his-grand-entrance-to-final-show/", pubDate: "Wed, 20 May 2015 07:49:10 GMT", title: "Watch: Letterman makes entrance to last show" }, { description: "Jeb Bush and other GOP presidential hopefuls' rationalizations miss the point.", guid: [ { isPermalink: "true", value: "http://www.usatoday.com/story/opinion/2015/05/20/iraq-war-jeb-bush-republican-candidates-hillary-editorials-debates/27681187/" } ], link: "http://www.usatoday.com/story/opinion/2015/05/20/iraq-war-jeb-bush-republican-candidates-hillary-editorials-debates/27681187/", pubDate: "Wed, 20 May 2015 08:11:33 GMT", title: "Admit Iraq War was a disastrous mistake: Our view" }, { description: "Obama threw away hard-earned gains.", guid: [ { isPermalink: "true", value: "http://www.usatoday.com/story/opinion/2015/05/20/iraq-saddam-hussein-obama-bush-william-kristol-editorials-debates/27681429/" } ], link: "http://www.usatoday.com/story/opinion/2015/05/20/iraq-saddam-hussein-obama-bush-william-kristol-editorials-debates/27681429/", pubDate: "Wed, 20 May 2015 08:18:10 GMT", title: "William Kristol: We were right to fight in Iraq" }, { description: "Get ready to rock with USA TODAY's Summer Music Festivalaganza interactive.", guid: [ { isPermalink: "true", value: "http://www.usatoday.com/pages/interactives/2015-music-festivals/" } ], link: "http://www.usatoday.com/pages/interactives/2015-music-festivals/", pubDate: "Wed, 8 Apr 2015 09:37:38 GMT", title: "Interactive: Summer music festivals heat up" } ] }


Android Code

package com.oikostuio.newsapp;

import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.widget.Toast;

import com.squareup.okhttp.Call; import com.squareup.okhttp.Callback; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response;

import org.json.JSONException; import org.json.JSONObject;

import java.io.IOException;

public class MainActivity extends ActionBarActivity {

public static final String TAG = MainActivity.class.getSimpleName();
private CurrentNews mCurrentNews;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String apiKey = "";
    String newsURL = "http://api.usatoday.com/open/articles/topnews/home?count=10&days=0&page=0&encoding=json&api_key=" + apiKey;

    if (isNetworkAvailable()) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(newsURL).build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData);
                    if (response.isSuccessful()) {
                        mCurrentNews = getCurrentDetails(jsonData);
                    } else {
                        alertUserAboutError();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Exception caught: ", e);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
        Log.v(TAG, "Main UI code is running");
    } else {
        Toast.makeText(this, "Network is unavailable!", Toast.LENGTH_LONG).show();
    }

}

private CurrentNews getCurrentDetails(String jsonData) throws JSONException {

    JSONObject news = new JSONObject(jsonData);
    JSONObject description = news.getJSONObject("stories");
    Log.i(TAG, "From JSON: " + description);

    return new CurrentNews();

}

private boolean isNetworkAvailable() {

    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    boolean isAvailable = false;
    if(networkInfo != null && networkInfo.isConnected()) {
        return isAvailable = true;
    }
    return isAvailable;


}

private void alertUserAboutError() {
    AlertDialogFragment dialog = new AlertDialogFragment();
    dialog.show(getFragmentManager(), "error_dialog");
}

}

Got no value :Description, What is wrong with me?

JIHOON JUNG
JIHOON JUNG
10,927 Points

Solved this,

private CurrentNews getCurrentNews(String jsonData) throws JSONException {

    JSONObject news = new JSONObject(jsonData);
    JSONArray newsList = news.getJSONArray("stories");
    CurrentNews currentNews = new CurrentNews();
    for(int i=0; i<newsList.length(); i++) {
        JSONObject jsonObject = newsList.getJSONObject(i);
        currentNews.setDescription(jsonObject.getString("description"));
        currentNews.setTitle(jsonObject.getString("title"));
    }

    return currentNews;
}

Just left this one for people who look for the answer