flp
flp

Reputation: 1030

Send an HTTP request from Android to a rails server

I try to send a xml file with http in android to a rails server.

My android send code:

    String link = "http://10.0.2.2:3000/export";

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(link);

    try {
        String xmlFile = createXml(locationList);
        StringEntity entity = new StringEntity(xmlFile, "UTF-8");
        httppost.setEntity(entity);
        httppost.addHeader("Content-Type", "application/xml");
        HttpResponse response = httpclient.execute(httppost);
        Log.d("response reason phrase", ""
                + response.getStatusLine().getReasonPhrase());
        Log.d("response content", ""
                + response.getStatusLine().getStatusCode());
    } catch (ClientProtocolException e) {
        Log.d("exception", e.getMessage());
    } catch (IOException e) {
        Log.d("exception", e.getMessage());
    }

In rails I have created a controller method to listen for the export-link:

def export

begin
  result = Net::HTTP.get(URI.parse("http://localhost:3000/export"))
  MyLog.debug "#{result}"
  xml_doc = Nokogiri::XML(result)


  locations = xml_doc.xpath("//location")

  if locations.count > 0
    training = Training.create!

    locations.each do |xml_location|
      location = Location.new
      location.latitude = xml_location.xpath("latitude").text
      location.longitude = xml_location.xpath("longitude").text
      location.training_id = training.id
      location.save!
    end
  end
    # end
rescue TimeoutError => e
  MyLog.debug "timeout #{e}"
end
end

And I was forced to create a export.html.erb, which contains the following:

Nothing to see

My routes.rb contains this:

   match 'export' => 'Trainings#export', as: "export", :via => [:get, :post]

But if I send now a request with my application to the server, I see the following in my cmd:

    Started POST "/export" for 127.0.0.1 at 2013-01-15 22:47:59 +0100
    Connecting to database specified by database.yml
    Processing by TrainingsController#export as HTML
    Parameters: {"training"=>{"location"=>[{"longitude"=>"60.0", 
    "latitude"=>"60.0"}, {"longitude"=>"59.9999", "latitude"=>"60.0"},
    {"longitude"=>"59.99995", "latitude"=>"59.9999"}]}}
    WARNING: Can't verify CSRF token authenticity
    Rendered trainings/export.html.erb within layouts/application (25.0ms)
    Completed 200 OK in 61633ms (Views: 576.0ms | ActiveRecord: 0.0ms)

After this it's not more possible to access to my server and I will see in the console always the following:

     Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-01-15 22:53:00 +0100
     Served asset /jquery.js - 200 OK (76ms)
     [2013-01-15 22:53:00] ERROR Errno::ECONNABORTED: Eine bestehende Verbindung wurd
     e softwaregesteuertdurch den Hostcomputer abgebrochen.
     C:/Ruby/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:396:in `write'
     C:/Ruby/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:396:in `<<'
     C:/Ruby/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:396:in `_write_data'
     C:/Ruby/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:368:in `send_body_string'
     C:/Ruby/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:249:in `send_body'
     C:/Ruby/Ruby193/lib/ruby/1.9.1/webrick/httpresponse.rb:152:in `send_response'
     C:/Ruby/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:110:in `run'
     C:/Ruby/Ruby193/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

My xml-parsing is fortunately working, because I've tested it with copying a created xml file in the method, but hopefully you can help my to understand my problem :). I've searched many hours, but I didn't something, which helped me to fix my problem.

Upvotes: 1

Views: 597

Answers (1)

flp
flp

Reputation: 1030

Fortunately I fixed the problem through a tip from a friend.

My misunderstanding was that the controller method normally listen to the export link and so I have listend twice.

So the solution was deleting the http get:

      result = Net::HTTP.get(URI.parse("http://localhost:3000/export"))

And then I had to delete the xml parsing, because I got the information in a hash

Upvotes: 1

Related Questions