-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIRequest.java
More file actions
132 lines (105 loc) · 3.24 KB
/
APIRequest.java
File metadata and controls
132 lines (105 loc) · 3.24 KB
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
123
124
125
126
127
128
129
130
131
132
package com.edward.http;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public class APIRequest {
public enum ApiRequestMethod {
GET, POST, PUT, DELETE
}
protected HttpClient getHttpClient(){
HttpClient client = new HttpClient();
client.getParams().setContentCharset("utf-8");
return client;
}
protected String call;
protected ApiRequestMethod method;
protected String paramsStr;
protected String getBaseURL(){
return "http://pleader.cloume.com/";
};
private List<NameValuePair> headers = new ArrayList<NameValuePair>();
public void addHeaders(String key, String value){
NameValuePair header = new NameValuePair(key, value);
headers.add(header);
}
protected void beforeExecute(HttpMethod m){
addHeaders("Accept", "application/json");
for(NameValuePair nv : headers){
m.setRequestHeader(nv.getName(), nv.getValue());
}
};
/***
* Construct a OAPI request
* @param call: api path
* @param method: request method
*/
public APIRequest(String call, ApiRequestMethod method){
this.setCall(call);
this.setMethod(method);
}
public APIRequest(String call, ApiRequestMethod method, String requestParameters){
this.setCall(call);
this.setMethod(method);
this.setParamsStr(requestParameters);
}
public JSON execute(){
HttpMethod m = null;
String fullCall = this.getBaseURL() + this.call;
if(this.method == ApiRequestMethod.GET){
m = new GetMethod(fullCall);
((GetMethod) m).setQueryString(paramsStr);
//((GetMethod) m).setQueryString(array);
}else if(this.method == ApiRequestMethod.POST){
m = new PostMethod(fullCall);
RequestEntity entity = new StringRequestEntity(paramsStr);
((PostMethod) m).setRequestEntity(entity);
//((PostMethod) m).setRequestBody(array);
}
JSON obj = JSONObject.fromObject(null);
this.beforeExecute(m);
try {
int state = this.getHttpClient().executeMethod(m);
if(state == HttpStatus.SC_OK){
InputStream bs = m.getResponseBodyAsStream();
BufferedReader br = new BufferedReader( new InputStreamReader(bs, "utf8") );
String temp = null;
StringBuffer buffer = new StringBuffer();
while((temp = br.readLine()) != null){
buffer.append(temp);
}
String body = buffer.toString();
if(body.length() > 0){
obj = JSONSerializer.toJSON(body);
}
}
} catch (Exception e) {
e.printStackTrace();
}
if(m != null){
m.releaseConnection();
}
return obj;
}
public void setCall(String call){
this.call = call;
}
public void setMethod(ApiRequestMethod method) {
this.method = method;
}
public void setParamsStr(String paramsStr){
this.paramsStr = paramsStr;
}
}