添加okhttp和oki的依赖或者导入jar包
okhttp官网:https://github.com/square/okhttp
添加联网权限
布局:点击按钮获取数据展示到TextView上
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwie.okhttpdemo.MainActivity">
<Button
android:id="@+id/btn_get_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="get和post请求" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示请求数据" />
</LinearLayout>
主界面
/**
* Okhttp3原生get和post请求获取网络数据
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// 一些配置,可以省略
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
private static final int GET = 1;
private static final int POST = 2;
private Button btn_get_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:
// 获取数据
tv_result.setText((String) msg.obj);
break;
case POST:
// 获取数据
tv_result.setText((String) msg.obj);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_get_post = findViewById(R.id.btn_get_post);
tv_result = findViewById(R.id.tv_result);
// 设置点击事件
btn_get_post.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_get_post: // 使用原生的okhttp请求网络数据,get和post请求
tv_result.setText("");// 请求之前清空
/**
* 两个请求方法二选一,都能获取到数据
*/
// Get请求
// getDataFromGet();// 自动生成方法,Alt+Shift+M
// Post请求
getDataFromPost();
break;
}
}
/**
* 使用get在子线程中请求网络数据
*/
private void getDataFromGet() {
new Thread() {
@Override
public void run() {
super.run();
try {
String result = get("http://api.m.mtime.cn/PageSubArea/TrailerList.api");
Log.e("TAG", result);
Message msg = Message.obtain();
msg.what = GET;
msg.obj = result;
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
/**
* 使用post在子线程中请求网络数据
*/
private void getDataFromPost() {
new Thread() {
@Override
public void run() {
super.run();
try {
String result = post("http://api.m.mtime.cn/PageSubArea/TrailerList.api", "");
Log.e("TAG", result);
Message msg = Message.obtain();
msg.what = POST;
msg.obj = result;
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
/**
* get请求
*
* @param url
* @return
*/
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();
}
/**
* 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();
}
}