Open GenweiWu opened 2 years ago
public interface ImageService {
@DELETE("api/v1/attachment")
Call<BaseResponse> delete(@Body DeleteModel deleteModel);
}
https://stackoverflow.com/q/41509195
@HTTP(method = "DELETE", path = "api/v1/attachment", hasBody = true) Call<ResponseBody> getData(@Body DeleteModel deleteModel);
// proxyHost类似a.b.com不要加上http://a.b.com
java.net.Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
OkHttpClient client = new OkHttpClient.Builder().proxy(proxy).build();
Retrofit.Builder builder = new Retrofit.Builder().client(client);
Retrofit retrofit = builder.build();
@Part
要搭配Multipart使用MultipartBody.Part
对应的@Part
不能有name ,改到MultipartBody.Part.createFormData
这里添加@Multipart @POST("user/updateprofile") Observable<ResponseBody> updateProfile(@Part("user_id") RequestBody id, @Part("full_name") RequestBody fullName, @Part MultipartBody.Part image, @Part("other") RequestBody other);
//pass it like this File file = new File("/storage/emulated/0/Download/Corrections 6.jpg"); RequestBody requestFile = RequestBody.create(MultipartBody.FORM, file);
// MultipartBody.Part is used to send also the actual file name MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
// add another part within the multipart request RequestBody fullName = RequestBody.create(MultipartBody.FORM, "Your Name");
service.updateProfile(id, fullName, body, other);
遇到一个post请求,308重定向了;如何在代码中进行兼容处理?
OkHttpClient client = new OkHttpClient.Builder()
.followRedirects(true)
.build();
分析后发现,只支持get或head方法的重定向
https://github.com/square/okhttp/issues/936#issuecomment-266430151
public Response post(String url, String content) throws IOException {
RequestBody body = RequestBody.create(PROTOCOL, content);
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
Request request = requestBuilder.build();
Response response = this.client.newCall(request).execute();
if(response.code() == 307) {
String location = response.header("Location");
return post(location, content);
}
return response;
}
常见的4种添加header方式
针对单个接口添加header
针对所有请求添加header,但是希望针对url选择是否添加对应header