使用okhttp时没有返回json数据
2016-07-13 15:05
459 查看
今天踩过的大坑........
这是一段使用okhttp进行post请求的代码
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormEncodingBuilder()
.add("key", value)
.build();
Request request = new Request.Builder()
.url(urlstr)
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
final String res = response.body().string();
new Thread(new Runnable() {
@Override
public void run() {
Log.d("response", res);
}
}).start();
}
});运行结果返回的是com.squareup.okhttp.internal.http.RealResponseBody@52858c28这段字符串,一脸懵逼,本来应该是返回一段json字符串的。
解决方法:
Replace
with
改了之后运行一下就出来了:
这是一段使用okhttp进行post请求的代码
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormEncodingBuilder()
.add("key", value)
.build();
Request request = new Request.Builder()
.url(urlstr)
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
final String res = response.body().string();
new Thread(new Runnable() {
@Override
public void run() {
Log.d("response", res);
}
}).start();
}
});运行结果返回的是com.squareup.okhttp.internal.http.RealResponseBody@52858c28这段字符串,一脸懵逼,本来应该是返回一段json字符串的。
解决方法:
Replace
String json = response.body().toString();
with
String json = response.body().string();
改了之后运行一下就出来了:
{"code":1,"msg":"成功","result":"201607121644019558"}
相关文章推荐
- 关于直播,所有的技术细节都在这里了
- TCP客户、服务端程序示例
- TCP网络库:Acceptor、TcpServer、TcpConnection
- 动态注册监听网络变化
- (MyStudy:一)三级缓存:首先是内存-文件(外存)-网络三级缓存机制。
- 使用Android的OkHttp包实现基于HTTP协议的文件上传下载
- 【转】2012年6月26 – PPS网络电视PHP工程师最新面试题
- angularJS 发起$http.post和$http.get请求
- Http进行网络通信
- HTTP请求报文格式
- 用PHP操作http中Etag、lastModified和Expires标签
- 格式工厂下载地址http://www.33lc.com/soft/1958.html
- Ubuntu下移植Tcpdump到Android平台
- Learning Deconvolution Network for Semantic Segmentation
- 《Deep Learning》译文 第六章 深度前馈网络 从异或函数说起
- 使用HttpSessionListener接口监听Session的创建和失效
- HTTPS
- 如何创建物化视图-摘抄自网络
- 深度神经网络实现图像理解的原理
- HTTP Status 404 - /chp-adapter-web/ 问题解决