问题
Why do I always get the connection error with fromJSON
? Sometimes it is fine. Most of time it is not.
> # Load json and other packages.
> library(jsonlite)
>
> # Live server.
> server <- 'http://0.0.0.0:3000'
> # Stream 143
> key <- '9p06nngO2pcQM03nIJ71dLXNA1v'
>
> # Retrieve json data from the data platform via the URLs.
> streams <- fromJSON(paste(server, '/output/streams', sep=""), flatten=TRUE)
Result:
Error in open.connection(con, "rb") : Couldn't connect to server
If I try again:
> streams <- fromJSON(paste(server, '/output/streams', sep=""), flatten=TRUE)
Error:
Error in open.connection(con, "rb") : Server returned nothing (no headers, no data)
But the server is fine. It is up and running.
Any ideas how I can fix this?
Or any other better and more reliable package for getting the json data from the server?
回答1:
It seems to me that there is some special character in the public_key
field. For instance:
Gurl<-"http://139.162.208.52:3000/output/stream?public_key=9p06nngO2pcQM03nIJ71dLXNA1v"
Burl<-"http://139.162.208.52:3000/output/stream?public_key=9p06nngO2pcQM03nIJ71dLXNA1v"
#this seems to work
fromJSON(Gurl, flatten=TRUE)
#this doesn't
fromJSON(Burl, flatten=TRUE)
Burl
and Gurl
are only apparently the same:
Gurl==Burl
#[1] FALSE
You can inspect the raw
content of the above urls (with charToRaw
) and check the Encoding
to spot the differences.
回答2:
Eg. Php, basic just get data from server by json_encode What programming languages are u using? For back-end
来源:https://stackoverflow.com/questions/42739594/r-jsonlite-fromjson-always-returns-error-in-open-connection