我如何找出如何调试呢? HTT presponse [英] how do i figure out how to debug this? HttpResponse

查看:168
本文介绍了我如何找出如何调试呢? HTT presponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从googleplaces API的结果做一个JSON对象。但是,code的一条线是造成我很多问题。我的问题是如何赫克我觉得这个问题是什么。我似乎无法赶上正确的例外(虽然在即时通讯调试小白)。那我传递在URL中的数值:
<一href=\"https://maps.googleapis.com/maps/api/place/search/json?location=34.7,-86.5&radius=16000&types=food&name=mcdonalds&sensor=true&key=(myApi$c$cWhichImNOTPostingHere\" rel=\"nofollow\">https://maps.googleapis.com/maps/api/place/search/json?location=34.7,-86.5&radius=16000&types=food&name=mcdonalds&sensor=true&key=(myApi$c$cWhichImNOTPostingHere)

^我做了正确的API code和链接的作品的android我们那边。

在这里是有问题的方法(强调是导致问题的行)。

 公共静态的JSONObject getTheJSON(字符串URL){
                        JSONObject的JSON = NULL;
                        尝试{
                            DefaultHttpClient myHttpClient =新DefaultHttpClient();
                            HttpPost myHttpPost =新HttpPost(URL);            //下面这一行是给我的问题(streight跳到catch异常)
                            HTT presponse响应= myHttpClient.execute(myHttpPost);
            //这条线上面给我的问题(streight跳到catch异常)                            字符串数据= EntityUtils.toString(response.getEntity());
                            JSON =新的JSONObject(数据);
                        //解析的JSONObject
                           }赶上(UnsupportedEncodingException五){e.printStackTrace();}
                             赶上(ClientProtocolException E){e.printStackTrace();}
                             赶上(IOException异常五){e.printStackTrace();}
                             赶上(JSONException E){e.printStackTrace();}
                             赶上(NullPointerException异常五){Log.e(我的应用程序,异常:+ e.getMessage());}    / *跳到下面线(跳过其他捕获例外)
 因为我使用的getMessage(),因此,多数民众赞成在日志显示空没有用处* /                     赶上(例外五){Log.e(我的应用程序,异常:+ e.getMessage());}
                            返回JSON; //返回JSON对象                }

(编辑):
这里是logcat的。我认为,即时通讯越来越工厂客户端错误连接

  10-16 21:11:30.105:E /我的APP(980):异常
10-16 21:11:30.345:E / MapActivity(980):无法获取连接工厂客户端


解决方案

而不是使用 e.getMessage(),使用 e.printStackTrace( )(外 Log.e(中)方法,但抓在条款),所以你可以跟踪真正的问题是什么。如果你没有得到任何明智的,请在这里发表的堆栈跟踪。

编辑(见commments):

我的LogCat中(无过滤器)始终显示在一个正确的方式踪迹,但有点搜索后,似乎并不总是这样的:
<一href=\"http://stackoverflow.com/questions/8217500/how-to-print-stacktrace-for-an-exception-android\">See这太问题。

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

  Log.e(我的应用,异常,E);

I'm trying to make a json object from the result of the googleplaces api. But one line of code is causing me alot of problems. My question is how the heck do i find what the issue is. I can't seem to catch the right exception (im a noob at debugging though). The url that i am passing in has the value: https://maps.googleapis.com/maps/api/place/search/json?location=34.7,-86.5&radius=16000&types=food&name=mcdonalds&sensor=true&key=(myApiCodeWhichImNOTPostingHere)

^i do have the correct apicode and the link works ourside of android.

here is the method in question (highlighted is the line that is causing problems).

        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

                }

(Edit): Here is the logcat. i think im getting a connection to factory client error

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

解决方案

Instead of using e.getMessage(), use e.printStackTrace() (outside of the Log.e() method, but within the catch clause) so you can trace what the real problem is. If you do not get any wiser from that, please post the stacktrace here.

Edit (see commments):

My LogCat (without filters) always displays the stacktraces in a correct way, but after a bit of searching it seems that that is not always the case: See this SO question.

You should use three(!) arguments with the Log.e method. Example:

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

这篇关于我如何找出如何调试呢? HTT presponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆