我正在使用Android设备上的google places api。以下是我使用的代码段,如this教程中所述。
URL googlePlaces = new URL("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" +
URLEncoder.encode(params[0], "UTF-8") +
"&types=geocode&language=en&sensor=true&key=" +
getResources().getString(R.string.googleAPIKey));
URLConnection tc = googlePlaces.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
StringBuffer sb = new StringBuffer();
//take Google's legible JSON and turn it into one big string.
while ((line = in.readLine()) != null) {
sb.append(line);
//turn that string into a JSON object
Log.d("json predictions", "before predictions");
JSONObject predictions = new JSONObject(sb.toString());
Log.d("json predictions", predictions.toString());
如图所示的例外
02-07 17:25:38.830: D/dalvikvm(8984): Late-enabling CheckJNI
02-07 17:25:38.971: D/libEGL(8984): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
02-07 17:25:38.971: D/libEGL(8984): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
02-07 17:25:38.979: D/libEGL(8984): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
02-07 17:25:39.049: D/OpenGLRenderer(8984): Enabling debug mode 0
02-07 17:25:50.041: D/gottaGo3(8984): onTextChanged
02-07 17:25:50.041: D/gottaGo(8984): doInBackground
02-07 17:25:50.486: D/dalvikvm(8984): GC_FOR_ALLOC freed 221K, 3% free 9155K/9412K, paused 19ms, total 19ms
02-07 17:25:50.518: D/gottaGo3(8984): onTextChanged
02-07 17:25:50.869: D/json predictions(8984): before predictions
02-07 17:25:50.869: E/YourApp(8984): GetPlaces : doInBackground
02-07 17:25:50.869: E/YourApp(8984): org.json.JSONException: End of input at character 1 of {
02-07 17:25:50.869: E/YourApp(8984): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
02-07 17:25:50.869: E/YourApp(8984): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
02-07 17:25:50.869: E/YourApp(8984): at org.json.JSONTokener.readObject(JSONTokener.java:362)
02-07 17:25:50.869: E/YourApp(8984): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
02-07 17:25:50.869: E/YourApp(8984): at org.json.JSONObject.<init>(JSONObject.java:154)
02-07 17:25:50.869: E/YourApp(8984): at org.json.JSONObject.<init>(JSONObject.java:171)
02-07 17:25:50.869: E/YourApp(8984): at com.test.locationautocomplete.MainActivity$GetPlaces.doInBackground(MainActivity.java:89)
02-07 17:25:50.869: E/YourApp(8984): at com.test.locationautocomplete.MainActivity$GetPlaces.doInBackground(MainActivity.java:1)
02-07 17:25:50.869: E/YourApp(8984): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-07 17:25:50.869: E/YourApp(8984): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-07 17:25:50.869: E/YourApp(8984): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-07 17:25:50.869: E/YourApp(8984): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-07 17:25:50.869: E/YourApp(8984): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-07 17:25:50.869: E/YourApp(8984): at java.lang.Thread.run(Thread.java:841)
02-07 17:25:50.869: D/YourApp(8984): onPostExecute : 0
02-07 17:25:50.869: D/YourApp(8984): onPostExecute : autoCompleteAdapter0
02-07 17:25:50.877: D/gottaGo(8984): doInBackground
02-07 17:25:51.275: D/gottaGo3(8984): onTextChanged
02-07 17:25:51.377: D/json predictions(8984): before predictions
02-07 17:25:51.385: E/YourApp(8984): GetPlaces : doInBackground
02-07 17:25:51.385: E/YourApp(8984): org.json.JSONException: End of input at character 1 of {
02-07 17:25:51.385: E/YourApp(8984): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
02-07 17:25:51.385: E/YourApp(8984): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
02-07 17:25:51.385: E/YourApp(8984): at org.json.JSONTokener.readObject(JSONTokener.java:362)
02-07 17:25:51.385: E/YourApp(8984): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
02-07 17:25:51.385: E/YourApp(8984): at org.json.JSONObject.<init>(JSONObject.java:154)
02-07 17:25:51.385: E/YourApp(8984): at org.json.JSONObject.<init>(JSONObject.java:171)
02-07 17:25:51.385: E/YourApp(8984): at com.test.locationautocomplete.MainActivity$GetPlaces.doInBackground(MainActivity.java:89)
02-07 17:25:51.385: E/YourApp(8984): at com.test.locationautocomplete.MainActivity$GetPlaces.doInBackground(MainActivity.java:1)
02-07 17:25:51.385: E/YourApp(8984): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-07 17:25:51.385: E/YourApp(8984): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-07 17:25:51.385: E/YourApp(8984): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-07 17:25:51.385: E/YourApp(8984): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-07 17:25:51.385: E/YourApp(8984): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-07 17:25:51.385: E/YourApp(8984): at java.lang.Thread.run(Thread.java:841)
02-07 17:25:51.385: D/YourApp(8984): onPostExecute : 0
02-07 17:25:51.385: D/YourApp(8984): onPostExecute : autoCompleteAdapter0
02-07 17:25:51.393: D/gottaGo(8984): doInBackground
02-07 17:25:51.893: D/json predictions(8984): before predictions
02-07 17:25:51.893: E/YourApp(8984): GetPlaces : doInBackground
02-07 17:25:51.893: E/YourApp(8984): org.json.JSONException: End of input at character 1 of {
02-07 17:25:51.893: E/YourApp(8984): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
02-07 17:25:51.893: E/YourApp(8984): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
02-07 17:25:51.893: E/YourApp(8984): at org.json.JSONTokener.readObject(JSONTokener.java:362)
02-07 17:25:51.893: E/YourApp(8984): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
02-07 17:25:51.893: E/YourApp(8984): at org.json.JSONObject.<init>(JSONObject.java:154)
02-07 17:25:51.893: E/YourApp(8984): at org.json.JSONObject.<init>(JSONObject.java:171)
02-07 17:25:51.893: E/YourApp(8984): at com.test.locationautocomplete.MainActivity$GetPlaces.doInBackground(MainActivity.java:89)
02-07 17:25:51.893: E/YourApp(8984): at com.test.locationautocomplete.MainActivity$GetPlaces.doInBackground(MainActivity.java:1)
02-07 17:25:51.893: E/YourApp(8984): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-07 17:25:51.893: E/YourApp(8984): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-07 17:25:51.893: E/YourApp(8984): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-07 17:25:51.893: E/YourApp(8984): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-07 17:25:51.893: E/YourApp(8984): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-07 17:25:51.893: E/YourApp(8984): at java.lang.Thread.run(Thread.java:841)
02-07 17:25:51.893: D/YourApp(8984): onPostExecute : 0
02-07 17:25:51.893: D/YourApp(8984): onPostExecute : autoCompleteAdapter0
02-07 17:25:56.627: D/gottaGo3(8984): onTextChanged
02-07 17:25:56.627: D/gottaGo(8984): doInBackground
02-07 17:25:57.135: D/json predictions(8984): before predictions
02-07 17:25:57.135: E/YourApp(8984): GetPlaces : doInBackground
02-07 17:25:57.135: E/YourApp(8984): org.json.JSONException: End of input at character 1 of {
02-07 17:25:57.135: E/YourApp(8984): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
02-07 17:25:57.135: E/YourApp(8984): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
02-07 17:25:57.135: E/YourApp(8984): at org.json.JSONTokener.readObject(JSONTokener.java:362)
02-07 17:25:57.135: E/YourApp(8984): at org.json.JSONTokener.nextValue(JSONTokener.java:100)
02-07 17:25:57.135: E/YourApp(8984): at org.json.JSONObject.<init>(JSONObject.java:154)
02-07 17:25:57.135: E/YourApp(8984): at org.json.JSONObject.<init>(JSONObject.java:171)
02-07 17:25:57.135: E/YourApp(8984): at com.test.locationautocomplete.MainActivity$GetPlaces.doInBackground(MainActivity.java:89)
02-07 17:25:57.135: E/YourApp(8984): at com.test.locationautocomplete.MainActivity$GetPlaces.doInBackground(MainActivity.java:1)
02-07 17:25:57.135: E/YourApp(8984): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-07 17:25:57.135: E/YourApp(8984): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-07 17:25:57.135: E/YourApp(8984): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-07 17:25:57.135: E/YourApp(8984): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-07 17:25:57.135: E/YourApp(8984): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-07 17:25:57.135: E/YourApp(8984): at java.lang.Thread.run(Thread.java:841)
02-07 17:25:57.143: D/YourApp(8984): onPostExecute : 0
02-07 17:25:57.143: D/YourApp(8984): onPostExecute : autoCompleteAdapter0
这个错误的原因是什么,虽然它适用于浏览器?
答案 0 :(得分:2)
在此行之前关闭while循环括号 JSONObject predictions = new JSONObject(sb.toString());
while ((line = in.readLine()) != null) {
sb.append(line);
}
//turn that string into a JSON object
Log.d("json predictions", "before predictions");
JSONObject predictions = new JSONObject(sb.toString());
Log.d("json predictions", predictions.toString());