5

Here's my task. Given a pair of latitude and longitude, I want to get a JSON from Google Places API. Following function, reverseGeoCoding, returns the String JSON.

public String reverseGeoCoding(String lat, String lng) {
    latitude = lat;
    longitude = lng;
    String json = new String();
    String url = new String();

    URL googleAPI;
    try {
        url = URL + "location=" + latitude + "," + longitude + "&" + "radius=" + radius + "&" + "language=" + language + "&" + "sensor=false" + "&" + "key=" + APIKey;

        googleAPI = new URL(url);
        Log.d("TEST", googleAPI.toString());
        URLConnection gc = (URLConnection) googleAPI.openConnection();
        Log.d("TEST", gc.toString());

        InputStream is = gc.getInputStream();
        Log.d("TEST", "InputStream created");
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        Log.d("TEST", "reader created");
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            json = json + inputLine + "\n";
            Log.d("TEST", inputLine);
        }

        in.close();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return json;
}

However, getInputStream() does not work. I have checked the url request on web browser, and it worked properly (returing a JSON document).

Error messages are below. (Why the lines 17 and 18 produce the errors)

1. 12-11 00:46:02.862: W/System.err(12953): android.os.NetworkOnMainThreadException
2. 12-11 00:46:02.872: W/System.err(12953):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
3. 12-11 00:46:02.872: W/System.err(12953):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
4. 12-11 00:46:02.872: W/System.err(12953):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
5. 12-11 00:46:02.872: W/System.err(12953):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
6. 12-11 00:46:02.872: W/System.err(12953):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
7. 12-11 00:46:02.872: W/System.err(12953):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
8. 12-11 00:46:02.882: W/System.err(12953):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
9. 12-11 00:46:02.882: W/System.err(12953):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10. 12-11 00:46:02.882: W/System.err(12953):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11. 12-11 00:46:02.882: W/System.err(12953):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
12. 12-11 00:46:02.882: W/System.err(12953):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
13. 12-11 00:46:02.882: W/System.err(12953):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
14. 12-11 00:46:02.882: W/System.err(12953):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
15. 12-11 00:46:02.882: W/System.err(12953):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
16. 12-11 00:46:02.882: W/System.err(12953):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
17. 12-11 00:46:02.882: W/System.err(12953):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
18. 12-11 00:46:02.894: W/System.err(12953):    at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)
19. 12-11 00:46:02.894: W/System.err(12953):    at locationpredict.POIList.reverseGeoCoding(POIList.java:55)
20. 12-11 00:46:02.894: W/System.err(12953):    at locationpredict.TestMain$1.onClick(TestMain.java:62)

3 Answers 3

5

Because of people using heavy networking operation on the main thread which leads to application unresponsiveness, this new function has been added to prevent people from doing it.
You should use AsyncTask or service to do this.

        private class GetJsonTask extends AsyncTask<String, Void, String>{


            @Override
            protected String doInBackground(String... params) {
        try {
            latitude = params[0];
             longitude = params[1];
             String json = new String();
             String url = new String();
             URL googleAPI;


            url = URL + "location=" + latitude + "," + longitude + "&" + "radius=" + radius + "&" + "language=" + language + "&" + "sensor=false" + "&" + "key=" + APIKey;

            googleAPI = new URL(url);
            Log.d("TEST", googleAPI.toString());
            URLConnection gc = (URLConnection) googleAPI.openConnection();
            Log.d("TEST", gc.toString());

            InputStream is = gc.getInputStream();
            Log.d("TEST", "InputStream created");
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            Log.d("TEST", "reader created");
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                json = json + inputLine + "\n";
                Log.d("TEST", inputLine);
            }

            in.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return json;
    }

    @Override
        protected void onPostExecute(String result) {
            parseJson(result); // json should be stored in the result
        }
  }

private void parseJson(String json){
     JSONObject obj = new JSONObject(json);
....

}

Call it like this:

String[] data = new String[2];
data[0] = lat;
data[1] = long;
new GetJsonTask().execute(data);

Or you can bypass it by adding the following code in your onCreate() method before calling the method, but I dont recommend this method. Just use it for testing.

   if( Build.VERSION.SDK_INT >= 9){
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    }
Sign up to request clarification or add additional context in comments.

7 Comments

One more question, the override method, onPostExecute return void. How can I get the results from Google Places API by the onPostExecute?
The results are in the String result. You can parse your json inside the onPostExecute() method. Or pass it with a method like my example and parse it for cleaner code.
I already write the codes to parse JSON returned by doInBackground in the onPostExecute as you explained. However, the main function, which calls the asyncronous method (AsyncTask.execute(data)), cannot know when the onPostExecute methods would be called. right?
Yes. It is another thread. What do you want to do with the data from json?
For example, when the method, onPostExecute, is called, the parsed result of JSON is stored to a member variable, let's say arraylist in the GJsonTask class. However, right after the method, AsyncTask.execute(data), is called, the arraylist is empty.
|
2

> android.os.NetworkOnMainThreadException :

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

use AsyncTask to avoid this error. and put all network related task inside doInBackground method of AsyncTask

Comments

0

As other answers suggest, use async task. Also recommend gson for converting objects to / from json strings!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.