这是我的Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Observable<Object> a = fileRetrofit().create(ApiStores.class).addCase2("123", 1234);
a.subscribeOn(Schedulers.io()).subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
}
});
}
public static Retrofit mFileRetrofit;
public static Retrofit fileRetrofit(){
if (mFileRetrofit == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor());
OkHttpClient okHttpClient = builder.build();
mFileRetrofit = new Retrofit.Builder()
.baseUrl(ApiStores.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
}
return mFileRetrofit;
}
}
这是接口
public interface ApiStores {
//baseUrl
String BASE_URL = "http://123.12.123.123/";
@Multipart
@POST("qwe/asd")
Observable<Object> addCase2(@Part("tag") String tag, @Part("tag2") int tag2);
}
一下是LOG”
request
Sending request http://123.12.123.123/qwe/asd on null
--369f49f5-597d-4aa2-9c5e-86eccda84c88
Content-Disposition: form-data; name="tag"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 5
"123"
--369f49f5-597d-4aa2-9c5e-86eccda84c88
Content-Disposition: form-data; name="tag2"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 4
1234
--369f49f5-597d-4aa2-9c5e-86eccda84c88--
可以看到content-length为5, 即为“123”的长度,两边的双引号也被发送过去了
“@Part(“data”) String des”在Post请求中默认的Content-Type类型是“application/json”,这就说明我们在接口中不能再使用@Part注解了
@Multipart
@POST("userPhoto")
Observable<BaseHttpResult<String>> uploadMultipleTypeFile(@PartMap Map<String, RequestBody> params);
Map<String, RequestBody> bodyMap = new HashMap<>();
bodyMap.put("photo"; filename=""+file.getName(), RequestBody.create(MediaType.parse("image/png"),file));
bodyMap.put("userId", toRequestBody(userId));
bodyMap.put("serialNumber", toRequestBody(serialNumber));
public static RequestBody toRequestBody(String value) {
}