如何将图像和其他数据从Android上传到JSON Web服务

如何将图像和其他数据从Android上传到JSON Web服务,android,json,image,web-services,image-uploading,Android,Json,Image,Web Services,Image Uploading,我需要从Android上传一个图像以及其他数据到JSON web服务。问题是我找不到一种方法一次完成这一切。 以下是我用于发出请求的代码: public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) throws JSONException { try { if (method == "POST") {

我需要从Android上传一个图像以及其他数据到JSON web服务。问题是我找不到一种方法一次完成这一切。 以下是我用于发出请求的代码:

public JSONObject makeHttpRequest(String url, String method, 
        List<NameValuePair> params) throws JSONException {
    try {
        if (method == "POST") {     
            HttpPost httpPost = new HttpPost(url);

            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } else if (method == "GET") {
            //...
        }

    } catch (Exception e) {
        //...
    }
    //...
    // Parse response and return json object
    return null;
}
当我使用mutlipart实体时,我不知道如何设置params对象,只有图像。问题是我需要一次上传包括图像在内的所有数据

有什么解决办法吗?
提前感谢。

尝试创建一个多零件实体,并将其添加为键值对,如下所示:

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

//for strings
entity.addPart("first_name", new StringBody(nameValuePairs.get(0).getValue()));
entity.addPart("last_name", new StringBody(nameValuePairs.get(1).getValue()));

//for image
entity.addPart("image", new FileBody(new File (nameValuePairs.get(2).getValue())));
那就做吧

httpPost.setEntity(entity);
在执行之前

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

//for strings
entity.addPart("first_name", new StringBody(nameValuePairs.get(0).getValue()));
entity.addPart("last_name", new StringBody(nameValuePairs.get(1).getValue()));

//for image
entity.addPart("image", new FileBody(new File (nameValuePairs.get(2).getValue())));
httpPost.setEntity(entity);