尝试从url获取响应并使用Ruby解析JSON时,文件结束错误 [英] End of File error when trying to get response from a url and parsing the JSON with Ruby

查看:113
本文介绍了尝试从url获取响应并使用Ruby解析JSON时,文件结束错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到此错误:

Errno::ECONNRESET in SearchController#create

Connection reset by peer

EOFError in SearchController#create

end of file reached

我正在尝试从Google的API获取响应,然后用ruby解析JSON。这是我的代码:

I'm trying to get the response from Google's API and then parse the JSON with ruby. Here's my code:

require 'rubygems'
require 'json'
require 'net/https'

def create
  @search = params[:search][:search]
  base_url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0"
  url = "#{base_url}&q=#{@search}&rsz=8&start=0"
  resp = Net::HTTP.get_response(URI.parse(url))
  data = resp.body

  result = JSON.parse(data)

  if result.has_key? 'Error'
     raise "web service error"
  end
  return result
end

resp = Net :: HTTP.get_response(URI.parse(url))似乎是给我错误的那一行。如何解决这个问题?

and resp = Net::HTTP.get_response(URI.parse(url)) seems to be the line giving me the error. How can I fix this?

这是完整跟踪的第一部分:

Here's the first part of the full trace:

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:135:in `sysread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:126:in `readline'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:2020:in `read_status_line'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:2009:in `read_new'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1050:in `request'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:948:in `request_get'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:380:in `get_response'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:543:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:379:in `get_response'
app/controllers/search_controller.rb:10:in `create'


推荐答案

个人而言,我建议使用 Open :: URI ,除非你需要较低级别的例程的粒度或控制:

Personally, I recommend using Open::URI unless you need the lower-level routine's granularity or control:

require 'open-uri'
require 'json'

def create
  search = params[:search][:search]
  base_url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0"

  stream = open("#{base_url}&q=#{search}&rsz=8&start=0")
  raise 'web service error' if (stream.status.first != '200')

  JSON.parse(stream.read)
end

Open :: URI自动处理重定向更好地为此目的,处理为您设置HTTPs连接。

Open::URI automatically handles redirects and, better yet for this purpose, handles setting up the HTTPs connection for you.

如果要使用Net :: HTTP,这将工作:

If you want to use Net::HTTP this will work:

require 'rubygems'
require 'json'
require 'net/http'

def create(search)
  base_url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0"
  url = "#{base_url}&q=#{search}&rsz=8&start=0"
  uri = URI.parse(url)
  connection = Net::HTTP.new(uri.host, 443)
  connection.use_ssl = true

  resp = connection.request_get(uri.path + '?' + uri.query)

  if resp.code != '200'
     raise "web service error"
  end

  JSON.parse(resp.body) 
end

puts create('ruby')

区别在于我告诉Net :: HTTP使用端口443打开SSL连接,我认为你的代码是失败的。此外,我正在寻找成功状态代码'200',您可能需要检查,然后如果您有重定向,则作出反应。

The difference is that I'm telling Net::HTTP to open a SSL connection using port 443, which is where I think your code is failing. Also, I'm looking for the success status code '200', which you might want to check, and then react if you got a redirect.

这篇关于尝试从url获取响应并使用Ruby解析JSON时,文件结束错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆