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 Concurrency and Error Handling Making Our Code Asynchronous

Dhruv Mittal
Dhruv Mittal
3,050 Points

Unable to get the API Response from darksky API, the response always goes to onFailure

I have copied the code from the video; however, I am not able to get the JSON response in my logcat. I added a log message in my onFailure and I think the code always goes to the onFailure method. Not too sure why I can't connect to the API, I am pretty sure my URL is correct too as I tried simply copying and pasting from the website too.

Code:

package com.dhruvmittal.stormy;

import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle; import android.util.Log;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response;

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 = "64a62ee416652c32f316cae864d77ada";
    double latitude = 37.8267;
    double longitude = -122.4233;

    String forecastURL = "https://api.darksky.net/forecast/"+apiKey+"/"+latitude+","+longitude;

    //String forecastURL = "https://api.darksky.net/forecast/e614f7a3d13a567393ceed653c28de35/37.8267,-122.4233";

    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(@NotNull Call call, @NotNull IOException e) {
            Log.v(TAG, "CallBack Failed");
        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            try {

                Log.v(TAG, response.body().string());

                if (response.isSuccessful()){

                }else{
                    alerUserAboutError();
                }
            } catch (IOException e) {
                Log.e(TAG, "Exception Caught: ",e );
            }
        }

    });
    Log.d(TAG, "Main UI code is running!");




}

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

}

Clara Stancu
Clara Stancu
5,687 Points

I tried adding android:usesCleartextTraffic="true" to my AndroidManifest.xml and it worked

Thank you Clara! Lifesaver!