由网友(清酒暖风)分享简介:我需要一些数据上传到PHP服务器。我可以做后与参数:I need to upload some data to PHP server. I can do post with parameters: String url = "http://yourserver";HttpClient httpClient =...
我需要一些数据上传到PHP服务器。 我可以做后与参数:
I need to upload some data to PHP server. I can do post with parameters:
String url = "http://yourserver";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
nameValuePairs.add(new BasicNameValuePair("user", "username"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpClient.execute(httpPost);
}
我也可以上传一个文件:
I am also able to upload a file:
String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory(),
"yourfile");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
}
但我怎么可以把它放在一起呢?我需要上传的图片及参数的一个职位。 谢谢
But how can I put it together? I need to upload an image and parameters in one post. Thanks
推荐答案
您必须使用多部分HTTP POST,就像HTML表单。这可以用一个额外的库来完成。 看到帖子的发送使用HTTP POST 图像一个完整的例子。
You must use a multipart http post, like in HTML forms. This can be done with an extra library. See the post Sending images using Http Post for a complete example.
相关推荐
最新文章