-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsynTest.java
59 lines (51 loc) · 1.56 KB
/
AsynTest.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
import juno.concurrent.Async;
import juno.concurrent.Callback;
import juno.http.FormBody;
import juno.http.HttpClient;
import juno.http.HttpRequest;
import juno.http.HttpResponse;
import juno.http.HttpUrl;
public class AsynTest {
HttpClient client = HttpClient.getInstance()
.setDebug(true)
;
public Async<HttpResponse> getIpLocation() {
HttpUrl url = new HttpUrl("http://ip-api.com/")
.addPath("json")
.addPath("24.48.0.1")
;
FormBody body = new FormBody()
.add("fields", "status,message,query,country,city")
.add("lang", "en")
;
HttpRequest request = new HttpRequest(
"GET", url, body)
;
return client.createAsync(request);
}
public void async() {
Async<HttpResponse> async = getIpLocation();
async.execute(new Callback<HttpResponse>() {
@Override
public void onResponse(HttpResponse response) throws Exception {
String result = response.readString();
System.out.println(result);
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
});
}
public void sync() throws Exception {
HttpResponse response = getIpLocation().await();
String result = response.readString();
System.out.println(result);
response.close();
}
public static void main(String[] args) throws Exception {
AsynTest test = new AsynTest();
//test.async();
test.sync();
}
}