Skip to content

Commit

Permalink
Merge pull request #43 from bilthon/master
Browse files Browse the repository at this point in the history
Added some more information about errors on the onRoutingFailure
  • Loading branch information
jd-alexander committed Jan 17, 2016
2 parents 6005a80 + 3f5abee commit 15b0909
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 17 deletions.
21 changes: 15 additions & 6 deletions library/src/main/java/com/directions/route/AbstractRouting.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public abstract class AbstractRouting extends AsyncTask<Void, Void, ArrayList<Ro

protected static final String DIRECTIONS_API_URL = "https://maps.googleapis.com/maps/api/directions/json?";

/* Private member variable that will hold the RouteException instance created in the background thread */
private RouteException mException = null;

public enum TravelMode {
BIKING("bicycling"),
DRIVING("driving"),
Expand Down Expand Up @@ -85,9 +88,9 @@ protected void dispatchOnStart() {
}
}

protected void dispatchOnFailure() {
protected void dispatchOnFailure(RouteException exception) {
for (RoutingListener mListener : _aListeners) {
mListener.onRoutingFailure();
mListener.onRoutingFailure(exception);
}
}

Expand All @@ -111,7 +114,13 @@ private void dispatchOnCancelled() {
*/
@Override
protected ArrayList<Route> doInBackground(Void... voids) {
return new GoogleParser(constructURL()).parse();
ArrayList<Route> result = new ArrayList<Route>();
try {
result = new GoogleParser(constructURL()).parse();
}catch(RouteException e){
mException = e;
}
return result;
}

protected abstract String constructURL();
Expand All @@ -123,9 +132,7 @@ protected void onPreExecute() {

@Override
protected void onPostExecute(ArrayList<Route> result) {
if (result == null) {
dispatchOnFailure();
} else {
if (!result.isEmpty()) {
int shortestRouteIndex = 0;
int minDistance = Integer.MAX_VALUE;

Expand All @@ -145,6 +152,8 @@ protected void onPostExecute(ArrayList<Route> result) {
result.get(i).setPolyOptions(mOptions);
}
dispatchOnSuccess(result, shortestRouteIndex);
} else {
dispatchOnFailure(mException);
}
}//end onPostExecute method

Expand Down
17 changes: 12 additions & 5 deletions library/src/main/java/com/directions/route/GoogleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class GoogleParser extends XMLParser implements Parser {
*/
private int distance;

/* Status code returned when the request succeeded */
private String OK = "OK";

public GoogleParser(String feedUrl) {
super(feedUrl);
}
Expand All @@ -31,18 +34,24 @@ public GoogleParser(String feedUrl) {
* @return a Route object based on the JSON object by Haseem Saheed
*/

public ArrayList<Route> parse() {
public ArrayList<Route> parse() throws RouteException {
ArrayList<Route> routes = new ArrayList<>();

// turn the stream into a string
final String result = convertStreamToString(this.getInputStream());
if (result == null) return null;
if (result == null) {
throw new RouteException("Result is null");
}

try {
//Tranform the string into a json object
final JSONObject json = new JSONObject(result);
//Get the route object

if(!json.getString("status").equals(OK)){
throw new RouteException(json);
}

JSONArray jsonRoutes = json.getJSONArray("routes");

for (int i = 0; i < jsonRoutes.length(); i++) {
Expand Down Expand Up @@ -110,10 +119,8 @@ public ArrayList<Route> parse() {
}

} catch (JSONException e) {
Log.e("Routing Error", e.getMessage());
return null;
throw new RouteException("JSONException. Msg: "+e.getMessage());
}

return routes;
}

Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/directions/route/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

//. by Haseem Saheed
public interface Parser {
List<Route> parse();
List<Route> parse() throws RouteException;
}
45 changes: 45 additions & 0 deletions library/src/main/java/com/directions/route/RouteException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.directions.route;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

/**
* Created by nelson on 1/13/16.
*/
public class RouteException extends Exception {
private static final String TAG = "RouteException";
private final String KEY_STATUS = "status";
private final String KEY_MESSAGE = "error_message";

private String statusCode;
private String message;

public RouteException(JSONObject json){
if(json == null){
statusCode = "";
message = "Parsing error";
return;
}
try {
statusCode = json.getString(KEY_STATUS);
message = json.getString(KEY_MESSAGE);
} catch (JSONException e) {
Log.e(TAG, "JSONException while parsing RouteException argument. Msg: " + e.getMessage());
}
}

public RouteException(String msg){
message = msg;
}

@Override
public String getMessage() {
return message;
}

public String getStatusCode() {
return statusCode;
}
}
1 change: 0 additions & 1 deletion library/src/main/java/com/directions/route/Routing.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ protected String constructURL () {
if(key != null) {
stringBuilder.append("&key=").append(key);
}

return stringBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;

public interface RoutingListener {
void onRoutingFailure();
void onRoutingFailure(RouteException e);

void onRoutingStart();

Expand Down
10 changes: 7 additions & 3 deletions sample/src/main/java/com/directions/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.directions.route.AbstractRouting;
import com.directions.route.Route;
import com.directions.route.RouteException;
import com.directions.route.Routing;
import com.directions.route.RoutingListener;
import com.google.android.gms.common.ConnectionResult;
Expand All @@ -40,7 +41,6 @@
import com.google.android.gms.maps.model.PolylineOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
Expand Down Expand Up @@ -362,10 +362,14 @@ public void route()


@Override
public void onRoutingFailure() {
public void onRoutingFailure(RouteException e) {
// The Routing request failed
progressDialog.dismiss();
Toast.makeText(this,"Something went wrong, Try again", Toast.LENGTH_SHORT).show();
if(e != null) {
Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}else {
Toast.makeText(this, "Something went wrong, Try again", Toast.LENGTH_SHORT).show();
}
}

@Override
Expand Down

0 comments on commit 15b0909

Please sign in to comment.