//倒依赖
//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.ty.MainActivity">
<TextView
android:id="@+id/txt"
android:text="测试"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/yibu"
android:text="异步get"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/tb"
android:text="同步"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.ty;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private static final int RESPONSE_FLAG = 0x123;
private Button yibu;
private Button tb;
private TextView txt;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case RESPONSE_FLAG:
String str=(String) msg.obj;
txt.setText(str);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yibu =(Button)findViewById(R.id.yibu);
tb =(Button)findViewById(R.id.tb);
txt =(TextView)findViewById(R.id.txt);
//点击事件
setListener();
}
private void setListener() {
yibu.setOnClickListener(this);
tb.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.yibu://异步
OkHttpClient client=new OkHttpClient.Builder().build();
//2Request对象
final Request request = new Request.Builder()
.get()
.url("http://www.wuxirui.com/")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "onFailure: "+e.getMessage() );
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// response.body().string()本质上是读流的操作 读完之后就没有了
final String a = response.body().string();
// Log.e(TAG, "onResponse: "+string );
runOnUiThread(new Runnable() {
@Override
public void run() {
txt.setText(a);//创建主线程更新UI
}
});
}
});
break;
case R.id.tb://同步
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client3 = new OkHttpClient();
Request request3 = new Request.Builder()
.get()
.url("http://www.wuxirui.com/")
.build();
Call call3 = client3.newCall(request3);
try {
//是同步请求
// 默认是在当前线程执行的网络请求
//我们需要创建子线程 然后创建Handler主线程
//发送handler在主线程更新ui界面
Response execute = call3.execute();
String string = execute.body().string();
Message message = handler.obtainMessage();
message.what=RESPONSE_FLAG;
message.obj=string;
message.sendToTarget();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
break;
}
}
}