介绍
Android子线程不能操作UI组件,Google提供runUiOnThread( Runable runable )方法在子线程委托主线程更新UI组件
方法
void runUiOnThread( Runable runable )
参数:
Runable runable:ui主线程
返回值:无
返回值意义:无
作用:在子线程中调用出主线
Thread tht = new Thread(new Runnable() { @Override public void run() { Request request = new Request.Builder().url("http://106.55.50.79:8080/CoolWeather/china").build(); OkHttpClient okHttpClient = new OkHttpClient(); Call call = okHttpClient.newCall(request); String string = null; try { Response response = call.execute(); string = response.body().string(); } catch (IOException e) { e.printStackTrace(); } String finalString = string; runOnUiThread(new Runnable() { @Override public void run() { Log.i("MA", "run: "); txt.setText(finalString); } }); } }); tht.start();