按照下面流程开发时遇到的bug
java.lang.IllegalArgumentException: @PartMap parameters can only be used with multipart encoding. (parameter #9
利用retrofit进行上传图片请求时’
ApiNet中的注解`
@POST("orders-comments")
Observable sendEvaluate(@HeaderMap Map<String,String> headers,@Query("productId"),@QueryMap Map<String,String> map,@PartMap() Map<String, RequestBody> file);
封装的方法
public void sendEvaluate(Subscriber<EvaluateEntity> subscriber,Map requestToken,long productId,long Map<String,String> map,Map<String, RequestBody> partParams){
mApiNet.sendEvaluate(requestToken,productId,shopId,anonymity,description,logistics,attitude,map,partParams)
.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
请求数据时得应用
private void sendPublish() {
String content;
Map partParams;
List list = new ArrayList();
for (int i = 0; i < Bimp.drr.size(); i++) {
String Str = Bimp.drr.get(i)
.substring(Bimp.drr.get(i).lastIndexOf("/") + 1, Bimp.drr.get(i).lastIndexOf("."));
list.add(FileUtils.SDPATH + Str + ".JPEG");
}
if (list.size() > 0) {
partParams = new HashMap<>();
for (int i = 0; i < list.size(); i++) {
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(new File(list.get(i))));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
partParams.put("file\";filename=\"" + i + "#" + options.outWidth + "#" + options.outHeight + ".jpeg",
RequestBody.create(MediaType.parse("multipart/form-data"), new File(list.get(i))));
System.out.println(i + "#" + options.outWidth + "#" + options.outHeight + ".jpeg");
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Toast.makeText(getContext(), "图片为空", Toast.LENGTH_SHORT).show();
return;
}
Map filedParams = new HashMap<>();
if (etPostDesc.getText().toString().isEmpty()) {
} else {
content = etPostDesc.getText().toString().trim();
filedParams.put("context", content);
}
unsubscribe();
//利用封装后的retrofit来请求
Subscriber mSubscriber = new Subscriber() {
@Override public void onCompleted() {
}
@Override public void onError(Throwable e) {
if (isNetCollect) {
stateLayout.showEmpty();
} else {
stateLayout.showError();
}
}
@Override public void onNext(EvaluateEntity EvaluateEntity) {
if (EvaluateEntity.getState() == 0) {
stateLayout.showContent();
if (rlNetStates != null) {
rlNetStates.setVisibility(View.GONE);
}
Toast.makeText(getContext(), "发布成功", Toast.LENGTH_SHORT).show();
Bimp.bmp.clear();
Bimp.drr.clear();
Bimp.max = 0;
}
}
};
HttpMethods.getInstance()
.sendEvaluate(mSubscriber, GetToken.getAccessToken(), productId, filedParams, partParams);
}
上面bug的解决办法
**@Multipart**
@POST("orders-comments")
Observable sendEvaluate(@HeaderMap Map<String,String> headers,@Query("productId") long productId ,@QueryMap Map<String,String> map,@PartMap() Map<String, RequestBody> file);
问题2
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 81
利用封装好的retrofit进行网络请求,接口和参数都没有问题,但就是请求不到数据,如果在onError中debug异常e.printStackTrace();就会看到此bug
这种bug的意思是你按对象解析的,而实际它是一个数组
其实还是因为retrofit内部json解析时出现了问题
接下来看你封装的retrofit
我先前封装的retrofit
mRetrofit = new Retrofit.Builder()
.client(httpClientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BASE_URL)
.build();
解决bug
修改GSON为fastjson
mRetrofit = new Retrofit.Builder()
.client(httpClientBuilder.build())
//.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(FastJsonConverterFactory.create())
.baseUrl(BASE_URL)
.build();