[SOLVED] HTTP-Post Request to RESTful Rails web-application

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

[SOLVED] HTTP-Post Request to RESTful Rails web-application

Postby emrah » Tue Nov 18, 2008 3:29 am

Hi,
I am currently working on a project, that uses Android and Rails. My goal is to write a web-application based on Rails (with RESTful service) that makes it possible for users to post information with their Android mobile phones.
But I have a problem: users should have the possibility to add a picture to their post, unfortunately this doesn’t work so far. I have tried out many ways, but without success.

Any hints or ideas how I can solve this problem? That would be great :-). If desired I can post more detailed informations.

Thanks!

ANDROID-CODE:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void httpPostImage() throws Exception {
  2.  
  3.        
  4.  
  5.         HttpClient httpclient = new DefaultHttpClient();
  6.  
  7.         HttpPost httppost = new HttpPost("http://my-ip/information/add_image");
  8.  
  9.        
  10.  
  11.         File file = new File("/data/data/org.emrah/icon.png");
  12.  
  13.         InputStreamEntity reqEntity = new InputStreamEntity(
  14.  
  15.                         new FileInputStream(file), -1);
  16.  
  17.        
  18.  
  19.         reqEntity.setContentType("application/x-www-form-urlencoded");
  20.  
  21.        
  22.  
  23.         Header[] headers = new BasicHeader[2];
  24.  
  25.         headers[0] = new BasicHeader("Accept","application/xml");
  26.  
  27.         headers[1] = new BasicHeader("Content-type", "application/x-www-form-urlencoded");
  28.  
  29.         httppost.addHeader(headers[0]);
  30.  
  31.         httppost.addHeader(headers[1]);
  32.  
  33.        
  34.  
  35.         httppost.setEntity(reqEntity);
  36.  
  37.        
  38.  
  39.         System.out.println("executing request " + httppost.getRequestLine());
  40.  
  41.         HttpResponse response = httpclient.execute(httppost);
  42.  
  43.         HttpEntity resEntity = response.getEntity();
  44.  
  45. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


RAILS-CODE:

Code: Select all
def add_image
  @image = Image.new
  @image.blob = params[:image]
  @information = Information.find 1
   
  respond_to do |format|
    if @image.save
      format.xml { render :xml => @information, :status => :created, :location => @information }
    else
      format.xml  { render :xml => @information, :status => :unprocessable_entity }
    end
  end
end
Last edited by emrah on Fri Jan 02, 2009 12:59 am, edited 2 times in total.
emrah
Freshman
Freshman
 
Posts: 5
Joined: Mon May 12, 2008 7:31 pm
Location: Europe/Austria/Vienna

Top

Solved

Postby emrah » Wed Dec 24, 2008 9:31 pm

It was a Rails Configuration thing.
emrah
Freshman
Freshman
 
Posts: 5
Joined: Mon May 12, 2008 7:31 pm
Location: Europe/Austria/Vienna

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests