上传单个图像似乎对改造2没有问题.
但是,我无法弄清楚如何同时上传2张图片.
如果遵循文档:http: //square.github.io/retrofit/2.x/retrofit/retrofit2/http/PartMap.html
File file = new File(path, "theimage");
File file2 = new File(path2, "theimage");
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
RequestBody requestBody2 = RequestBody.create(MediaType.parse("image/png"), file2);
Map<String, RequestBody> params = new HashMap<>();
params.put("image2", requestBody2 );
Call<ResponseBody> call = service.upload(requestBody, params);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
Log.v("Upload", "success");
}
Run Code Online (Sandbox Code Playgroud)
接口:
public interface FileUploadService {
@Multipart
@POST("/upload")
Call<ResponseBody> upload(
//@Part("image_logo\"; filename=\"image.png\" ") RequestBody file,
@Part("file") RequestBody file,
@PartMap Map<String, RequestBody> params
// @Part("description") String …Run Code Online (Sandbox Code Playgroud) 我以前使用过gson自动转换为pojo的。
但现在我正在尝试使用改造将api结果转换为对象。
只要json已命名对象数组,就没有问题:
{
items:[
{"name":"foo"},
{"name":"bar"}
]
}
Run Code Online (Sandbox Code Playgroud)
public class AnItem {
String name;
}
public class MyItems {
List<AnItem> items;
}
public interface MyApi {
@GET("/itemlist")
Call<MyItems> loadItems();
}
public boolean onOptionsItemSelected(MenuItem item) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://someurl.com/api")
.addConverterFactory(GsonConverterFactory.create())
.build();
MyApi myApi = retrofit.create(MyApi.class);
Call<MyApi> call = myApi.loadItems();
call.enqueue(this);
return true;
}
@Override
public void onResponse(Response<MyItems> response, Retrofit retrofit) {
ArrayAdapter<AnItem> adapter = (ArrayAdapter<AnItem>) getListAdapter();
adapter.clear();
adapter.addAll(response.body().items);
}
Run Code Online (Sandbox Code Playgroud)
这将自动工作。
但是如果未命名json数组怎么办?
[
{"name":"foo"},
{"name":"bar"}
] …Run Code Online (Sandbox Code Playgroud)