0

我想从手机上传一张带有姓名和电子邮件等数据的照片。

在 Android 设备上,我知道如何上传照片,也知道如何在手机和服务器之间发送数据,但是您如何同时进行这两项操作呢?

我应该分开做吗?

4

1 回答 1

0

在您的情况下,您应该使用 MultipartEntity 类,

            MultipartEntity reqEntity = new MultipartEntity();
            reqEntity.addPart("name", new StringBody(name));
            reqEntity.addPart("email", new StringBody(email));

            if(imagePath.trim().length() != 0) {
                reqEntity.addPart("profilePic", new FileBody(new File(imagePath))); 
            }

            HttpClient hc = new DefaultHttpClient();
            HttpPost postMethod = new HttpPost(urlString);
            HttpEntity resEntity;
            HttpResponse response = null;
            postMethod.setEntity(reqEntity);
            response = hc.execute(postMethod);
            resEntity = response.getEntity();
            response_str = EntityUtils.toString(resEntity);
于 2013-01-22T06:38:51.517 回答