将带有附加数据的图片上传到php

时间:2013-01-22 06:18:22

标签: android

我想从手机上传一张照片,其中包含姓名和电子邮件等一些数据。

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

我应该单独做吗?

1 个答案:

答案 0 :(得分: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);