java - Retrofit2上傳圖片失敗
怪我咯
怪我咯 2017-05-16 13:26:56
0
1
743

上傳的時候報錯: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
}

#這是錯誤的內容:
05-06 16:15:43.599 19961-19961/com.example.yuan.imagerecognitionmanager I/wxl: onFailure=Use JsonReader.setLenient(true) to cept mal mal 15 column 1 path $

javabean:
public class UploadResult<T> {

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

}

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(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();
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板