【发布时间】:2011-09-30 20:35:12
【问题描述】:
我是 android 的新手,但是查看网络和一些书籍,我制作了一个必须发送图像和另外两个参数的应用程序。我只想制作一个 php 服务脚本来检查数据是否上传/发送正常。
这是我的代码:
public void executeMultipartPost() //throws Exception
{
try {
//convert image to stream, strImage is an image converted to string
InputStream is = new ByteArrayInputStream( this.strImage.getBytes("UTF-8") );
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://192.168.12.12/androidlistener/test.php");
byte[] data = IOUtils.toByteArray(is);
InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data), "uploadedFile");
//These params were filled in before this process
StringBody sPhoneId = new StringBody(strPhoneId);
StringBody sMessage = new StringBody(strMessage);
MultipartEntity multipartContent = new MultipartEntity();
multipartContent.addPart("uploadedFile", isb);
multipartContent.addPart("phoneid", sPhoneId);
multipartContent.addPart("msg", sMessage);
postRequest.setEntity(multipartContent);
HttpResponse response = httpClient.execute(postRequest);
response.getEntity().getContent().close();
} catch (Throwable e)
{
// Manejar error
}
}
所以我需要帮助来制作 php 脚本。提前谢谢你。
【问题讨论】:
标签: php android http upload multipart