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