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) Networking Making an HTTP GET Request with OkHttp

Kevin Chen
Kevin Chen
2,561 Points

Is there any way that I can send the data from OKHTTP thread to main thread? So I don't have to call runOnUIThread()?

I guess enqueue() method will start a new thread to handle everything with http request. And I'm wondering is there any way that I can send the data back to main thread so I can update UI or change anything in UI thread directly without using runOnUIThread() method? Seems like I don't have the reference to the thread that handles http...

I'm trying to write the code in the following way:

mJSONData = getJsonData(latitude, longitude);

mWeather = getWeather(mJSONData);

updateDispay(mWeather);

you have two options, inside your call back you can do

  MyActivity.this.runOnUiThread(new Runnable() {
         @Override
        public void run() {
           //Handle UI here                                  
        }
     });

or you can do it with a handler

have an insatance var private Handler handler = new Handler(Looper.getMainLooper());

then in your callback

handler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "email and Password were not correct", Toast.LENGTH_SHORT).show();
        }
    });