如何将声音文件从Android上传到使用paperclip gem的Rails网络服务?

时间:2016-03-01 03:10:18

标签: android ruby-on-rails ruby web-services audio

我正在创建一个在RoR和Android应用程序中拥有在线Web服务的应用程序。有一个名为Cry的模型有一个名为" sound"的回形针附件,如下所示:

class Cry < ActiveRecord::Base
  has_many :comments
  has_many :dennies
  has_many :phones, :through => :votes
  validates :body, presence: true

  has_attached_file :sound
  validates_attachment_content_type :sound, :content_type => ["audio/mp3", "audio/3gp"]
end

,控制器是:

class CriesController < ApplicationController
  before_action :set_cry, only: [:show, :edit, :update, :destroy]
  skip_before_filter  :verify_authenticity_token

  # GET /cries
  # GET /cries.json
  def index
    @cries = Cry.all.page(params['page']).order('created_at DESC').per(10)
  end

  # GET /cries/1
  # GET /cries/1.json
  def show
  end

  # GET /cries/new
  def new
    @cry = Cry.new
  end

  # GET /cries/1/edit
  def edit
  end

  # POST /cries
  # POST /cries.json
  def create
    @cry = Cry.new(cry_params)

    #respond_to do |format|
      if @cry.save
        #format.html { redirect_to @cry, notice: 'Cry was successfully created.' }
        #format.json { render :show, status: :created, location: @cry }
        render :json => @cry
      else
        #format.html { render :new }
        #format.json { render json: @cry.errors, status: :unprocessable_entity }
        render :json => { :errors => @cry.errors.full_messages }, :status => 422
      end
    #end
  end

  # PATCH/PUT /cries/1
  # PATCH/PUT /cries/1.json
  def update
    respond_to do |format|
      if @cry.update(cry_params)
        format.html { redirect_to @cry, notice: 'Cry was successfully updated.' }
        format.json { render :show, status: :ok, location: @cry }
      else
        format.html { render :edit }
        format.json { render json: @cry.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cries/1
  # DELETE /cries/1.json
  def destroy
    @cry.destroy
    respond_to do |format|
      format.html { redirect_to cries_url, notice: 'Cry was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_cry
      @cry = Cry.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def cry_params
      params.require(:cry).permit(:body, :category, :sound)
    end
end

我通过浏览器发送声音文件没有问题,但我想从我的Android应用程序发送。我尝试过多部分POST请求,但由于回形针宝石,它无效。

如何向此网络服务发送Android的音频文件?

从浏览器发送时记录(没关系!)

Started POST "/cries" for 10.0.2.2 at 2016-03-01 02:53:13 +0000
Processing by CriesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"dmWpnPwlPmz+e4uxhM7l78Iv7E9Mh/Y8NT8vsEZxXsA8cfKACHETJUKnA5UDABFQUn1LrnzzvKh+Cl12ZVhqug==", "cry"=>{"body"=>":(", "sound"=>#<ActionDispatch::Http::UploadedFile:0x007fd0d9debd70 @tempfile=#<Tempfile:/tmp/RackMultipart20160301-5527-1yxq7k8.mp3>, @original_filename="189.mp3", @content_type="audio/mp3", @headers="Content-Disposition: form-data; name=\"cry[sound]\"; filename=\"189.mp3\"\r\nContent-Type: audio/mp3\r\n">, "category"=>"1"}, "commit"=>"Create Cry"}
Command :: file -b --mime '/tmp/a2557a7b2e94197ff767970b6704169720160301-5527-1769qfr.mp3'
   (0.2ms)  BEGIN
Command :: file -b --mime '/tmp/a2557a7b2e94197ff767970b6704169720160301-5527-1dmpum2.mp3'
  SQL (0.2ms)  INSERT INTO `cries` (`body`, `category`, `sound_file_name`, `sound_content_type`, `sound_file_size`, `sound_updated_at`, `created_at`, `updated_at`) VALUES (':(', 1, '189.mp3', 'audio/mp3', 638256, '2016-03-01 02:53:13', '2016-03-01 02:53:13', '2016-03-01 02:53:13')
   (0.5ms)  COMMIT
Completed 200 OK in 48ms (Views: 0.3ms | ActiveRecord: 0.9ms)

从Android发送时记录(不工作):

Started POST "/cries" for 10.0.2.2 at 2016-03-01 12:35:16 +0000
Processing by CriesController#create as HTML
  Parameters: {"cry"=>{"sound"=>"\u0000\u0000\u0000\u0018ftyp3gp4\u0000\u0000\u0000\u0000isom3gp4\ --- a long sequence of unicode characteres ---", "body"=>"sound", "category"=>"1"}}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)

ArgumentError (invalid byte sequence in UTF-8):
  app/controllers/cries_controller.rb:28:in `create'

0 个答案:

没有答案