Retrofit2上传图片失败

Benny
  • 34

上传的时候报错: onFailure=Use JsonReader.setLenient(true) to accept malformed JSON at line 15 column 1 path $
多次修改URL地址和模型都是这个错误

这是Retrofit接口代码
···
public interface ImageUpload {

//上传图片
@Multipart
@POST("/xxzx/a/tpsb/uploadPicture")
Call<UploadResult> uploadMultipleFiles(
        @PartMap Map<String, RequestBody> files
        );

}
···

Retrofit 调用代码
···
public class ServiceGenerator {

private static final String API_BASE_URL= "http://114.115.139.232:8080/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

private static Retrofit.Builder builder =
        new Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addConverterFactory(GsonConverterFactory.create());

public static <S> S createService(Class<S> serviceClass){
    Retrofit retrofit = builder.client(httpClient.build()).build();
    return retrofit.create(serviceClass);
}

}
···

以下是调用方法:
···
private void uploadFiles() {

    if(imagesList.size()==0){
        Toast.makeText(MainActivity.this, "没有选择图片", Toast.LENGTH_SHORT).show();
        return;
    }
    Map<String, RequestBody>files = new HashMap<>();
    //ImageUpload是interface不是class,所以我们是无法直接调用该方法,需要用Retrofit创建一个ImageUpload的代理对象
    final ImageUpload service = ServiceGenerator.createService(ImageUpload.class);
    for (int i = 0;i<imagesList.size();i++){
        File file = new File(imagesList.get(i).path);
        files.put("file" + i + "\"; filename=\"" + file.getName(),
                RequestBody.create(MediaType.parse(imagesList.get(i).mimeType), file));
    }
    Call<UploadResult> call = service.uploadMultipleFiles(files);
    call.enqueue(new Callback<UploadResult>() {
        @Override
        public void onResponse(Call<UploadResult> call, Response<UploadResult> response) {
            if (response.isSuccessful()){
                Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                Log.i("图片上传:","---------------------上传成功-----------------------");
            }
        }

        @Override
        public void onFailure(Call<UploadResult>call, Throwable t) {
            Log.i("wxl", "onFailure=" + t.getMessage());
            Toast.makeText(MainActivity.this,"上传失败", Toast.LENGTH_SHORT).show();
        }
    });
}

···

这是使用Postman post成功的返回json:
{
"failureList": [],
"successNum": 1,
"failureNum": 0
}

clipboard.png

这是报错的内容:
05-06 16:15:43.599 19961-19961/com.example.yuan.imagerecognitionmanager I/wxl: onFailure=Use JsonReader.setLenient(true) to accept malformed JSON at line 15 column 1 path $

javabean:
public class UploadResult<T> {

public int successNum;
public int failureNum;
public ArrayList<String> failureList;

}

回复
阅读 2.9k
1 个回答

http://stackoverflow.com/ques... 可以参考下这里。

主要是这个

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();
宣传栏