多部分上传不适用于 android +导轨

发布于 2024-11-30 00:30:44 字数 1165 浏览 2 评论 0原文

我想通过 Android 应用程序将文件 + 字符串上传到我的 Rails 应用程序。但在 Rails 中使用分段上传时,我总是会遇到奇怪的错误。

我在客户端使用 Apache HttpClient 4.1.1 + httpMime Lib 4.1.1,在服务器端使用 Rails 3.0.10。

CountingMultipartEntity mpEntity = new CountingMultipartEntity(this);
File file = new File(getRealPathFromURI(Uri.parse(local_uri)));
ContentBody cbFile = new FileBody(file, "image/jpeg");

mpEntity.addPart("artifact[binary_payload]", cbFile);
mpEntity.addPart("artifact[description]", new StringBody("test", Charset.forName("UTF-8")));

HttpPost post = new HttpPost(url);
post.setEntity(mpEntity);

HttpResponse response = mHttpClient.execute(post);

这将是 Rails 中的错误行:

Error during failsafe response: ActionView::Template::Error
  /Users/magu/.rvm/gems/ruby-1.9.2-p136@ssp/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
  /Users/magu/.rvm/gems/ruby-1.9.2-p136@ssp/gems/actionpack-3.0.10/lib/action_dispatch/http/parameters.rb:10:in `parameters'
...
  /Users/magu/.rvm/gems/ruby-1.9.2-p136@ssp/
09:51:55 NoMethodError (undefined method `rewind' for "test":String):

我无法找到任何解决方案 - 有人有提示吗?

谢谢, 马丁

i want to upload a file + String to my Rails app via an Android App. But I'll always get a weird error, when using the multipart upload in rails.

I'm using Apache HttpClient 4.1.1 + httpMime Lib 4.1.1 on Client Side and Rails 3.0.10 on Serverside.

CountingMultipartEntity mpEntity = new CountingMultipartEntity(this);
File file = new File(getRealPathFromURI(Uri.parse(local_uri)));
ContentBody cbFile = new FileBody(file, "image/jpeg");

mpEntity.addPart("artifact[binary_payload]", cbFile);
mpEntity.addPart("artifact[description]", new StringBody("test", Charset.forName("UTF-8")));

HttpPost post = new HttpPost(url);
post.setEntity(mpEntity);

HttpResponse response = mHttpClient.execute(post);

this would be the error line in rails:

Error during failsafe response: ActionView::Template::Error
  /Users/magu/.rvm/gems/ruby-1.9.2-p136@ssp/gems/activesupport-3.0.10/lib/active_support/whiny_nil.rb:48:in `method_missing'
  /Users/magu/.rvm/gems/ruby-1.9.2-p136@ssp/gems/actionpack-3.0.10/lib/action_dispatch/http/parameters.rb:10:in `parameters'
...
  /Users/magu/.rvm/gems/ruby-1.9.2-p136@ssp/
09:51:55 NoMethodError (undefined method `rewind' for "test":String):

I cannout find any solution for this - anyone have a hint?

thanks,
Martin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2024-12-07 00:30:44

像这样创建您的多部分实体:

MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );

它会起作用。

Create your Multipart entity like this:

MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );

And it's going to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文