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 Build a Blog Reader Android App Getting Data from the Web Parsing Data Returned in JSON Format

James N
James N
17,864 Points

i am getting errors!!!!

my code for MainListActivity.java is:

package james.blogreader;

import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class MainListActivity extends ListActivity {

    protected String[] mBlogPostTitles;
    public static final int NUMBER_OF_POSTS = 20;
    public static final String TAG = MainListActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list);

        if (isNetworkAvailable()) {
            GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask();
            getBlogPostsTask.execute();
        }
        else {
            Toast.makeText(this,"No network detected, check your internet settings.",Toast.LENGTH_LONG).show();
        }

    }

        //Toast.makeText(this,getString(R.string.no_items),Toast.LENGTH_LONG).show();
    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        return false;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_list, menu);

        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()){
            isAvailable = true;
        }
        return isAvailable;
    }


    private class GetBlogPostsTask extends AsyncTask<Object,Void,String> {
        @Override
        protected String doInBackground(Object... arg0) {
            int responceCode = -1;

            try {
                URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
                HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
                connection.connect();

                 responceCode = connection.getResponseCode();
                if (responceCode == HttpURLConnection.HTTP_OK) {
                    InputStream inputStream = connection.getInputStream();
                    Reader reader = new InputStreamReader(inputStream);
                    int contentLength = connection.getContentLength();
                    char[] charArray = new char[contentLength];
                    reader.read(charArray);
                    String responceData = new String(charArray);
                    JSONObject jsonResponce= new JSONObject(responceData);
                    String status = jsonResponce.getString("status");
                    Log.v(TAG, status);
                    JSONArray jsonPosts = jsonResponce.getJSONArray("posts");
                    for (int i = 0;i < jsonPosts.length();i++) {
                        JSONObject jsonPost = jsonPosts.getJSONObject(i);
                        String title = jsonPost.getString("title");
                        Log.v(TAG,"Post " + i + ": " + title);
                    }
                }
                else {
                    Log.i(TAG,"Unsuccessful HTTP Responce Code : " + responceCode);
                }

            }
            catch (MalformedURLException e) {
                Log.e(TAG,"Exception caught",e);
            }
            catch (IOException e) {
                Log.e(TAG,"Exception caught", e);
            }
            catch(Exception e){
                Log.e(TAG,"Exception caught", e);
            }
            return  "Code: " + responceCode;
        }


    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }
}

my errors are:

cannot resolve symbol networkInfo

and i also have 2 warnings:

field mBlogPostTitles is never used
Variable networkInfo is never used

i would REALLY appreciate your help!!!!

1 Answer

Chris Shaw
Chris Shaw
26,650 Points

Hi James,

You're getting a symbol error because isNetworkAvailable is currently where your networkInfo variable lives, instead what you want to do is assign networkInfo as a class property and then set the value of manager.getActiveNetworkInfo() back to the property.

public class MainListActivity extends ListActivity {
    protected NetworkInfo networkInfo;

    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = manager.getActiveNetworkInfo();
        return false;
    }
}
James N
James N
17,864 Points

thanks, that helped a lot!!