从Android向rails服务器发送HTTP请求

时间:2013-01-15 21:58:35

标签: xml ruby-on-rails-3 http-get

我尝试将带有http的android的xml文件发送到rails服务器。

我的android发送代码:

    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());
    }

在rails中我创建了一个监听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

我被迫创建了一个export.html.erb,其中包含以下内容:          

Nothing to see

我的routes.rb包含:

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

但是,如果我现在将一个请求与我的应用程序发送到服务器,我会在我的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)

在此之后,它不可能访问我的服务器,我将在控制台中看到以下内容:

     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'

我的xml-parsing幸运的是,因为我已经通过在方法中复制创建的xml文件来测试它,但希望你能帮助我理解我的问题:)。我已经搜索了好几个小时,但我没有找到帮助我解决问题的东西。

1 个答案:

答案 0 :(得分:1)

幸运的是,我通过朋友的提示解决了这个问题。

我的误解是控制器方法通常会监听导出链接,所以我已经听了两次。

所以解决方案是删除http get:

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

然后我不得不删除xml解析,因为我在哈希

中获取了信息