-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonApi.java
122 lines (106 loc) · 3.35 KB
/
JsonApi.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cn.lxw.us.network;
import android.util.Log;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import cn.lxw.us.models.JsonMessage;
import cn.lxw.us.utils.NetworkUtils;
/**
* Created by Lianxw on 2015/7/22.
* 访问API基础类
*/
public abstract class JsonApi<T extends JsonMessage> {
public static final String API_HOST ="http://192.168.199.228/api/";
public static final int METHOD_GET = 0;
public static final int METHOD_POST = 1;
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final long CONNECTION_TIMEOUT = 10;
private static final long READ_TIMEOUT = 30;
private int method;
private String url;
private OkHttpClient client;
public JsonApi(String url) {
this(url, METHOD_GET);
}
public JsonApi(String url, int method) {
this.url = url;
this.method = method;
this.client = new OkHttpClient();
this.client.setConnectTimeout(CONNECTION_TIMEOUT, TimeUnit.SECONDS);
this.client.setReadTimeout(READ_TIMEOUT,TimeUnit.SECONDS);
}
public T call() {
prepareUrl();
String json = this.method == METHOD_GET?get():post();
if(json==null) {
return null;
}
T rst = null;
try {
rst = parse(json);
} catch (Exception e) {
Log.e("JsonApi","parse json error:"+e.getMessage());
e.printStackTrace();
}
return rst;
}
private void prepareUrl() {
int questionMark = url.indexOf('?');
String address;
String queryString;
if (questionMark > 0) {
address = url.substring(0, questionMark);
queryString = url.substring(questionMark)+"&";
} else {
address = url;
queryString = "?";
}
String params = getParams();
if (params != null) {
queryString += params;
}
if(queryString.length()>1) {
this.url = address + queryString;
}
}
private String get() {
Request request = new Request.Builder()
.url(url)
.build();
String json = null;
try {
Response response = client.newCall(request).execute();
json = response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
private String post() {
String jsonBody = getBody();
if (jsonBody==null){
return null;
}
RequestBody body = RequestBody.create(JSON, jsonBody);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
String json = null;
try {
Response response = client.newCall(request).execute();
json = response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
protected abstract String getParams();
protected abstract String getBody();
protected abstract T parse(String json);
}