Reputation: 149
I'm trying to implement the Google Places API, but it keeps sending a java.lang.nullpointerexception. I got the code off this site: http://www.androidhive.info/2012/08/android-working-with-google-places-and-maps-tutorial/
From what I can gather I send the URL to some Google Server and get a response. that response is in a JSON format and then it is parsed as my specific "Locations" object. I think something happens during this parsing phase. In the site they use some intermediary object PlacesList and I think that's where things go wrong because I don't understand it.
PlacesList:
public class PlacesList implements Serializable {
@Key
public String status;
@Key
public List<Locations> results;
}
Google search:
public PlacesList search(double latitude, double longitude, double radius)
throws Exception {
this._latitude = latitude;
this._longitude = longitude;
this._radius = radius;
try {
HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
HttpRequest request = httpRequestFactory
.buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
request.getUrl().put("key", API_KEY);
request.getUrl().put("location", _latitude + "," + _longitude);
request.getUrl().put("radius", _radius); // in meters
request.getUrl().put("sensor", "false");
PlacesList list = request.execute().parseAs(PlacesList.class);
Log.i("placelist.class", list.toString());
// Check log cat for places response status
// Log.i("Places Status", "" + list.status);
return list;
} catch (HttpResponseException e) {
Log.e("Error:", e.getMessage());
return null;
}
}
in Main from MainActivity System.err(6447):atcom.example.mapmapagain.MainActivity.onCreate(MainActivity.java:86) ==
try {
PlacesList places = ps.search(curr_lat, curr_longitude, 100.0);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
Here is the Logcat:
04-26 19:10:52.839: W/System.err(4156): android.os.NetworkOnMainThreadException
04-26 19:10:52.899: W/System.err(4156): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-26 19:10:52.899: W/System.err(4156): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-26 19:10:52.899: W/System.err(4156): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-26 19:10:52.899: W/System.err(4156): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
04-26 19:10:52.899: W/System.err(4156): at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165)
04-26 19:10:52.899: W/System.err(4156): at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:88)
04-26 19:10:52.899: W/System.err(4156): at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:814)
04-26 19:10:52.909: W/System.err(4156): at com.example.mapmapagain.PlaceSearch.search(PlaceSearch.java:64)
04-26 19:10:52.909: W/System.err(4156): at com.example.mapmapagain.MainActivity.onCreate(MainActivity.java:86)
04-26 19:10:52.909: W/System.err(4156): at android.app.Activity.performCreate(Activity.java:5104)
04-26 19:10:52.909: W/System.err(4156): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-26 19:10:52.909: W/System.err(4156): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-26 19:10:52.909: W/System.err(4156): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-26 19:10:52.909: W/System.err(4156): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-26 19:10:52.909: W/System.err(4156): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-26 19:10:52.909: W/System.err(4156): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 19:10:52.909: W/System.err(4156): at android.os.Looper.loop(Looper.java:137)
04-26 19:10:52.909: W/System.err(4156): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-26 19:10:52.909: W/System.err(4156): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 19:10:52.909: W/System.err(4156): at java.lang.reflect.Method.invoke(Method.java:511)
04-26 19:10:52.909: W/System.err(4156): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-26 19:10:52.909: W/System.err(4156): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-26 19:10:52.909: W/System.err(4156): at dalvik.system.NativeStart.main(Native Method)
Thanks for any help.
Upvotes: 0
Views: 761
Reputation: 1933
You are doing networking on the main UI thread which is now forbidden by the Android framework, as indicated by your android.os.NetworkOnMainThreadException
exception.
This is forbidden because imagine that your server is unreachable or that your user has a very slow internet connection. When the program starts that search
function of yours, the UI will be completely frozen until it returns because the UI thread will be busy trying to contact that server.
Put your search function in an AsyncTask
, or wrap it in a Runnable
, so that it gets executed in its own thread. Have a look at this question for a good example AsyncTask Android example
Upvotes: 1