java - Retrofit2上傳圖片失敗
問題描述
上傳的時候報錯: 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創(chuàng)建一個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>() {@Overridepublic void onResponse(Call<UploadResult> call, Response<UploadResult> response) { if (response.isSuccessful()){Toast.makeText(MainActivity.this, '上傳成功', Toast.LENGTH_SHORT).show();Log.i('圖片上傳:','---------------------上傳成功-----------------------'); }}@Overridepublic 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 accept malformed JSON at line 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();
相關文章:
1. python小白 關于類里面的方法獲取變量失敗的問題2. thinkPHP5中獲取數(shù)據(jù)庫數(shù)據(jù)后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙3. linux運維 - python遠程控制windows如何實現(xiàn)4. Python2中code.co_kwonlyargcount的等效寫法5. javascript - 如何用最快的速度C#或Python開發(fā)一個桌面應用程序來訪問我的網(wǎng)站?6. django - Python error: [Errno 99] Cannot assign requested address7. mysql數(shù)據(jù)庫做關聯(lián)一般用id還是用戶名8. [python2]local variable referenced before assignment問題9. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時間會消失是什么情況?10. python小白,關于函數(shù)問題
