使用 Android SDK 发布多部分请求
我正在尝试做一些我认为相对简单的事情:使用 Android SDK 将图像上传到服务器。我发现了很多示例代码:
http://linklens.blogspot.com/2009/06/android-multipart-upload.html
但这都不适合我。我一直遇到的困惑是发出多部分请求真正需要什么。对于 Android 进行分段上传(带有图像)的最简单方法是什么?
任何帮助或建议将不胜感激!
I'm trying to do something I thought would be relatively simple: Upload an image to a server with the Android SDK. I'm found a lot of example code:
http://linklens.blogspot.com/2009/06/android-multipart-upload.html
But neither work for me. The confusion I keep running into is what is really needed to make a multipart request. What is the simplest way to have a multipart upload (with an image) for Android?
Any help or advice would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
2014 年 4 月 29 日更新:
我的答案现在有点旧了,我猜你更想使用某种高级库,例如 改造。
基于这篇博客,我想出了以下解决方案:
http://blog.tropicalnuclearstrike.com/2010/01/using-multipartentity-in -android-applications/
您必须下载其他库才能运行
MultipartEntity
!1)从http://james.apache.org/download.cgi#Apache_Mime4J 并将 apache-mime4j-0.6.1.jar 添加到您的项目中。
2) 从 http://hc.apache.org/downloads.cgi 下载 httpcomponents-client-4.1-bin.zip并将 httpclient-4.1.jar、httpcore-4.1.jar 和 httpmime-4.1.jar 添加到您的项目中。
3) 使用下面的示例代码。
Update April 29th 2014:
My answer is kind of old by now and I guess you rather want to use some kind of high level library such as Retrofit.
Based on this blog I came up with the following solution:
http://blog.tacticalnuclearstrike.com/2010/01/using-multipartentity-in-android-applications/
You will have to download additional libraries to get
MultipartEntity
running!1) Download httpcomponents-client-4.1.zip from http://james.apache.org/download.cgi#Apache_Mime4J and add apache-mime4j-0.6.1.jar to your project.
2) Download httpcomponents-client-4.1-bin.zip from http://hc.apache.org/downloads.cgi and add httpclient-4.1.jar, httpcore-4.1.jar and httpmime-4.1.jar to your project.
3) Use the example code below.
由于
MultiPartEntity
已弃用。所以这是新的方法!您只需从 Apache 站点下载httpcore.jar(latest)
和httpmime.jar(latest)
即可。As
MultiPartEntity
is deprecated. So here is the new way to do it! And you only needhttpcore.jar(latest)
andhttpmime.jar(latest)
download them from Apache site.这是轻量级解决方案,无需外部 HTTPCore 和此类库即可为我工作。我面临 64K 方法的问题,因此没有办法避免使用 HTTPCore 库
使用
PHP 代码来接受上传
Here is the LIGHT WEIGHTED solution which worked for me with no external HTTPCore and such libs. I was facing issue of 64K methods so have no option left to avoid HTTPCore libraries
USAGE
PHP Code to accept upload
更简单、更轻量 (32k) 以及更多性能:
Android 异步 Http 客户端库: http:// Loopj.com/android-async-http/
实现:
如何使用 Volley 在 Android 中发送“multipart/form-data”POST
More easy, light (32k), and many more performance:
Android Asynchronous Http Client library: http://loopj.com/android-async-http/
Implementation:
How to send a “multipart/form-data” POST in Android with Volley
试试这个:
Try this:
我强烈推荐Loopj。
我已经成功地使用它一次上传多个文件,包括不同的 mime 类型。只需执行以下操作:
对于大型或多个文件,您可能必须增加超时量,否则使用默认超时,这可能太短:
请参阅此链接以获取 Loopj 的完整说明以及如何使用它,这是迄今为止最简单的异步我遇到过的http库:
http://loopj.com/android-async-http/
http://loopj.com/android-async -http/doc/com/loopj/android/http/AsyncHttpClient.html
I highly recommend Loopj.
I have successfully used it to upload multiple files at once, including different mime types. Simply do this:
For large or many files you might have to increase the timeout amount else the default timeout is used which might be too short:
Please see this links for a full description of loopj and how to use it, by far the easiest async http library I have come across:
http://loopj.com/android-async-http/
http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html
然后拨打下面的电话
Then call below
您可以使用 GentleRequest,这是一个用于发出 http 请求的轻量级库(免责声明:我是作者):
请随意查看:https://github.com/Iprogrammerr/Gentle-Request
You can you use GentleRequest, which is lightweight library for making http requests(DISCLAIMER: I am the author):
Feel free to check it out: https://github.com/Iprogrammerr/Gentle-Request
删除所有 httpclient、httpmime 依赖项并添加此依赖项
compile 'commons-httpclient:commons-httpclient:3.1'
。此依赖项已内置在 MultipartRequestEntity 中,以便您可以轻松地将一个或多个文件上传到服务器您还可以添加请求和响应超时
Remove all your httpclient, httpmime dependency and add this dependency
compile 'commons-httpclient:commons-httpclient:3.1'
. This dependency has built in MultipartRequestEntity so that you can easily upload one or more files to the serverYou can also add the request and response timeout
我可以推荐 Ion 库,它使用 3 个依赖项,您可以在这两个站点找到所有三个 jar 文件:
https://github.com/koush/ion#jars(ion 和 androidasync)
https://code.google.com/p/google-gson/downloads/list (gson)
这将异步运行,一旦收到响应,回调将在 UI 线程中执行
我强烈建议您访问 https://github.com/koush/ion
欲了解更多信息
I can recomend Ion library it use 3 dependences and you can find all three jar files at these two sites:
https://github.com/koush/ion#jars (ion and androidasync)
https://code.google.com/p/google-gson/downloads/list (gson)
this will run async and the callback will be executed in the UI thread once a response is received
I strongly recomned that you go to the https://github.com/koush/ion
for futher information
如果您使用 AOSP 库
Volley
,这里有一个简单的方法。如下扩展类
Request
-您可以创建并添加请求,例如 -
Here is a Simple approach if you are using the AOSP library
Volley
.Extend the class
Request<T>
as follows-You can create and add a request like-
对于后人来说,我没有看到提到 okhttp。
相关帖子。
基本上你建立了使用 MultipartBody.Builder 的 body,然后将其发布到请求中。
科特林中的示例:
For posterity, I didn't see okhttp mentioned.
Related post.
Basically you build up the body using a MultipartBody.Builder, and then post this in a request.
Example in kotlin: