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 Weather App (2015) Concurrency and Error Handling Configuring the Alert Dialog

CD Lim
CD Lim
4,782 Points

AlertDialogue / Error Message keeps on appearing

Alert Dialogue / Error Message keeps on appearing even after I fix the code. There are no error shown, red line or green line in the code. However, when I launch Stormy it show Opps! Sorry! Dialogue Message. I have also reset and change my api key but it is not not working...

Here is my code in MainActivity.java

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();

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

        String apiKey = " 9f7caaba306f46b0ce3a72345a890340";
        double latitude = 37.8267;
        double longitude = -122.423;
        String forecastUrl = "https://api.forecast.io/forecast/"+ apiKey +
            "/" + latitude + "," + longitude;

        if(isNetworkAvailable()){
        OkHttpClient client = new OkHttpClient();
        Request request  = new Request.Builder()
                .url (forecastUrl)
                .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{
                    Log.v(TAG, response.body().string());
                    if (response.isSuccessful()){
                    } else {
                        alertUserAboutError();
                    }
                } catch (IOException e){
                    Log.e(TAG, "Exception caught:",e);
                }
            }
        });}
        else {
            Toast.makeText(this,getString(R.string.network_unavailable_message),
            Toast.LENGTH_LONG).show();
        }
        Log.d(TAG, "Main UI code is Running");
    }

    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;
    }

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