如何在android多部分图像上传中传递JSONArray

时间:2017-07-18 10:05:20

标签: android multipart

我想在多部分图片上传

中传递arrayList值“tags”和“category”
HttpClient httpClient = new DefaultHttpClient();
        QuestionPicArray = params[0];
        HttpPost httpPost = new HttpPost(UrlLinks.base_url + "/api/forum/create");
        httpPost.addHeader(Constants.AUTHORIZATION, sp.getToken(context));
        httpPost.addHeader(Constants.contentType, "image/jpeg");

        org.apache.http.entity.mime.MultipartEntity entity = new org.apache.http.entity.mime.MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        for (int i = 0; i < QuestionPicArray.size(); i++) {
            File sourceFile = new File(QuestionPicArray.get(i).toString());

            entity.addPart("file[" + i + "]", new FileBody(sourceFile));

            entity.addPart("title", new StringBody( title.toString()));
            entity.addPart("desc", new StringBody( desc.toString()));
            entity.addPart("askedTo",new StringBody( askedTo.toString()));
            if (!askedTo.equalsIgnoreCase("open"))
            entity.addPart("anaonFlag",new StringBody(anaonFlag.toString()));


            for(int j=0;j<tags.size();j++)
            {
                //  Array
                entity.addPart("tags",new StringBody(String.valueOf(tags.get(j)), Charset.forName("EUC-KR")));

            }
            for(int j=0;j<category.size();j++)
            {
                //  Array
                entity.addPart("category",new StringBody(String.valueOf(category.get(j)), Charset.forName("EUC-KR")));

            }


        }

        httpPost.setEntity(entity);

        // Making server call
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity r_entity = response.getEntity();
        getData = EntityUtils.toString(r_entity);

我得到的结果是“结果错误:未找到”

请建议我如何在MultipartEntity的addPart()中传递arrayList的值

1 个答案:

答案 0 :(得分:0)

我很高兴回答你的问题。

if(result.contains("{")||result.contains("[")){
    // pass JSON data
} else {
    //  Dealing with errors
}

我希望这可以帮到你。