从上传的Andr​​oid图像(与Android异步HTTP客户端)到Rails服务器(使用回形针) [英] Uploading an image from Android (with Android Asynchronous Http Client) to rails server (with paperclip)

查看:127
本文介绍了从上传的Andr​​oid图像(与Android异步HTTP客户端)到Rails服务器(使用回形针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传通过HTTP POST方法的图像文件从Android设备到Rails服务器。 但是,发布图片不能正常工作。更具体地,交参数(包括图像文件)似乎没有被正确地发送。

I'm trying to upload an image file via http post method from android device to rails server. But posting an image is not working. More specifically, the post parameter (including image file) doesn't seem to be sent correctly.

我使用的是Android的异步HTTP客户端(http://loopj.com/android-async-http/)发布的图像从机器人,和code张贴形象是这样的。

I'm using Android Asynchronous Http Client (http://loopj.com/android-async-http/) to post an image from android, and the code for posting image is like this.

public static void postImage(){
    RequestParams params = new RequestParams();
    params.put("picture[name]","MyPictureName");
    params.put("picture[image]",File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/CameraApp/test.jpg"));
    AsyncHttpClient client = new AsyncHttpClient();
    client.post("http://x.x.x.x:3000/pictures/", params, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            Log.w("async", "success!!!!");
        }                                                                                                                                                                     
    }); 
}   

对于轨道回形针的应用程序,我只是用脚手架和生成的模型命名为照片,并补充就可以了回形针附加文件。模型和控制器(接收请求)就像下面的。

As for rails paperclip application, I simply used scaffold and generated model named "pictures" and added paperclip attached file on it. Model and controller (which receives the request) is like below.

模式

class Picture < ActiveRecord::Base                                                                                                                                            
  attr_accessible :name, :image
  has_attached_file :image,
                    :styles => { :original => "480x480>", :thumbnail => "100x100>" },
                    :path => ':rails_root/public/system/pictures/image/:id_partition/:style_:filename'
end

控制器

# POST /pictures                                                                                                                                                              
# POST /pictures.json
def create
  @picture = Picture.new(params[:picture])

  respond_to do |format|
    if @picture.save
      format.html { redirect_to @picture, notice: 'Picture was successfully created.' }
      format.json { render json: @picture, status: :created, location: @picture }
    else
      format.html { render action: "new" }
      format.json { render json: @picture.errors, status: :unprocessable_entity }
    end
  end
end

当接收到请求,轨道服务器控制台说像以下

When receiving the request, "rails server" console says something like following

Started POST "/pictures/" for y.y.y.y at 2012-09-03 18:23:00 +0900
Processing by PicturesController#create as HTML
  Parameters: {"picture"=>{"name"=>"PictureName"}}
   (0.1ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "pictures" ("album_id", "created_at", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "name", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["album_id", nil], ["created_at", Mon, 03 Sep 2012 09:23:00 UTC +00:00], ["image_content_type", nil], ["image_file_name", nil], ["image_file_size", nil], ["image_updated_at", nil], ["name", "PictureName"], ["updated_at", Mon, 03 Sep 2012 09:23:00 UTC +00:00], ["user_id", nil]]
[paperclip] Saving attachments.
   (11.0ms)  commit transaction
Redirected to http://x.x.x.x:3000/pictures/10                                                                                                                               
Completed 302 Found in 15ms (ActiveRecord: 11.6ms)

正如你所看到的,只有一个paramater发送和图片[图]这是没有接收原始图像数据的参数。谁能帮我?

As you can see, there's only one paramater sent and "picture[image]" parameter which is raw image data is not received. Can anyone help me?

推荐答案

对不起,那是我的愚蠢的错误。 我应该做的。

Sorry, that was my silly mistake. I should have done

params.put("picture[image]", new File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/CameraApp/test.jpg"));

不是

params.put("picture[image]", File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/CameraApp/test.jpg"));

应该使用新文件没有文件

在使用安卓异步HTTP客户端( http://loopj.com/android-async-http/ ),我们不必在意MultipartEntity。 谢谢谁回答我的问题的所有球员!!!!

When using Android Asynchronous Http Client (http://loopj.com/android-async-http/), we don't have to care about MultipartEntity. Thank you to all guys who answered my question!!!!

这篇关于从上传的Andr​​oid图像(与Android异步HTTP客户端)到Rails服务器(使用回形针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆