首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >URLConnection出错

URLConnection出错
EN

Stack Overflow用户
提问于 2013-02-14 15:35:57
回答 1查看 7.6K关注 0票数 1

我只是想了解一下JacksonJson库。为此,我尝试将JSON数据从Places API转换为一个字符串。

我的密钥是有效的(我在浏览器和另一个应用程序中进行了测试),但我收到了错误。代码如下:

代码语言:javascript
复制
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的输出:

代码语言:javascript
复制
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 >>

我的载货单里有上网权限。我不知道为什么这不能工作,或者这些错误是什么。

EN

回答 1

Stack Overflow用户

发布于 2013-02-14 17:43:10

这不是访问URL的正确方法。将其参数传递给url只是为了将字节写入输出流,然后请求url。

代码语言:javascript
复制
   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的正确方法。

代码语言:javascript
复制
  url=new URL("https://maps.googleapis.com/maps/api/place/textsearch/json");

然后将所有参数放到params Map中;

代码语言:javascript
复制
        Map<String, String> params = new HashMap<String, String>();
            params.put("query","Cloud");
            params.put("types", "foods");....like this put all

然后建造身体..。

代码语言:javascript
复制
    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,然后发出请求并写入字节。

代码语言:javascript
复制
               byte[] bytes = body.getBytes();
               OutputStream out = conn.getOutputStream();
               out.write(bytes);
               out.close();
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14869904

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档