Java 使用okhttp作为数组传递文本和多个图像
我正在使用lib向服务器发送数据。我想在Java 使用okhttp作为数组传递文本和多个图像,java,php,android,arrays,okhttp3,Java,Php,Android,Arrays,Okhttp3,我正在使用lib向服务器发送数据。我想在RequestBody中设置文本和图像。对于使用Okhttp将多个映像上载到服务器,我遵循以下步骤。我已经在我的应用程序中的另一个活动类中实现了这种类型的代码,并且工作正常。我有一个问题,如何在RequestBody中传递数组 我的arrayList格式如下 blockList { block0 { description0 = First block image0 = {image1, image2}
RequestBody
中设置文本和图像。对于使用Okhttp将多个映像上载到服务器,我遵循以下步骤。我已经在我的应用程序中的另一个活动类中实现了这种类型的代码,并且工作正常。我有一个问题,如何在RequestBody
中传递数组
我的arrayList格式如下
blockList
{
block0
{
description0 = First block
image0 = {image1, image2}
video0 = videolink
disp_order0 = 0
block0 = 0
}
block1
{
description1 = second block
image1 = {image1,image2,image2}
video1 = videolink
disp_order1 = 1
block1 = 1
}
.....
}
我的要求:-
现在我想在单个参数中以数组的形式发送多个图像。当我发送第一个块时,参数名称是description0
,image0[]
,video1
,disp\u order
和block0
,并且image0[]
将第一个块图像作为数组包含,其他块图像相同
API运行良好,因为当我在postman中测试时,我会在服务器端接收数据。你可以在下面看到
下面是我的java函数,它在RequestBody中设置数据,并调用在服务器上发送该数据
ProgressDialog pd;
private OkHttpClient client;
private void saveCastBoxOnServer(String castBoxTitle, String selectedCastBoxId, String selectedCategoryId,
String userId, String action, ArrayList<CastBoxBlock> blockList)
{
try
{
client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
ArrayList<CastBoxBlock> blockArrayList = blockList;
int blockSize = blockArrayList.size();
MultipartBody.Builder multipartBuilderNew = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (int i = 0; i < blockSize; i++)
{
String description = blockArrayList.get(i).getBlockDescription();
String descriptionField = "description"+i;
multipartBuilderNew.addFormDataPart(descriptionField, description);
/**This is used for distribution of images and videos. After that set that
* Images and video in multipartBuilder.
**/
CastBoxBlock model = blockArrayList.get(i);
ArrayList<SelectedMediaModel> mediaModels = model.getSelectedMediaModelArrayList();
int mediaModelsSize = mediaModels.size();
String passingVideoUri = "";
String videoUri = "";
for (int j = 0; j < mediaModelsSize; j++)
{
String mediaType = mediaModels.get(j).getMediaType();
if (mediaType.equals(StringKeyConstant.mediaVideo))
{
videoUri = mediaModels.get(j).getMediaPath();
if (passingVideoUri.trim().length()==0){
passingVideoUri = videoUri;
}else{
passingVideoUri = passingVideoUri + "," + videoUri;
}
}
else if (mediaType.equals(StringKeyConstant.mediaImage))
{
String imagePath = mediaModels.get(j).getMediaPath();
File sourceFile = new File(imagePath);
/**Changes whether JPEG or PNG**/
final MediaType MEDIA_TYPE = MediaType.parse(
constant.getFileExt(imagePath).endsWith("png") ? "image/png" : "image/jpeg");
String imageName = System.currentTimeMillis() + j + "_block_img.jpg";
String imageField = "image"+i+"["+j+"]";
multipartBuilderNew.addFormDataPart(imageField,imageName,
RequestBody.create(MEDIA_TYPE, sourceFile));
}
}
/**This is used to set the {@videoUri} block of videos and send to sever**/
String videoField = "video"+i;
multipartBuilderNew.addFormDataPart(videoField, passingVideoUri);
/**This will set the {@display_order} in multipartBuilder**/
String displayOrderField = "disp_order"+i;
String displayOrder = blockArrayList.get(i).getBlockIndex();
multipartBuilderNew.addFormDataPart(displayOrderField, displayOrder);
/**This will set the {@block} value in multipartBuilder**/
String blockField = "block"+i;
String block = ""+i;
multipartBuilderNew.addFormDataPart(blockField, block);
}
pd = new ProgressDialog(activity);
pd.setCancelable(false);
pd.setMessage(getResources().getString(R.string.please_wait));
pd.show();
RequestBody formBody = multipartBuilderNew
.addFormDataPart("cast_title", castBoxTitle)
.addFormDataPart("user_id", userId)
.addFormDataPart("cast_box", selectedCastBoxId)
.addFormDataPart("category", selectedCategoryId)
.addFormDataPart("action", action)
.build();
Request request = new Request.Builder()
.url(ApiUtils.ADD_FETCH_USER_CAST)
.post(formBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "Get Api credential fail."+call.toString());
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException
{
try
{
if (pd != null){
pd.cancel();
pd.dismiss();
pd = null;
}
String castSavedResponse = response.body().string();
Log.i(TAG, "castSavedResponse = " + castSavedResponse);
}
catch (Exception e){
Log.e(TAG, "***Error : onResponse() method");
e.printStackTrace();
}
}
});
}
catch (Exception e){
Log.e(TAG, "***Error : saveCastBoxOnServer()");
e.printStackTrace();
}
}
ProgressDialog-pd;
私人Okhttp客户端;
私有void saveCastBoxOnServer(字符串castBoxTitle、字符串selectedCastBoxId、字符串selectedCategoryId、,
字符串用户ID、字符串操作、ArrayList块列表)
{
尝试
{
client=new-OkHttpClient.Builder()
.retryOnConnectionFailure(真)
.build();
ArrayList blockArrayList=区块列表;
int blockSize=blockArrayList.size();
MultipartBody.Builder multipartBuilderNew=新的MultipartBody.Builder().setType(MultipartBody.FORM);
对于(int i=0;iString imageField = "image"+i+"[]";
String imageField = "image"+i+"["+j+"]";