如何使用改造2将文件和其他参数一起发送

pmb*_*pmb 3 android android-networking retrofit retrofit2

我正在寻找一个例子如何将文件和其他参数一起发送到服务器.

我必须发送服务器JSON

{
    "title": "title",
    "file": "uploaded file instance",
    "location": {
        "lat": 48.8583,
        "lng": 2.29232,
        "place": "Eiffel Tower"
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能创建Retrofit来处理这种情况?

如果file是一个字符串,我知道如何处理它.如果file是File对象,我不知道如何做到这一点.

Abt*_*ian 7

使用gson并为该位置创建模型类.

将以下依赖项添加到您的build.gradle.

compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.google.code.gson:gson:2.5'
Run Code Online (Sandbox Code Playgroud)

创建一个模型来表示位置.

public class Location {

    double lat;
    double lng;
    String location;

    public Location(double lat, double lon, String place) {
        this.lat = lat;
        this.lon = long;
        this.place = place;
    } 

}
Run Code Online (Sandbox Code Playgroud)

如果有效内容字段的变量名称与端点的实际所需名称不匹配,则需要添加注释 @SerializedName([expected name])

例如:

import com.google.gson.annotations.SerializedName;

public class Location {

    @SerializedName("lat")
    double latitude;
    @SerializedName("lng")
    double longitude;
    @SerializedName("place")
    String location;

    public Location(double lat, double lon, String place) {
        latitude = lat;
        longitude = long;
        location = place;
    } 

}
Run Code Online (Sandbox Code Playgroud)

定义api接口.

public interface Api {

    @POST("upload/")
    @Multipart
    Call<ResponseBody> uploadFile(@Part("title") RequestBody title,
                                  @Part MultipartBody.Part imageFile,
                                  @Part("location") Location location
    );

}
Run Code Online (Sandbox Code Playgroud)

创建一个Retrofit实例并调用api.

File file;
// create retrofit instance
Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://baseurl.com/api/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
// create api instance
Api api = retrofit.create(Api.class);
// create call object
Call<ResponseBody> uploadFileCall = api.uploadFile(
        RequestBody.create(MediaType.parse("text/plain"), "title"),
        MultipartBody.Part.createFormData(
            "file",
            file.getName(),
            RequestBody.create(MediaType.parse("image"), file)),
        new Location(48.8583, 2.29232, "Eiffel Tower"));
// sync call
try {
    ResponseBody responseBody = uploadFileCall.execute().body();
} catch (IOException e) {
    e.printStackTrace();
}
// async call
uploadFileCall.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if (response.isSuccessful()) {
            // TODO
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        // TODO
    }
});
Run Code Online (Sandbox Code Playgroud)

MediaType.parse()如果您不使用图像文件,则需要更改呼叫.

您可以类似地创建自定义响应类型对象并替换ResponseBody它以接收反序列化的结果对象.

让我知道这个是否奏效.我没有机会在你的确切场景中进行测试,但我相信这应该有效.我不是百分之百的唯一部分是@Part("location") Location location应该是@Body("location") Location location