Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Blog Reader Android App Getting Data from the Web Getting JSON Data from an HTTP Request

isNetworkAvailable not returning to true,

I can't access JSON file even the network is available, isNetworkAvailable not returning to true, so getBlogPostTask() never executed. Is it any problem with my genymotion? here's error from logcat: 06-25 08:13:45.479: W/EGL_genymotion(1383): eglSurfaceAttrib not implemented

Here's my code:

public class MainListActivity extends ListActivity{

    protected String[] BlogPostTitle;
    public static final int NUMBER_OF_POST = 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()){
            GetBlogPostTask getBlogPostTask = new GetBlogPostTask();
            getBlogPostTask.execute();

        }
        else{
            Toast.makeText(this, "Network is unavailable", Toast.LENGTH_LONG).show();

        }
        //Toast.makeText(this, getString(R.string.no_item),Toast.LENGTH_LONG).show();
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

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

    @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);
        return true;
    }

    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public class GetBlogPostTask extends AsyncTask<Object, Void, String> {

        @Override
        protected String doInBackground(Object... arg0) {
            int responseCode = -1;

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

                responseCode = connection.getResponseCode();
                if(responseCode == 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 responseData = new String(charArray);
                    Log.v(TAG, responseData);
                }
                else{
                    Log.i(TAG, "Unsuccessfull HTTP response code: " + responseCode);
                }
            }
            catch(MalformedURLException e){
                Log.e(TAG,"Error ditemukan: ", e);
            }
            catch(IOException e){
                Log.e(TAG,"Error ditemukan: ",e);
            }
            catch(Exception e){
                Log.e(TAG,"Error ditemukan: ",e);
            }

            return "Get code: " + responseCode;

        }
    }

}

been stuck on it for hours, can somebody help me figure it out? Thanks a lot!