2

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
Goku
  • 1,565
  • 1
  • 29
  • 62
  • you've not mentioned if you're getting any errors/exceptions and if so, the specific details of the same. – asgs Oct 15 '12 at 22:28

3 Answers3

1

You might want to check your SSL setup. You are calling a https:// url

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
1

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);
Community
  • 1
  • 1
MarchingHome
  • 1,184
  • 9
  • 15
  • This is more of a comment than an answer. – asgs Oct 15 '12 at 22:26
  • 1
    The question is how to debug his code. Not what the problem in the code itself is. I think it is an answer to his question. – MarchingHome Oct 15 '12 at 22:27
  • 1
    catch (Exception e ) { Log.e("My APP", "exception: " + e.getMessage()); e.printStackTrace();} so you are saying like that? now my question is how do i find the error message? i am 100% sure that this line is being executed but i have never understood how to find the message in logcat – Goku Oct 16 '12 at 14:41
  • Ah. 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](http://stackoverflow.com/questions/8217500/how-to-print-stacktrace-for-an-exception-android). You should use three(!) arguments with the `Log.e` method. Example: `Log.e("My APP", "exception", e);` Sorry to have misinformed you before. – MarchingHome Oct 16 '12 at 18:55
1

I ran your code in eclipse and it ran fine giving me a valid response. I think this is probably SSL related. You could enable SSL debugging by doing -Djavax.net.debug=all

Can you paste the output log after enabling SSL debugging?

javarebel
  • 160
  • 1
  • 9
  • sorry but im really new to this. can you explain how to do -Djavax.net.debug=all ? im just not sure where to put that... – Goku Oct 16 '12 at 14:27
  • -Djavax.net.debug=all is a java virtual machine argument. If you're using Eclipse as your ide, Run-->Run Configurations-->Arguments. Hope this helps. – javarebel Oct 16 '12 at 17:23
  • can you look at the screenshot i posted? i go to Run-->RunConfigurations but i dont see "Arguments" anywhere. If this was just a normal java class i would know where to find it but i cant find it in android. thanks alot, Adam – Goku Oct 16 '12 at 18:40
  • sorry, i'm not familiar with android development so I'm not sure either. – javarebel Oct 16 '12 at 19:46