Skip to content
This repository has been archived by the owner on Dec 21, 2020. It is now read-only.

RuntimeException when running sample code (Problem while fetching path [49.6724,11.3494, 49.655,11.418]: null) #7

Closed
farmazon3000 opened this issue Jan 7, 2016 · 9 comments

Comments

@farmazon3000
Copy link

When I run the example code on Android with

compile 'com.graphhopper:directions-api-java-client:0.5.0.2'
GraphHopperWeb gh = new GraphHopperWeb();
// insert your key here
gh.setKey("YOUR_KEY");
// change timeout, default is 5 seconds
gh.getDownloader().setConnectTimeout(10, TimeUnit.SECONDS);

// specify at least two coordinates
GHRequest req = new GHRequest().
   addPoint(new GHPoint(49.6724, 11.3494)).
   addPoint(new GHPoint(49.6550, 11.4180));
// Set vehicle like car, bike and foot
req.setVehicle("bike");
// Optionally enable/disable elevation in output PointList, currently bike and foot support elevation, default is false
req.getHints().put("elevation", false);
// Optionally enable/disable turn instruction information, defaults is true
req.getHints().put("instructions", true);
// Optionally enable/disable path geometry information, default is true
req.getHints().put("calcPoints", true);
// note: turn off instructions and calcPoints if you just need the distance or time 
// information to make calculation and transmission faster
//
// Optionally set specific locale for instruction information, supports already over 25 languages,
// defaults to English
req.setLocale(Locale.GERMAN);

GHResponse res = gh.route(req);

if(res.hasErrors()) {
   // handle or throw exceptions res.getErrors()
   return;
}

// get path geometry information (latitude, longitude and optionally elevation)
PointList pl = res.getPoints();
// distance of the full path, in meter
double distance = res.getDistance();
// time of the full path, in milliseconds
long millis = res.getTime();
// get information per turn instruction
InstructionList il = res.getInstructions();

I'm getting an exception

Caused by: java.lang.RuntimeException: Problem while fetching path [49.6724,11.3494, 49.655,11.418]: null
   at com.graphhopper.api.GraphHopperWeb.route(GraphHopperWeb.java:145)
   at myapp.MapsActivity.xxx(MapsActivity.java:121)
   at myapp.MapsActivity.onCreate(MapsActivity.java:72)
   at android.app.Activity.performCreate(Activity.java:5958)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
   at android.app.ActivityThread.access$800(ActivityThread.java:144) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:155) 
   at android.app.ActivityThread.main(ActivityThread.java:5696) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
Caused by: android.os.NetworkOnMainThreadException
   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1155)
   at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
   at libcore.io.IoBridge.connectErrno(IoBridge.java:154)
   at libcore.io.IoBridge.connect(IoBridge.java:122)
   at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
   at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
   at java.net.Socket.connect(Socket.java:882)
   at com.squareup.okhttp.internal.Platform$Android.connectSocket(Platform.java:190)
   at com.squareup.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160)
   at com.squareup.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:79)
   at com.squareup.okhttp.Connection.connect(Connection.java:143)
   at com.squareup.okhttp.Connection.connectAndSetOwner(Connection.java:185)
   at com.squareup.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
   at com.squareup.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
   at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
   at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
   at com.squareup.okhttp.Call.getResponse(Call.java:273)
   at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
   at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
   at com.squareup.okhttp.Call.execute(Call.java:81)
   at com.graphhopper.api.GraphHopperWeb.route(GraphHopperWeb.java:126)
   at myapp.MapsActivity.xxx(MapsActivity.java:121) 
   at myapp.MapsActivity.onCreate(MapsActivity.java:72) 
   at android.app.Activity.performCreate(Activity.java:5958) 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
   at android.app.ActivityThread.access$800(ActivityThread.java:144) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:155) 
   at android.app.ActivityThread.main(ActivityThread.java:5696) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
@karussell
Copy link
Member

Thanks - I'll investigate!

@farmazon3000
Copy link
Author

It seems that the exception happens even before any HTTP request leaves my Android device - I've set up Charles proxy and it does not show any communication when the exception reproduces.

@karussell
Copy link
Member

The sample code itsef still runs fine here. The probem is highly likely something related to Android.

OkHttpClient fails to fetch the content at this line in GraphHopperWeb

String str = downloader.newCall(okRequest).execute().body().string();

Can you print here the url created in createRequest (without your API key!) and try via browser and command line like
curl the_url_as_string

It seems that the exception happens even before any HTTP request leaves my Android device

Strange. Would you mind to make sure you do not encounter something like this here: http://stackoverflow.com/questions/13390026/android-socket-connection-fail

@farmazon3000
Copy link
Author

Yes, this may be it as in the log I provided above there is

Caused by: android.os.NetworkOnMainThreadException

I'll make sure tomorrow if this is the problem, anyway I thought this client does some background threading by itself.

@karussell
Copy link
Member

anyway I thought this client does some background threading by itself

No. It it intended for usage in any environment like desktop/server/android and the client has to make sure it will be called properly according to the environment requirements

BTW: changing the timeout shoud NOT be required. Results should come always within milliseconds:

// change timeout, default is 5 seconds

@farmazon3000
Copy link
Author

Yes, this was problem with my code calling your example on main thread - calling it from non main thread works fine. However it may be helpful to either:

  • give more meaningful exception than Caused by: java.lang.RuntimeException: Problem while fetching path [...]: null
  • or state it clearly in README.md (it may not be so obvious in Ardroid world).

@karussell
Copy link
Member

Would you try again the wrong approach on the thread and change the code:
String str = downloader.newCall(okRequest).execute().body().string();

to

Object o = downloader.newCall(okRequest).execute().body();
if(o == null)
   throw new IllegalStateException("Response body is empty. Maybe issue #7?");

String str = body.toString();

If this is the reason I'll fix the code and also include a notice in the readme

@karussell
Copy link
Member

@farmazon3000 Sorry to bother you again but as Android is not my main target: would you try this fix?

@karussell
Copy link
Member

Closing here for now

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants