Java-最佳方法:从Android到GAE的HTTP POST(多部分)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java-最佳方法:从Android到GAE的HTTP POST(多部分),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2796字,纯文字阅读大概需要4分钟。
内容图文
![Java-最佳方法:从Android到GAE的HTTP POST(多部分)](/upload/InfoBanner/zyjiaocheng/671/7297014204884f3b9afc2abc3b93d502.jpg)
我想从Android相机上捕获图像,然后将其发送到Google App Engine,后者会将图像存储在Blob存储区中.听起来很简单,我可以进行到GAE的多部分POST,但是存储到Blob存储区需要Servlet返回HTTP重定向(302).因此,我需要一个可以在执行HTTP POST之后进行重定向的连接.这是我希望的代码:
public static String sendPhoto(String myUrl, byte[] imageData) {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String pathToOurFile = "/data/file_to_send.jpg";
String urlServer = myUrl;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
// Please follow redirects
HttpURLConnection.setFollowRedirects(true);
BufferedReader rd = null;
StringBuilder sb = null;
String line = null;
try {
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// I really want you to follow redirects
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(10000); // 10 sec
connection.setReadTimeout(10000); // 10 sec
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
connection.connect();
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream
.writeBytes("Content-Disposition: form-data; name="
+ "\"file1\";filename=\""
+ pathToOurFile + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.write(imageData);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
outputStream.flush();
outputStream.close();
rd = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
sb = new StringBuilder();
while ((line = rd.readLine()) != null) {
sb.append(line + '\n');
}
Log.i("Response: ", sb.toString());
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.i("Response: serverResponseCode:",
String.valueOf(serverResponseCode));
Log.i("Response: serverResponseMessage:", serverResponseMessage);
} catch (Exception ex) {
ex.printStackTrace();
}
}
尽我所能,此代码不会跟随重定向.输入的蒸汽始终为空,响应始终为302.尽管图像很好地上传.如果有人可以告诉我我可以做些什么使它跟随重定向以便我可以阅读响应,我将不胜感激.
另外,如果有更好的方法,我很想听听.我知道那里有像Apache HTTP Client这样的库,但是它需要太多的依赖项,对于一个简单的任务来说似乎太过膨胀了.
谢谢
解决方法:
几个月前,我试图做完全一样的事情!
基本上,不要使用HttpURLConnection.相反,请使用org.apache.http包中的HttpClient和HttpGet,它们是Android SDK的一部分.
不幸的是,我没有提供示例的源代码,但希望它将为您指明正确的方向.
内容总结
以上是互联网集市为您收集整理的Java-最佳方法:从Android到GAE的HTTP POST(多部分)全部内容,希望文章能够帮你解决Java-最佳方法:从Android到GAE的HTTP POST(多部分)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。