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 Making Our Code Asynchronous

Need Help with Asynchronous code

I'm using a new version on Android Studios than what he is using in the video. But I did try to keep as much of what he is using and the code the same. This is the error message I receive whenever I change the code to an asynchronous call: E/libprocessgroup: failed to make and chown /acct/uid_10058: Read-only file system 11-06 14:58:25.154 3572-3572/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT? 11-06 14:58:25.155 3572-3572/? I/art: Not late-enabling -Xcheck:jni (already on) 11-06 14:58:25.182 3572-3580/? E/art: Failed sending reply to debugger: Broken pipe 11-06 14:58:25.182 3572-3580/? I/art: Debugger is no longer active 11-06 14:58:25.302 3572-3572/? I/InstantRun: starting instant run server: is main process 11-06 14:58:25.333 3572-3572/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 11-06 14:58:25.481 3572-3572/? D/com.example.shikirrawilliams.myweatherapp.MainActivity: Main UI code is Running! 11-06 14:58:25.487 3572-3591/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                             [ 11-06 14:58:25.494  3572: 3572 D/         ]
                                             HostConnection::get() New Host Connection established 0xa3c2abf0, tid 3572


                                             [ 11-06 14:58:25.530  3572: 3572 W/         ]
                                             Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 

11-06 14:58:25.531 3572-3572/? D/Atlas: Validating map... 11-06 14:58:25.602 3572-3591/? I/OpenGLRenderer: Initialized EGL, version 1.4 11-06 14:58:25.602 3572-3591/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 11-06 14:58:25.606 3572-3591/? D/EGL_emulation: eglCreateContext: 0xae834b80: maj 2 min 0 rcv 2 11-06 14:58:25.608 3572-3591/? D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 11-06 14:58:25.610 3572-3591/? D/OpenGLRenderer: Enabling debug mode 0 11-06 14:58:25.615 3572-3591/? D/EGL_emulation: eglMakeCurrent: 0xae834b80: ver 2 0 11-06 14:58:25.696 3572-3572/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView

This is my code for Main Activity:

public class MainActivity extends AppCompatActivity {

public static  final String TAG = MainActivity.class.getName();

private CurrentWeather currentWeather;

@BindView(R.id.timeLabel) TextView timeLabel;
@BindView(R.id.tempLabel) TextView temperatureLabel;
@BindView(R.id.humidityLabel) TextView humidityValue;
@BindView(R.id.precipLabel) TextView precipValue;
@BindView(R.id.summaryLabel) TextView summaryLabel;
@BindView(R.id.weatherIcon) ImageView weatherIcon;


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

    String apiKey = "0123456789abcdef9876543210fedcba";
    double latitude = 42.3601;
    double longitude = -71.0589;
    String url = "https://api.darksky.net/forecast/" + apiKey + "/" + latitude + "," + longitude;

    if(isNetworkAvailable()) {

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

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    String jsonData = response.body().string();
                    Log.v(TAG, jsonData);
                    if (response.isSuccessful()) {
                        currentWeather = getCurrentDetails(jsonData);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                updateWeather();
                            }
                        });
                    } else {
                        alertUserAboutError();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Exception caught: ", e);

// e.printStackTrace(); } 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 void updateWeather() {

    temperatureLabel.setText(currentWeather.getTemperature() + "");
    timeLabel.setText("At " + currentWeather.getFormattedTime() + " it will be");
    humidityValue.setText(currentWeather.getHumidity() + "");
    precipValue.setText(currentWeather.getChanceOfPrecipiatation() + "%");
    summaryLabel.setText(currentWeather.getSummary());
}

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
    JSONObject forecast = new JSONObject(jsonData);
    String timezone = forecast.getString("timezone");
    Log.i(TAG, "From JSON: " + timezone);

    JSONObject currently = forecast.getJSONObject("currently");

    CurrentWeather currentWeather = new CurrentWeather();
    currentWeather.setHumidity(currently.getDouble("humidity"));
    currentWeather.setTime(currently.getLong("time"));
    currentWeather.setIcon(currently.getString("icon"));
    currentWeather.setChanceOfPrecipiatation(currently.getDouble("precipProbability"));
    currentWeather.setSummary(currently.getString("summary"));
    currentWeather.setTemperature(currently.getDouble("temperature"));
    currentWeather.setTimezone(timezone);

    Log.d(TAG, currentWeather.getFormattedTime());

    return 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_didalog");

}

}