安卓:上传文件到一个网页与其他POST字符串 [英] Android: Uploading a file to a page along with other POST strings
问题描述
我试图将图像上传到PHP页面以及有关使PHP页面知道如何处理它的图像其他一些信息。目前,我就要用它使用这样的:
I am trying to upload an image to a PHP page along with some other info about the image so the PHP page knows what to do with it. Currently, I am getting away with it using this:
URL url = new URL("http://www.tagverse.us/upload.php?authcode="+WEB_ACCESS_CODE+"&description="+description+"&userid="+userId);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStream os = connection.getOutputStream();
InputStream is = mContext.getContentResolver().openInputStream(uri);
BufferedInputStream bis = new BufferedInputStream(is);
int totalBytes = bis.available();
for(int i = 0; i < totalBytes; i++) {
os.write(bis.read());
}
os.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String serverResponse = "";
String response = "";
while((response = reader.readLine()) != null) {
serverResponse = serverResponse + response;
}
reader.close();
bis.close();
有一个更优雅的解决这个除了有一个GET / POST混合动力?我觉得如果这是草率的,但就我所知这是一个完全可以接受的解决办法。如果有这样做的更好的方法,我倒是AP preciate被指出了正确的方向。谢谢!
Is there a more elegant solution to this besides having a GET/POST hybrid? I feel as if this is sloppy, but for all I know it is a perfectly acceptable solution. If there is a better way of doing this, I'd appreciate being pointed in the right direction. Thanks!
PS:我所熟悉的,你将如何,在正常情况下,通过发布一个PHP页面交互:
PS: I am familiar with how you would, under normal conditions, interact with a PHP page via POST:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.tagverse.us/login.php");
try {
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("authcode", WEB_ACCESS_CODE));
pairs.add(new BasicNameValuePair("username", username));
pairs.add(new BasicNameValuePair("password", password));
post.setEntity(new UrlEncodedFormEntity(pairs));
client.execute(post);
}
从本质上讲就是我想要做的就是结合这两种方法,但因为我有HttpURLConnection类对象,而不是HttpPost对象的工作,它不是简单,只是合并两个。
Essentially what I'd like to do is combine these two methods, but because I work with an HttpURLConnection object rather than a HttpPost object, it isn't as simple as just merging the two.
感谢您!
推荐答案
您可以尝试看看我说的这个类似问题的答案: http://stackoverflow.com/a/9003674/472747
You can try an take a look at the answer I added for this similar question: http://stackoverflow.com/a/9003674/472747
下面是code:
byte[] data = {10,10,10,10,10}; // better get this from a file or memory
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("server url");
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", bab);
FormBodyPart bodyPart=new FormBodyPart("formVariableName", new StringBody("formValiableValue"));
reqEntity.addPart(bodyPart);
bodyPart=new FormBodyPart("formVariableName2", new StringBody("formValiableValue2"));
reqEntity.addPart(bodyPart);
bodyPart=new FormBodyPart("formVariableName3", new StringBody("formValiableValue3"));
reqEntity.addPart(bodyPart);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while((line = in.readLine()) != null) {
System.out.println(line);
}
这篇关于安卓:上传文件到一个网页与其他POST字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!