Android Studio OkHttpClient使用教程详解
本次来记录下OkHttpClient的使用,OkHttpClient是用来完成android 客户端对服务端请求的工具。
首先记住,使用网络的时候一定要加入权限,加入到AndroidMainfest.xml中
<uses-permission android:name="android.permission.INTERNET" />
在初次使用的时候会出现报错。cannot resolve symbol OkHttpClient
这里需要引入
implementation 'com.squareup.okhttp3:okhttp:3.0.1'
然后刷新下项目就可以了。
代码:
package com.example.administrator.testclient;
import com.squareup.*;
import java.io.IOException;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class BaseHttpClient {
public static final MediaType MEDIA_TYPE_MARKDOWN
= MediaType.parse("text/x-markdown; charset=utf-8");
// 01. 定义okhttp
private final OkHttpClient client = new OkHttpClient();
public BaseHttpClient(){
//client.connectTimeoutMillis();
}
/**
* 发送一个表单请求
* @throws Exception
*/
public void SendForm() throws Exception {
RequestBody formBody = new FormBody.Builder()
.add("search", "Jurassic Park")
.build();
Request request = new Request.Builder()
.url("https://en.wikipedia.org/w/index.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
/**POST 请求
* 发送一个string请求
* @throws Exception
*/
public void SendPostString() throws Exception {
String postBody = ""
+ "Releases\n"
+ "--------\n"
+ "\n"
+ " * _1.0_ May 6, 2013\n"
+ " * _1.1_ June 15, 2013\n"
+ " * _1.2_ August 11, 2013\n";
Request request = new Request.Builder()
.url("https://api.github.com/markdown/raw")
.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}
/**POST 请求
* 发送一个From请求
* @throws Exception
*/
public void SendPostFrom() throws Exception {
RequestBody body = new FormBody.Builder()
.add("name", "sy")//添加参数体
.add("age", "18")
.build();
Request request = new Request.Builder()
.post(body) //请求参数
.url("http://123.207.70.54:8080/SpringMvc/hello")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
}
/**Get请求
* 发送一个From请求
* @throws Exception
*/
public void SendGetFrom() throws Exception {
Request request = new Request.Builder()
.get() //请求参数
.url("http://123.207.70.54:8080/SpringMvc/hello")
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
}
}
测试发现,上面的用不了,下面放一个测试通过的方法:
public void getDatasyncFactory(){
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
Request request = new Request.Builder()
.url("http://123.207.70.54:8080/SpringMvc/hello")//请求接口。如果需要传参拼接到接口后面。
.build();//创建Request 对象
Response response = null;
response = client.newCall(request).execute();//得到Response 对象
if (response.isSuccessful()) {
Log.d("kwwl","response.code()=="+response.code());
Log.d("kwwl","response.message()=="+response.message());
Log.d("kwwl","res=="+response.body());
//此时的代码执行在子线程,修改UI的操作请使用handler跳转到UI线程。
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
返回信息:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
以上是 Android Studio OkHttpClient使用教程详解 的全部内容, 来源链接: utcz.com/p/243014.html