我只是想了解一下JacksonJson库。为此,我尝试将JSON数据从Places API转换为一个字符串。
我的密钥是有效的(我在浏览器和另一个应用程序中进行了测试),但我收到了错误。代码如下:
protected Void doInBackground(Void... params)
{
try
{
URL googlePlaces = new URL(
"https://maps.googleapis.com/maps/api/place/textsearch/json?query=Cloud&types=food&language=en&sensor=true&location=33.721314,73.053498&radius=10000&key=<Key>");
URLConnection tc = googlePlaces.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
StringBuffer sb = new StringBuffer();
while ((line = in.readLine()) != null)
{
sb.append(line);
}
Log.d("The Line: ", "" + line);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}这是logcat的输出:
02-14 12:29:07.407: D/libc-netbsd(16792): getaddrinfo: maps.googleapis.com return error = 0x8 >>
02-14 12:29:07.813: D/libc-netbsd(16792): getaddrinfo: maps.googleapis.com get result from proxy >>
02-14 12:29:08.706: D/libc-netbsd(16792): getaddrinfo: maps.googleapis.com return error = 0x8 >>我的载货单里有上网权限。我不知道为什么这不能工作,或者这些错误是什么。
发布于 2013-02-14 17:43:10
这不是访问URL的正确方法。将其参数传递给url只是为了将字节写入输出流,然后请求url。
URL googlePlaces = new URL("https://maps.googleapis.com/maps/api/place/textsearch/json?query=Cloud&types=food&language=en&sensor=true&location=33.721314,73.053498&radius=10000&key=<Key>");这是访问URL的正确方法。
url=new URL("https://maps.googleapis.com/maps/api/place/textsearch/json");然后将所有参数放到params Map中;
Map<String, String> params = new HashMap<String, String>();
params.put("query","Cloud");
params.put("types", "foods");....like this put all然后建造身体..。
StringBuilder bodyBuilder = new StringBuilder();
Iterator<Entry<String, String>> iterator = params.entrySet().iterator();
// constructs the POST body using the parameters
while (iterator.hasNext()) {
Entry<String, String> param = iterator.next();
bodyBuilder.append(param.getKey()).append('=')
.append(param.getValue());
if (iterator.hasNext()) {
bodyBuilder.append('&');
}
}
String body = bodyBuilder.toString();这里的Body包含了所有参数,这些参数不能直接通过URL请求,但是您已经将其写入OutputStream,然后发出请求并写入字节。
byte[] bytes = body.getBytes();
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();https://stackoverflow.com/questions/14869904
复制相似问题