retrofit使用注意事项

1.上传文件

遇到错误:java.lang.IllegalStateException: JSON must start with an array or an object.

Multipart requests are used when @Multipart is present on the method. Parts are declared using the @Part annotation.

@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);
注意:类型必须是  RequestBody 
更多讨论结果https://github.com/square/retrofit/issues/1063

2.设置超时时间(okhttp3)

 OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .build();
区别:

http://www.veritas.com/community/forums/client-read-and-client-connect-time-out  Nicolai和 mph999的回答

简单点就是connectTimeout就是和服务器建立连接的时间;readTimeout是响应请求


3.获得服务器返回的json字符串

注意Bean的类型
@GET("url")
Call<ResponseBody> getHome();
然后在请求的回调方法 onResponse中获取json字符串
String jsonString = new String(response.body().bytes());


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值