Rumah > Soal Jawab > teks badan
Ralat dilaporkan semasa memuat naik: onFailure=Gunakan JsonReader.setLenient(true) untuk menerima JSON yang cacat pada baris 15 lajur 1 laluan $
Ralat ini berlaku walaupun selepas mengubah suai alamat URL dan model berkali-kali
Ini ialah kod antara muka Retrofit
···
Muat Naik Imej antara muka awam {
//上传图片
@Multipart
@POST("/xxzx/a/tpsb/uploadPicture")
Call<UploadResult> uploadMultipleFiles(
@PartMap Map<String, RequestBody> files
);
}
···
Retrofit kod panggilan
···
ServisGenerator kelas awam {
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);
}
}
···
Berikut ialah kaedah panggilan:
···
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();
}
});
}
···
Ini adalah json yang berjaya dikembalikan oleh posmen:
{
"failureList": [],
"successNum": 1,
"failureNum": 0
}
Ini ialah kandungan laporan ralat:
05-06 16:15:43.599 19961-19961/com.example.yuan.imagerecognitionmanager I/wxl: onFailure=Gunakan JsonReader.setLenient(true) untuk menerima line15 JSON yang cacat lajur 1 laluan $
javabean:
UploadResult kelas awam<T>
public int successNum;
public int failureNum;
public ArrayList<String> failureList;
}某草草2017-05-16 13:28:56
http://stackoverflow.com/ques... Anda boleh merujuknya di sini.
Terutamanya ini
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();