AndroidStudioOkHttpClient使⽤教程详解本次来记录下OkHttpClient的使⽤,OkHttpClient是⽤来完成android 客户端对服务端请求的⼯具。
⾸先记住,使⽤⽹络的时候⼀定要加⼊权限,加⼊到l中
<uses-permission android:name="android.permission.INTERNET" />
在初次使⽤的时候会出现报错。cannot resolve symbol OkHttpClient
这⾥需要引⼊
implementation 'com.squareup.okhttp3:okhttp:3.0.1'
然后刷新下项⽬就可以了。
代码:
stclient;
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(){
//tTimeoutMillis();
}
/**
* 发送⼀个表单请求
* @throws Exception
*/
public void SendForm() throws Exception {
RequestBody formBody = new FormBody.Builder()
.add("search", "Jurassic Park")
.build();
Request request = new Request.Builder()
.url("/w/index.php")
.post(formBody)
.build();
Response response = wCall(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";android学习教程
Request request = new Request.Builder()
.
url("api.github/markdown/raw")
.ate(MEDIA_TYPE_MARKDOWN, postBody))
.build();
Response response = wCall(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("123.207.70.54:8080/SpringMvc/hello")
.build();
Response response = wCall(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("123.207.70.54:8080/SpringMvc/hello")
.
build();
Response response = wCall(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("123.207.70.54:8080/SpringMvc/hello")//请求接⼝。如果需要传参拼接到接⼝后⾯。 .build();//创建Request 对象
Response response = null;
response = wCall(request).execute();//得到Response 对象
if (response.isSuccessful()) {
Log.d("kwwl","de()=="+de());
Log.d("kwwl","ssage()=="+ssage());
Log.d("kwwl","res=="+response.body());
//此时的代码执⾏在⼦线程,修改UI的操作请使⽤handler跳转到UI线程。
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
返回信息:
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论