php–Android:将文件与其他POST字符串一起上传到页面

php–Android:将文件与其他POST字符串一起上传到页面,第1张

概述我正在尝试将图像上传到PHP页面以及有关图像的其他一些信息,以便PHP页面知道如何处理它.目前,我正在使用它:URLurl=newURL("http://www.tagverse.us/upload.php?authcode="+WEB_ACCESS_CODE+"&description="+description+"&userid="+userId);HttpURLConnectionconnection=

我正在尝试将图像上传到PHP页面以及有关图像的其他一些信息,以便PHP页面知道如何处理它.目前,我正在使用它:

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混合之外,还有更优雅的解决方案吗?我觉得好像这很草率,但据我所知,这是一个完全可以接受的解决方案.如果有更好的方法,我会感谢被指向正确的方向.谢谢!

PS:我很熟悉在正常情况下你会如何通过POST与PHP页面进行交互:

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对象,所以它并不像仅仅合并这两种方法那么简单.

谢谢!

解决方法:

您可以尝试查看我为此类似问题添加的答案:
https://stackoverflow.com/a/9003674/472747

这是代码:

byte[] data = {10,10,10,10,10}; // better get this from a file or memoryhttpClIEnt 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);}
总结

以上是内存溢出为你收集整理的php – Android:将文件与其他POST字符串一起上传到页面全部内容,希望文章能够帮你解决php – Android:将文件与其他POST字符串一起上传到页面所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116889.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存