代码如下所示:
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);
/* 输出结果为:
<!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>意见反馈<
// /a> 京ICP证030173号 <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这个栈的地址指向了response.body().string()所在的堆的地址
huidiao.onSuccess(responsedata);//这一步将responsedata传到了onSuccess方法中,相当于将MainActivity中的onSuccess方法中参数s指向了responsedata所指向的堆的地址
} catch (IOException e) {
huidiao.onFailure(e);
e.printStackTrace();
}
}
}).start();
}
}
在huidiao.java文件中
public interface huidiao {
void onSuccess(String s);
void onFailure(Exception e);
}
再来一个例子,在okhttp中的接口回调,可以进行二者的对比
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);
}
}