Android 将JSON数据和图像发送到服务器

Android 将JSON数据和图像发送到服务器,android,json,image,asynchronous,Android,Json,Image,Asynchronous,我不确定我在努力实现我的目标时是否走了正确的道路 我正在尝试向服务器发送JSON数据和图像。当用户单击按钮时,异步调用将激活,收集包含数据并获取图像路径的JSON容器。以下是我到目前为止的情况: protected String doInBackground(String... data) { gatherEditTextStringValue(); HttpClient httpclient = new DefaultHttpClient();

我不确定我在努力实现我的目标时是否走了正确的道路

我正在尝试向服务器发送JSON数据和图像。当用户单击按钮时,异步调用将激活,收集包含数据并获取图像路径的JSON容器。以下是我到目前为止的情况:

protected String doInBackground(String... data) {
            gatherEditTextStringValue();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);

            try {
                ArrayList<NameValuePair> postVars = new ArrayList<NameValuePair>();
                postVars.add(new BasicNameValuePair("JSON", String.valueOf(JSONMainContainer)));
                httppost.setEntity(new UrlEncodedFormEntity(postVars));

                if (questionTitleImageUri != null) {
                    questionTitleImageFile = new File(getRealPathFromURI(questionTitleImageUri));
                    FileBody bin1 = new FileBody(questionTitleImageFile);
                    MultipartEntity reqEntity = new MultipartEntity();
                    reqEntity.addPart("uploadedfile1", bin1);
                    httppost.setEntity(reqEntity);
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            try {
                HttpResponse response = httpclient.execute(httppost);
                responseBody = EntityUtils.toString(response.getEntity());
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return responseBody;
        }
受保护的字符串doInBackground(字符串…数据){
GathereEditTextStringValue();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
试一试{
ArrayList postVars=新的ArrayList();
add(新的BasicNameValuePair(“JSON”,String.valueOf(JSONMainContainer));
setEntity(新的UrlEncodedFormEntity(postVars));
if(questionTitleImageUri!=null){
questionTitleImageFile=新文件(getRealPathFromURI(questionTitleImageUri));
FileBody bin1=新的FileBody(questionTitleImageFile);
MultipartEntity reqEntity=新的MultipartEntity();
reqEntity.addPart(“uploadedfile1”,bin1);
httppost.setEntity(reqEntity);
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
试一试{
HttpResponse response=httpclient.execute(httppost);
responseBody=EntityUtils.toString(response.getEntity());
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回响应体;
}

现在的问题是,我可以发送一个或两个,而不是两个。有没有一种方法可以将图像数据附加到setEntity,以便其聚合这两种数据?谢谢。

将这两个参数添加到
MultipartEntity
中,而不是调用
setEntity
两次,因为
setEntity
方法的第二次调用将覆盖第一次方法调用设置,请按以下方式执行:

MultipartEntity reqEntity = new MultipartEntity();
// add file
reqEntity.addPart("uploadedfile1", bin1);
// add JSON String 
reqEntity.addPart("JSON", new StringBody(String.valueOf(JSONMainContainer)));
httppost.setEntity(reqEntity);

尝试一个多部分post,将json加载到其中一个parmsyour multipart只包含一项。您需要添加
reqEntity.addPart(“json_数据”,新的UrlEncodedFormEntity(postVars))。此外,您实际上不需要
UrlEncodedFormEntity
。您可以使用
新的StringEntity(String.valueOf(JSONMainContainer))
(在这两种情况下)。作为补充说明,您的服务器可能不希望同时具有多部分和非多部分请求。您可能希望在这两种情况下都使用multipart,并且只添加所需的部分。@njzk2嘿,谢谢您的指针。我试图将JSON数据添加到多部分实体中,但它给了我一个“错误的第二个参数类型”错误。参数需要mime类型的对象。非常感谢!作品谢谢你,你是明星。