I am programmatically navigating to a site that returns application/json format. I can't seem to read the json returned in the HttpURLConnection. I am using Jackson to demarshall the JSON into the java object. The code is:
我以编程方式导航到一个返回应用程序/json格式的站点。我似乎无法读取在HttpURLConnection中返回的json。我正在使用Jackson将JSON放入java对象中。的代码是:
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
geoLocation = (new ObjectMapper()).readValue(sb.toString(), GeoLocation.class);
When I print sb.toString(), I get funny looking characters and unicodes. I should get a well formed String. The resulting exception is:
当我打印sb.toString()时,我得到了有趣的字符和unicodes。我应该得到一个很好的绳子。由此产生的异常:
org.codehaus.jackson.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3f6dadf9; line: 1, column: 2]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1291)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:385)
For example, if my url is:
例如,如果我的url是:
http://api.ipinfodb.com/v3/ip-city/?key=<mykeyhere>&ip=38.111.145.101&format=xml
I get the following inside the Browser window:
我在浏览器窗口中看到以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<statusCode>OK</statusCode>
<statusMessage></statusMessage>
<ipAddress>38.111.145.101</ipAddress>
<countryCode>US</countryCode>
<countryName>UNITED STATES</countryName>
<regionName>CALIFORNIA</regionName>
<cityName>OAKLAND</cityName>
<zipCode>94601</zipCode>
<latitude>37.7993</latitude>
<longitude>-122.24</longitude>
<timeZone>-08:00</timeZone>
</Response>
But when I do
但是当我做
http://api.ipinfodb.com/v3/ip-city/?key=<mykeyhere>&ip=38.111.145.101&format=json
It prompts me to download a file. Once you open the file after it has been downloaded, it contains:
它提示我下载一个文件。一旦你打开文件,它已经被下载,它包含:
{
"statusCode" : "OK",
"statusMessage" : "",
"ipAddress" : "38.111.145.101",
"countryCode" : "US",
"countryName" : "UNITED STATES",
"regionName" : "CALIFORNIA",
"cityName" : "OAKLAND",
"zipCode" : "94601",
"latitude" : "37.7993",
"longitude" : "-122.24",
"timeZone" : "-08:00"
}
I also tried passing the input stream directly to jackson but it failed with the same result.
我还尝试将输入流直接传递给jackson,但结果却失败了。
geoLocation = (new ObjectMapper()).readValue(urlConn.getInputStream(), GeoLocation.class);
Any idea how I can retrieve the JSON from the URLConn in a viewable String format so I can pass it to Jacson?
知道如何从URLConn中检索JSON格式的JSON格式,这样我就可以把它传递给Jacson吗?
Thank you
谢谢你!
1 个解决方案
#1
6
By default, HttpURLConnection
sets the encoding to gzip
. Once I disabled it, I was able to read the String from the stream:
默认情况下,HttpURLConnection将编码设置为gzip。一旦禁用了它,我就可以从流中读取字符串:
urlConn.setRequestProperty("Accept-Encoding", "identity");
#1
6
By default, HttpURLConnection
sets the encoding to gzip
. Once I disabled it, I was able to read the String from the stream:
默认情况下,HttpURLConnection将编码设置为gzip。一旦禁用了它,我就可以从流中读取字符串:
urlConn.setRequestProperty("Accept-Encoding", "identity");