拦截器没搞懂就先搞这个法子暂时先写到请求里面,不然api接口啥都用不了,其实也并不是特别麻烦,后续要改,不考虑使用拦截器的话,可能要使用到sqlite数据库,emmmm,想想还是别这么干
public void postTest(){
OkHttpClient client = new OkHttpClient();
//post请求
FormBody formBody = new FormBody.Builder()
.add("event_id","33")
.build();
Request request = new Request.Builder().url(DecryptionAddress+"app/event/accept").
addHeader("Token","服务器获取的token").post(formBody).build();
client.newCall(request).enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
System.out.println(e.getMessage());
}
public void onResponse(Call call, Response response) throws IOException {
if(response.code() >= 200 && response.code() < 300) {
String result = response.body().string();
System.out.println(result);
}
}
});
}
public void getTest(){
//get请求
OkHttpClient client = new OkHttpClient();
Request request1 = new Request.Builder()
.url(DecryptionAddress+"/app/event/detail?event_id=89")
.addHeader("Token","服务器获取的token")
.build();
client.newCall(request1).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.code() >= 200 && response.code() < 300) {
String result = response.body().string();
System.out.println(result);
}
}
});
}
本文介绍了使用OkHttp进行POST和GET请求的具体实现方式,并展示了如何处理响应结果。包括设置请求头、构建请求体及使用回调处理成功或失败的情况。
&spm=1001.2101.3001.5002&articleId=113115931&d=1&t=3&u=7f37ad14976542c386e6a6fa9952ec59)
5975

被折叠的 条评论
为什么被折叠?



