-
Notifications
You must be signed in to change notification settings - Fork 215
/
GeolocationClient.java
63 lines (51 loc) · 2.4 KB
/
GeolocationClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.mozilla.vrbrowser.geolocation;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import org.json.JSONObject;
import org.mozilla.geckoview.GeckoWebExecutor;
import org.mozilla.geckoview.WebRequest;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
class GeolocationClient {
static void getGeolocation(@NonNull GeckoWebExecutor executor, String aQuery, Function<GeolocationData, Void> success, Function<String, Void> error) {
new Handler(Looper.getMainLooper()).post(() -> {
WebRequest request = new WebRequest.Builder(aQuery)
.method("GET")
.build();
executor.fetch(request).then(webResponse -> {
String body;
if (webResponse != null) {
if (webResponse.body != null) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = webResponse.body.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
body = new String(buffer.toByteArray(), StandardCharsets.UTF_8);
if (webResponse.statusCode == 200) {
JSONObject reader = new JSONObject(body);
success.apply(GeolocationData.parse(reader.toString()));
} else {
// called when response HTTP status is not "200"
error.apply(String.format("Network Error: %s", webResponse.statusCode));
}
} else {
// WebResponse body is null
error.apply("Response body is null");
}
} else {
// WebResponse is null
error.apply("Response is null");
}
return null;
}).exceptionally(throwable -> {
// Exception happened
error.apply(String.format("Unknown network Error: %s", String.format("An exception happened during the request: %s", throwable.getMessage())));
return null;
});
});
}
}