Skip to content

Commit

Permalink
Refresh token automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
shinovon committed Sep 5, 2024
1 parent c0bd64a commit e4171b7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/mahomaps/api/YmapsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ private final String GetRouteUrl(Geopoint a, Geopoint b, int type) {

public final JSONArray Search(String text, Geopoint around, double zone)
throws JSONException, IOException, Http403Exception {
JSONArray j = (JSON.getObject(GetUtf(GetSearchUrl(text, around, zone)))).getArray("features");
return j;
JSONObject j = JSON.getObject(GetUtf(GetSearchUrl(text, around, zone)));
if (!j.has("features")) throw new Http403Exception();
return j.getArray("features");
}

public final JSONObject Route(Geopoint a, Geopoint b, int type)
public final JSONArray Routes(Geopoint a, Geopoint b, int type)
throws JSONException, IOException, Http403Exception {
JSONArray j = (JSON.getObject(GetUtf(GetRouteUrl(a, b, type)))).getArray("features");
if (j.size() == 0)
throw new ConnectionNotFoundException();
JSONObject j1 = j.getObject(0).getArray("features").getObject(0);
return j1;
return j.getObject(0).getArray("features");
}

public static final int ROUTE_BYFOOT = 1;
Expand Down
11 changes: 10 additions & 1 deletion src/mahomaps/overlays/RouteOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class RouteOverlay extends MapOverlay implements Runnable, IButtonHandler
private Geopoint b;
private final int method;
Route route;
private int tries;

private boolean anchorsShown = false;

Expand Down Expand Up @@ -62,12 +63,20 @@ public boolean OnPointTap(Geopoint p) {

public void run() {
try {
route = new Route(MahoMapsApp.api.Route(a, b, method));
if (tries != 0) {
MahoMapsApp.api.RefreshToken();
}
// TODO route variant selection
route = new Route(MahoMapsApp.api.Routes(a, b, method).getObject(0));
LoadRoute();
} catch (IOException e) {
content = new FillFlowContainer(new UIElement[] { new SimpleText(MahoMapsApp.text[111]),
new Button(MahoMapsApp.text[37], 2, this), new Button(MahoMapsApp.text[38], 0, this) });
} catch (Http403Exception e) {
if (tries++ == 0) {
run();
return;
}
content = new FillFlowContainer(
new UIElement[] { new SimpleText(MahoMapsApp.text[135]), new SimpleText(MahoMapsApp.text[136]),
new Button(MahoMapsApp.text[37], 2, this), new Button(MahoMapsApp.text[38], 0, this) });
Expand Down
8 changes: 8 additions & 0 deletions src/mahomaps/screens/SearchLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class SearchLoader extends Form implements Runnable, CommandListener {
private Thread th;
public final String query;
public final Geopoint point;
private int tries;

public SearchLoader(String query, Geopoint point) {
super(query);
Expand All @@ -33,6 +34,9 @@ public SearchLoader(String query, Geopoint point) {
public void run() {
append(new Gauge(MahoMapsApp.text[14], false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING));
try {
if (tries != 0) {
MahoMapsApp.api.RefreshToken();
}
JSONArray arr = MahoMapsApp.api.Search(query, point, 0.1d);
MahoMapsApp.BringSubScreen(new SearchScreen(query, point, arr));
} catch (IOException e) {
Expand All @@ -42,6 +46,10 @@ public void run() {
e.printStackTrace();
} catch (Http403Exception e) {
deleteAll();
if (tries++ == 0) {
run();
return;
}
append(new StringItem(MahoMapsApp.text[135], MahoMapsApp.text[136]));
} catch (Exception e) {
deleteAll();
Expand Down

0 comments on commit e4171b7

Please sign in to comment.