2

我正在尝试从 googleplaces api 的结果中创建一个 json 对象。但是一行代码给我带来了很多问题。我的问题是我到底是如何找到问题所在的。我似乎无法捕捉到正确的异常(尽管我是调试的菜鸟)。我传入的网址具有以下值: https://maps.googleapis.com/maps/api/place/search/json?location=34.7,-86.5&radius=16000&types=food&name=mcdonalds&sensor=true&key=(myApiCodeWhichImNOTPostingHere )

^我确实有正确的 apicode,并且该链接在 android 端有效。

这是有问题的方法(突出显示的是导致问题的行)。

        public static JSONObject getTheJSON(String url){
                        JSONObject json=null;
                        try{
                            DefaultHttpClient myHttpClient = new DefaultHttpClient();
                            HttpPost myHttpPost = new HttpPost(url);

            //this line below is giving me problems (jumps streight to the catch Exception)
                            HttpResponse response = myHttpClient.execute(myHttpPost); 
            //this line above is giving me problems(jumps streight to the catch Exception)

                            String data = EntityUtils.toString(response.getEntity());
                            json= new JSONObject(data);
                        //parse the JSONObject
                           } catch (UnsupportedEncodingException e){e.printStackTrace();}
                             catch (ClientProtocolException e){e.printStackTrace();}
                             catch (IOException e){e.printStackTrace();}
                             catch (JSONException e) {e.printStackTrace();}
                             catch (NullPointerException e){ Log.e("My APP", "exception: " + e.getMessage());}

    /* jumps to line below (skips the other catch exceptions)
 the log reads "null" since i use the "getMessage()" so thats not useful*/  

                     catch (Exception e ) { Log.e("My APP", "exception: " + e.getMessage());}
                            return json;  // returning the JSON object

                }

(编辑):这是 logcat。我想我连接到工厂客户端错误

10-16 21:11:30.105: E/My APP(980): exception
10-16 21:11:30.345: E/MapActivity(980): Couldn't get connection factory client
4

3 回答 3

1

您可能需要检查您的 SSL 设置。您正在调用 https:// 网址

于 2012-10-15T22:17:26.447 回答
1

而不是使用e.getMessage(),使用e.printStackTrace()(在Log.e()方法之外,但在catch子句内),这样您就可以追踪真正的问题是什么。如果您没有从中得到更明智的结果,请在此处发布堆栈跟踪。

编辑(见评论):

我的 LogCat(没有过滤器)总是以正确的方式显示堆栈跟踪,但经过一番搜索后,情况似乎并非总是如此: 请参阅这个 SO question。

您应该在该方法中使用三个(!)参数Log.e。例子:

Log.e("My APP", "exception", e);
于 2012-10-15T22:25:02.293 回答
1

我在 Eclipse 中运行了你的代码,它运行良好,给了我一个有效的响应。我认为这可能与 SSL 有关。您可以通过执行启用 SSL 调试-Djavax.net.debug=all

启用 SSL 调试后可以粘贴输出日志吗?

于 2012-10-15T22:49:59.807 回答