通过Android使用谷歌Api但面临一个问题

时间:2015-06-30 08:07:00

标签: java android json google-maps http

我试图使用google apis检索地点的地址,我试图通过发送地理编码来使用api调用。 我尝试将请求的类型从get更改为post但没有任何效果。 但它给出了一个错误:

代码:

 protected Void doInBackground(Location... params) {

            try {
                Location location = params[0];
                String finalURL = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + location.getLatitude() + ", " + location.getLongitude() + "&key=************************************";
                URL url = new URL(finalURL);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String result = bufferedReader.readLine();
                JSONObject jo1 = new JSONObject(result);
                JSONArray jArray1 = jo1.getJSONArray("results");
                JSONObject jo2 = jArray1.getJSONObject(0);
                JSONArray jArray2 = jo2.getJSONArray("address_components");
                Log.d("address : ",jo2.getString("formatted_address"));


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

错误:

    Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.04.04.02.162.107 + cb93e16 + f50fe49 + d7c18e6 + 5b9a565 + 0f3a25d + 607156e + 75511aa + e4d16c0 + 686f3eb + 211a271 + dd281ee +  NOTHING
06-30 13:29:12.207  27343-27441/com.example.kritarthaghosh.business_from_db I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-30 13:29:12.230  27343-27441/com.example.kritarthaghosh.business_from_db D/OpenGLRenderer﹕ Enabling debug mode 0
06-30 13:29:14.338  27343-27359/com.example.kritarthaghosh.business_from_db I/art﹕ Background sticky concurrent mark sweep GC freed 55471(2MB) AllocSpace objects, 49(3MB) LOS objects, 24% free, 13MB/18MB, paused 13.187ms total 69.368ms
06-30 13:29:21.413  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ java.io.FileNotFoundException: https://maps.googleapis.com/maps/api/geocode/json?latlng=12.9592602, 77.7028222&key=************************************
06-30 13:29:21.426  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
06-30 13:29:21.426  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
06-30 13:29:21.426  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
06-30 13:29:21.427  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at com.example.kritarthaghosh.business_from_db.Opening_Screen$GetUserLocationDetails.doInBackground(Opening_Screen.java:117)
06-30 13:29:21.427  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at com.example.kritarthaghosh.business_from_db.Opening_Screen$GetUserLocationDetails.doInBackground(Opening_Screen.java:94)
06-30 13:29:21.427  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-30 13:29:21.427  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-30 13:29:21.427  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-30 13:29:21.427  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-30 13:29:21.427  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-30 13:29:21.427  27343-27679/com.example.kritarthaghosh.business_from_db W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

编辑2:错误

    Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.04.04.02.162.107 + cb93e16 + f50fe49 + d7c18e6 + 5b9a565 + 0f3a25d + 607156e + 75511aa + e4d16c0 + 686f3eb + 211a271 + dd281ee +  NOTHING
06-30 13:45:12.191    1868-2002/com.example.kritarthaghosh.business_from_db I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-30 13:45:12.246    1868-2002/com.example.kritarthaghosh.business_from_db D/OpenGLRenderer﹕ Enabling debug mode 0
06-30 13:45:15.252    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ org.json.JSONException: End of input at character 1 of {
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at org.json.JSONTokener.nextValue(JSONTokener.java:97)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at org.json.JSONTokener.readObject(JSONTokener.java:362)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at org.json.JSONTokener.nextValue(JSONTokener.java:100)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:156)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:173)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at com.example.kritarthaghosh.business_from_db.Opening_Screen$GetUserLocationDetails.doInBackground(Opening_Screen.java:119)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at com.example.kritarthaghosh.business_from_db.Opening_Screen$GetUserLocationDetails.doInBackground(Opening_Screen.java:94)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-30 13:45:15.279    1868-2026/com.example.kritarthaghosh.business_from_db W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

0 个答案:

没有答案