<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.atguigu.okhttpsample.MainActivity">
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="get请求" />
<Button
android:id="@+id/btn_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="post请求" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示请求数据" />
</LinearLayout>
点击事件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_get = (Button) findViewById(R.id.btn_get);
btn_post = (Button) findViewById(R.id.btn_post);
tv_result = (TextView) findViewById(R.id.tv_result);
//设置点击事件
btn_get.setOnClickListener(this);
btn_post.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_get:
getDataFromByGet();
break;
case R.id.btn_post:
getDataFromByPost();
break;
}
}
OKHttp的get请求
private void getDataFromByGet() {
new Thread(){
@Override
public void run() {
super.run();
try {
String resutl = get("http://api.m.mtime.cn/PageSubArea/TrailerList.api");
Message msg = Message.obtain();
msg.what = GET;
msg.obj = resutl;
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
/**
* get请求
* @param url
* @return
* @throws IOException
*/
private String get(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
OKHttp的post请求
private void getDataFromByPost() {
new Thread(){
@Override
public void run() {
super.run();
String resutl = null;
try {
resutl = post("http://api.m.mtime.cn/PageSubArea/TrailerList.api","");
Message msg = Message.obtain();
msg.what = POST;
msg.obj = resutl;
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
/**
* post请求
* @param url
* @param json
* @return
* @throws IOException
*/
private String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
在Handler中显示数据
**
* get请求
*/
private static final int GET = 1;
/**
* post请求
*/
private static final int POST = 2;
private Button btn_get,btn_post;
private TextView tv_result;
private OkHttpClient client = new OkHttpClient();
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case GET://get请求
tv_result.setText(msg.obj.toString());
break;
case POST://post请求
tv_result.setText(msg.obj.toString());
break;
}
}
};