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());