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) Working with JSON Introducing JSONObject

Yiu Ming Lai
Yiu Ming Lai
5,861 Points

get error from Callback() I am not sure why i am getting red line under the Callback, and 2 @Override

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

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

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();

private CurrentWeather mCurrentWeather;

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

    if (isNetworkAvailable()) {
        String apiKey = "6ebc486bd593e1dc64f71ba283d74d31";
        double latitude = 37.8267;
        double longitude = -122.423;
        String forecastUrl = "https://api.forecase.io/forcast/" + apiKey + "/" + latitude + "," + longitude;

        if (isNetworkAvailable()) {

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(forecastUrl).build();

            Call call = client.newCall(request);


            //red line under this Callback.
            call.enqueue(new Callback() {

            //red line under this Override.
            @Override
            public void onFailure(Request request, IOException e) {
            }

             //red line under this Override.
            @Override
            public void onResponse(Response response) throws IOException {
                try {
                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData);
                    if (response.isSuccessful()) {
                        mCurrentWeather = getCurrentDetails(jsonData);
                    } else {
                        alertUserAboutError();
                    }
                }
                catch (IOException e) {
                    Log.e(TAG, "Exception caught: ", e);
                }
                catch (JSONException 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 CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
    JSONObject forecast = new JSONObject(jsonData);
    String timezone = forecast.getString("timezone");
    Log.i(TAG, "From JSON: " + timezone);

    return new CurrentWeather();
}


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

}

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

OkHttp underwent a major revision since the course was made. Part of that was changing the parameters (or signature) of the two callback functions. Ii Looks like you typed in what Ben had on screen, which were for the older version. Because the signatures don't match, you are overriding the method, hence the errors.

I recommend removing onResponse and onFailure then use the Alt+Enter quick-fix on Callback to implement them with the new parameters, and put the same body back into onResponse.

Yiu Ming Lai
Yiu Ming Lai
5,861 Points

I triy, but i still didnt get any data such the location name and the temp also, i am getting this error.

getSlotFromBufferLocked: unknown buffer: 0xa21421c0

Seth Kroger
Seth Kroger
56,413 Points

'unknown buffer' has to do with the display graphics. It doesn't seem to reallyu affect anything and is more in internal message from the emulator, so don't worry about that one. Have you logged the response itself to see if you're getting all the JSON data?