代碼如下所示:
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HttpUtil.getResponse(new huidiao() {
@Override
public void onSuccess(final String s) {
Log.i("xinxi",s);
/* 輸出結(jié)果為:
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta h
ttp-equiv=content-type content=text/htm
l;charset=utf-8><meta http-equiv=X-UA-Compda class=mnav>貼吧</a> <noscript> <a href=h
> </p> <p id=cp>©2017 Baidu&n.......bsp;<a href=http://www.baidu.com/duty/
// >使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見(jiàn)反饋<
// /a> 京ICP證030173號(hào) <img src=//www.baidu.com/img/gs.gif> </p> </div> </div>
// </div> </body> </html>*/
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(s);
}
});
}
@Override
public void onFailure(Exception e) {
}
});
}
});
}
}
在HttpUtil.java文件中
public class HttpUtil {
public static void getResponse(final huidiao huidiao){
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.baidu.com")
.build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
final String responsedata = response.body().string();//responsedata這個(gè)棧的地址指向了response.body().string()所在的堆的地址
huidiao.onSuccess(responsedata);//這一步將responsedata傳到了onSuccess方法中佛南,相當(dāng)于將MainActivity中的onSuccess方法中參數(shù)s指向了responsedata所指向的堆的地址
} catch (IOException e) {
huidiao.onFailure(e);
e.printStackTrace();
}
}
}).start();
}
}
在huidiao.java文件中
public interface huidiao {
void onSuccess(String s);
void onFailure(Exception e);
}
再來(lái)一個(gè)例子荧呐,在okhttp中的接口回調(diào),可以進(jìn)行二者的對(duì)比
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HttpUtil.getResponse(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
textView.setText(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
});
}
}
在HttpUtil.java文件中
public class HttpUtil {
public static void getResponse(Callback callback){
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.baidu.com")
.build();
okHttpClient.newCall(request).enqueue(callback);
}
}