我正在构建一个需要在其POST正文请求中包含数组的API调用的应用。我正在使用OkHttp 2.6来请求API。
Postman中的请求如下所示:
为了达到这个目的,我尝试了几种写RequestBody
的方法,
第一种方法
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart("app_token", MY_TOKEN)
.addFormDataPart("user_id", 377)
.addFormDataPart("group_id", String.valueOf(groupId))
.addFormDataPart("key_id", deals.toString())
.addFormDataPart("img", filename + ".jpg", fileBody)
.build();
第二种方法
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart("app_token", MY_TOKEN)
.addFormDataPart("user_id", 377)
.addFormDataPart("group_id", String.valueOf(groupId))
.addFormDataPart("key_id[0]", "33")
.addFormDataPart("key_id[1]", "34")
.addFormDataPart("img[0]", filename + ".jpg", fileBody)
.build();
第三种方法,我发现here。
MediaType json = MediaType.parse("application/json; charset=utf-8");
Map<String, Integer> params = new HashMap<>();
for (int i = 0; i < deals.size(); i++) {
params.put("key_id["+i+"]", Integer.valueOf(deals.get(i).getUid()));
}
JSONObject parameter = new JSONObject(params);
RequestBody theBody = RequestBody.create(json, parameter.toString());
RequestBody requestBodyyy = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart("app_token", MY_TOKEN)
.addFormDataPart("user_id", 377)
.addFormDataPart("group_id", String.valueOf(groupId))
.addPart(theBody)
.addFormDataPart("img[0]", filename + ".jpg", fileBody)
.build();
但是,他们都没有工作。
这样做的正确方法是什么?我还需要数组大小是动态的,因为我没有传递静态数据。
答案 0 :(得分:2)
MultipartBuilder multipartBuilder = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart("token", MY_TOKEN)
.addFormDataPart("user_id", user_id)
.addFormDataPart("group_id", group_id)
// .addFormDataPart("img[0]", filename + ".jpg", RequestBody.create(MediaType.parse("image/png"), file));
for (int i = 0; i < keys.size(); i++) {
multipartBuilder.addFormDataPart("key_id[" + i + "]", keys.get(i));
}
for (int i = 0; i < files.size(); i++) {
filename = Utils.getSimpleTimestamp();
multipartBuilder.addFormDataPart("img[" + i + "]", filename + ".jpg", RequestBody.create(MediaType.parse("image/png"), files.get(i)));
}
RequestBody requestBody = multipartBuilder.build();
Request request = new Request.Builder()
.url(API_URL)
.post(requestBody)
.addHeader("content-type", "multipart/form-data;")
.build();
OkHttpClient okHttpClient = new OkHttpClient();
答案 1 :(得分:1)
我正在使用Retrofit和Gson
private class AddImageAsyncTask extends AsyncTask<Void, Imageuploadpojo, Imageuploadpojo> {
ApiCall a;
String path1,path2;
private AddImageAsyncTask(View vs, String path1,String path2) {
this.vs = vs;
this.path1 = path1;
this.path2 = path2;
a = retrofit2.create(ApiCall.class);
}
@Override
protected Imageuploadpojo doInBackground(Void... params) {
Map<String, RequestBody> typedfile = new HashMap<String, RequestBody>();
File f = new File(path1);
File f2 = new File(path2);
if (f.exists()) {
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), f);
String filename = f.getName();
typedfile.put("fileToUpload" + "\"; filename=\"" + filename, requestFile);
// typedfile.put("img[0]" + count, requestFile);
}
if (f2.exists()) {
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), f2);
String filename = f.getName();
typedfile.put("fileToUpload" + "\"; filename=\"" + filename, requestFile);
// typedfile.put("img[1]" + count, requestFile);
}
// asynchronous
// Create a call instance for looking up Retrofit contributors.
Call<Imageuploadpojo> call = a.ImageCall(typedfile);
// Fetch and print a list of the contributors to the library.
Imageuploadpojo c = null;
try {
c = call.execute().body();
} catch (IOException e) {
e.printStackTrace();
}
return c;
}
@Override
protected void onPostExecute(Imageuploadpojo c) {
super.onPostExecute(c);
}
}
看到这个你就可以了解。或者在改造中试试
代码已更新:
//Create Hashmap:
Map<String, RequestBody> typedfile = new HashMap<String, RequestBody>();
//Add your file path
File f2 = new File(path2);
//Check the file is exist or not
if (f2.exists()) {
//Then Create RequestBody add file in second perameter
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), f2);
typedfile.put("img[1]" + "\"; filename=\"" + filename, requestFile);
//"img[1]" is parameter