Error reported when uploading: onFailure=Use JsonReader.setLenient(true) to accept malformed JSON at line 15 column 1 path $
This error occurs when the URL address and model are modified multiple times
This is the Retrofit interface code
···
public interface ImageUpload {
//上传图片
@Multipart
@POST("/xxzx/a/tpsb/uploadPicture")
Call<UploadResult> uploadMultipleFiles(
@PartMap Map<String, RequestBody> files
);
}
···
Retrofit calling code
···
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);
}
}
···
The following is the calling method:
···
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();
}
});
}
···
This is the json returned successfully using Postman post:
{
"failureList": [],
"successNum": 1,
"failureNum": 0
}
This is the error message:
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;
}
某草草2017-05-16 13:28:56
http://stackoverflow.com/ques... You can refer to it here.
Mainly this
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();