|
1 | 1 | package org.mozilla.vrbrowser.geolocation; |
2 | 2 |
|
3 | | -import com.loopj.android.http.AsyncHttpClient; |
4 | | -import com.loopj.android.http.JsonHttpResponseHandler; |
| 3 | +import android.os.Handler; |
| 4 | +import android.os.Looper; |
| 5 | + |
| 6 | +import androidx.annotation.NonNull; |
5 | 7 |
|
6 | 8 | import org.json.JSONObject; |
| 9 | +import org.mozilla.geckoview.GeckoWebExecutor; |
| 10 | +import org.mozilla.geckoview.WebRequest; |
7 | 11 |
|
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import java.nio.charset.StandardCharsets; |
8 | 14 | import java.util.function.Function; |
9 | 15 |
|
10 | | -import cz.msebera.android.httpclient.Header; |
11 | | - |
12 | | -public class GeolocationClient { |
13 | | - |
14 | | - private static final int RETRY_SLEEP = 5 * 1000; |
15 | | - |
16 | | - private static AsyncHttpClient client = new AsyncHttpClient(); |
17 | | - |
18 | | - public static void getGeolocation(String aQuery, int retries, Function success, Function error) { |
19 | | - client.cancelAllRequests(true); |
20 | | - client.setMaxRetriesAndTimeout(retries, RETRY_SLEEP); |
21 | | - client.get(aQuery, null, new JsonHttpResponseHandler("ISO-8859-1") { |
22 | | - |
23 | | - @Override |
24 | | - public void onSuccess(int statusCode, Header[] headers, JSONObject response) { |
25 | | - success.apply(GeolocationData.parse(response.toString())); |
26 | | - } |
27 | | - |
28 | | - @Override |
29 | | - public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { |
30 | | - error.apply(errorResponse); |
31 | | - } |
32 | | - |
| 16 | +class GeolocationClient { |
| 17 | + |
| 18 | + static void getGeolocation(@NonNull GeckoWebExecutor executor, String aQuery, Function<GeolocationData, Void> success, Function<String, Void> error) { |
| 19 | + new Handler(Looper.getMainLooper()).post(() -> { |
| 20 | + WebRequest request = new WebRequest.Builder(aQuery) |
| 21 | + .method("GET") |
| 22 | + .build(); |
| 23 | + |
| 24 | + executor.fetch(request).then(webResponse -> { |
| 25 | + String body; |
| 26 | + if (webResponse != null) { |
| 27 | + if (webResponse.body != null) { |
| 28 | + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 29 | + int nRead; |
| 30 | + byte[] data = new byte[1024]; |
| 31 | + while ((nRead = webResponse.body.read(data, 0, data.length)) != -1) { |
| 32 | + buffer.write(data, 0, nRead); |
| 33 | + } |
| 34 | + body = new String(buffer.toByteArray(), StandardCharsets.UTF_8); |
| 35 | + |
| 36 | + if (webResponse.statusCode == 200) { |
| 37 | + JSONObject reader = new JSONObject(body); |
| 38 | + success.apply(GeolocationData.parse(reader.toString())); |
| 39 | + |
| 40 | + } else { |
| 41 | + // called when response HTTP status is not "200" |
| 42 | + error.apply(String.format("Network Error: %s", webResponse.statusCode)); |
| 43 | + } |
| 44 | + |
| 45 | + } else { |
| 46 | + // WebResponse body is null |
| 47 | + error.apply("Response body is null"); |
| 48 | + } |
| 49 | + |
| 50 | + } else { |
| 51 | + // WebResponse is null |
| 52 | + error.apply("Response is null"); |
| 53 | + } |
| 54 | + return null; |
| 55 | + |
| 56 | + }).exceptionally(throwable -> { |
| 57 | + // Exception happened |
| 58 | + error.apply(String.format("Unknown network Error: %s", String.format("An exception happened during the request: %s", throwable.getMessage()))); |
| 59 | + return null; |
| 60 | + }); |
33 | 61 | }); |
34 | 62 | } |
35 | 63 | } |
0 commit comments