Saturday, 15 February 2014

post android to rails 4 -



post android to rails 4 -

i've been trying create multipart post upload photo, can't figure out how solve this. firstly, server log:

started post "/ads" 192.168.0.17 @ 2014-06-19 12:11:07 -0300 processing adscontroller#create html parameters: {"image"=>#<actiondispatch::http::uploadedfile:0x000001010beee0 @tempfile=#<tempfile:/var/folders/h1/ql7mfgwn2yd9yf5_t11fv4mm0000gn/t/rackmultipart20140619-605-1yfs6sb>, @original_filename="img_20140619_121043.jpg", @content_type="application/octet-stream", @headers="content-disposition: form-data; name=\"image\"; filename=\"img_20140619_121043.jpg\"\r\ncontent-type: application/octet-stream\r\ncontent-transfer-encoding: binary\r\n">, "category_id"=>"1", "title"=>"jaba", "price"=>"01", "description"=>"nsna"} completed 400 bad request in 1ms actioncontroller::parametermissing (param missing or value empty: ad): app/controllers/ads_controller.rb:82:in `ad_params' app/controllers/ads_controller.rb:37:in `create'

i don't know why advertisement param empty! android side code:

filebody bin = new filebody(mediafile); multipartentitybuilder advertisement = multipartentitybuilder.create(); ad.addpart("image", bin); ad.addtextbody("category_id", adcategory); ad.addtextbody("title", adtitle); ad.addtextbody("price", adprice); ad.addtextbody("description", addescription); httppostrequest.setentity(ad.build()); httpresponse httpresponse = null; httpresponse = httpclient.execute(httppostrequest);

and create method on server side:

def create @ad = ad.new(ad_params) respond_to |format| if @ad.save format.html { redirect_to @ad, notice: 'ad created.' } format.json { render :show, status: :created, location: @ad } else format.html { render :new } format.json { render json: @ad.errors, status: :unprocessable_entity } end end end def ad_params params.require(:ad).permit(:title, :price, :location, :description, :image, :category_id) end

**edit*****

i think advertisement param should json post, don't know how create json including image file. i've done far:

jsonobject json = new jsonobject(); json.put("category_id", adcategory); json.put("title", adtitle); json.put("price", adprice); json.put("description", addescription); json.put("image", teste); stringentity adparams = new stringentity(json.tostring()); adparams.setcontenttype(new basicheader(http.content_type, "application/json")); httppostrequest.setentity(adparams);

android ruby-on-rails post image-uploading

No comments:

Post a Comment