我怎么弄清楚如何调试这个?的HttpResponse

时间:2012-10-15 22:14:28

标签: java android json debugging google-places-api

我正在尝试从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

3 个答案:

答案 0 :(得分:1)

您可能需要检查SSL设置。您正在呼叫https:// url

答案 1 :(得分:1)

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

编辑(参见公告):

我的LogCat(没有过滤器)总是以正确的方式显示堆栈跟踪,但经过一些搜索后,似乎并非总是如此: See this SO question.

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

Log.e("My APP", "exception", e);

答案 2 :(得分:1)

我在eclipse中运行了你的代码,运行得很好,给了我一个有效的回复。我认为这可能与SSL有关。您可以通过-Djavax.net.debug=all

启用SSL调试

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

相关问题