在MainActivity中的子线程不能操作UI 所以我总结几种比较简单实用的方法来实现在子线程中对Ui进行修改的方法
方法:
1.handler的使用
2.imgeView.post()或者textView.post()方法
3.MainActivity.this.runOnUiThread()方法
废话不多说,看代码:
package demo.liuchen.com.okhttp;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private TextView text;
private ImageView image;
private String url = "http://bz.budejie.com/?typeid=2&ver=3.4.3&no_cry=1&client=android&c=search&a=hot&location=1";
private String imgurl = "http://t2.27270.com/uploads/tu/201606/32/k5xnewfzvz0.jpg";
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
String Ibody = (String) msg.obj;
text.setText(Ibody);
}
};
private Handler handler1 = new Handler(){
@Override
public void handleMessage(Message msg) {
byte [] data = (byte[]) msg.obj;
image.setImageBitmap(BitmapFactory.decodeByteArray(data,0,data.length));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.tv);
image = (ImageView) findViewById(R.id.imageview);
}
public void downLoadText(View view) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("TAG","onFailure"+e.getMessage().toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//注意 此处response只能获取一次,第二次获取会为null
final String body = response.body().string();
//在子线程操作UI两种都可以
//handler也可以
// text.post(new Runnable() {
// @Override
// public void run() {
// text.setText(body);
// }
// });
// MainActivity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// text.setText(body);
// }
// });
Message msg = new Message();
msg.obj= body;
handler.sendMessage(msg);
}
});
}
public void downLoadImage(View view) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(imgurl)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("TAG","onFailure"+e.getMessage().toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// byte[] data= new byte[1024];
// data = response.body().bytes();
final byte[] data = response.body().bytes();
// image.post(new Runnable() {
// @Override
// public void run() {
// image.setImageBitmap(BitmapFactory.decodeByteArray(data,0,data.length));
// }
// });
// MainActivity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// image.setImageBitmap(BitmapFactory.decodeByteArray(data,0,data.length));
// }
// });
Message msg1 = new Message();
msg1.obj = data;
handler1.sendMessage(msg1);
}
});
}
}