public interface ActivityRender {
public void onRequestSuccess(JSONObject json,HttpRequestMethod method);
public void onRequestFailed(int status,String msg,HttpRequestMethod method);
public void onRequestException(Exception e,HttpRequestMethod method);
}
public enum HttpRequestMethod {
Post,Get;
}
public final class UrlConstants {
public static final String BASE_URL = "http://121.199.26.12:8080/";
// Post url
public static final String LOGIN = "mguid/user/phone/login.do";
public static final String CHANGE_PWD = "mguid/user/phone/updatePass.do";
public static final String REGISTER = "mguid/user/phone/register.do";
// Get url
public static final String ADVERTISE = "mguid/madvs/phone/select.do";
public static final String MANUAL_INDEX = "mguid/medguid/phone/select.do";
private UrlConstants(){}
}
public class WebServiceClient {
protected HttpClient client = HttpClients.createDefault();
public JSONObject request(String url) throws ClientProtocolException,IOException,JSONException,RequestFailedException{
return request(url,(List<NameValuePair>)null,HttpRequestMethod.Get);
}
public JSONObject request(String url,HttpRequestMethod method) throws ClientProtocolException,method);
}
public JSONObject request(String url,List<NameValuePair> params) throws ClientProtocolException,RequestFailedException{
return requestJson(url,params,List<NameValuePair> params,method);
}
public void request(String url,ActivityRender render){
request(url,null,render,HttpRequestMethod.Get);
}
public void request(String url,ActivityRender render,HttpRequestMethod method){
request(url,HttpRequestMethod.Get);
}
public void request(final String url,final List<NameValuePair> params,final ActivityRender render,final HttpRequestMethod method){
Runnable thread = new Runnable(){
public void run() {
try{
JSONObject json = requestJson(url,method);
render.onRequestSuccess(json,method);
}catch(RequestFailedException rfe){
render.onRequestFailed(rfe.getStatus(),rfe.getMessage(),method);
}catch(Exception e){
render.onRequestException(e,method);
}
}
};
thread.run();
}
protected JSONObject requestJson(String url,RequestFailedException{
JSONObject json = null;
HttpResponse response = requestResponse(url,method);
int status = response.getStatusLine().getStatusCode();
if(status == HttpStatus.SC_OK){
HttpEntity entity = response.getEntity();
String text = EntityUtils.toString(entity,Consts.UTF_8);
json = new JSONObject(text);
}else{
throw new RequestFailedException(status,response.getStatusLine().toString());
}
return json;
}
protected HttpResponse requestResponse(String url,IOException{
HttpHost target = new HttpHost("121.199.26.12",8080,"http");
HttpRequestBase httpMethod = null;
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(60000).build();
if(method == HttpRequestMethod.Post){
HttpPost postMethod = createHttpPostInstance("/" + url);
// HttpPost postMethod = new HttpPost(url);
HttpEntity entity = new UrlEncodedFormEntity(params,Consts.UTF_8);
postMethod.setEntity(entity);
httpMethod = postMethod;
}else{
String query = buildQueryParams(params);
HttpGet getMethod = createHttpGetInstance("/" + url + query);
// HttpGet httpMethod = new HttpGet(url);
httpMethod = getMethod;
}
return client.execute(target,httpMethod);
// return client.execute(httpMethod);
}
protected String buildQueryParams(List<NameValuePair> params) throws UnsupportedEncodingException{
String query = "";
if(params != null){
for(NameValuePair nvp : params){
query += nvp.getName() + "=" + URLEncoder.encode(nvp.getValue(),Consts.UTF_8.toString()) + "&";
}
if(query.length() > 0){
query = query.substring(0,query.length() - 1);
query = "?" + query;
}
}
return query;
}
private HttpGet createHttpGetInstance(String url){
HttpHost proxy = new HttpHost("",8000,"http");
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpGet getMethod = new HttpGet(url);
getMethod.setConfig(config);
return getMethod;
}
private HttpPost createHttpPostInstance(String url){
HttpHost proxy = new HttpHost("","http");
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
HttpPost httpMethod = new HttpPost(url);
httpMethod.setConfig(config);
return httpMethod;
}
}
public class WebServiceClientTest extends WebServiceClient implements ActivityRender { public void onRequestSuccess(JSONObject json,HttpRequestMethod method) { out.println(method + ":" + json.toString()); } public void onRequestFailed(int status,HttpRequestMethod method) { out.println("method=" + method + "status=" + status + ",msg=" + msg); } public void onRequestException(Exception e,HttpRequestMethod method) { out.println("exception occured with method " + method); out.println(e); } @Test public void test0() throws Exception{ JSONObject json = request(UrlConstants.ADVERTISE); Assert.assertNotNull(json); } @Test public void test1() throws Exception{ List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("sectionkey","1")); params.add(new BasicNameValuePair("pageNumber","1")); params.add(new BasicNameValuePair("pageSize","2")); request(UrlConstants.MANUAL_INDEX,this); } @Test public void test2() throws Exception{ List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("name","lee")); params.add(new BasicNameValuePair("address","liaoning dalian")); String str = buildQueryParams(params); Assert.assertEquals("?name=lee&address=liaoning+dalian",str); } @Test public void test3() throws Exception{ List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("name","lee")); String str = buildQueryParams(params); Assert.assertEquals("?name=lee",str); } @Test public void test4() throws Exception{ List<NameValuePair> params = new ArrayList<NameValuePair>(); String str = buildQueryParams(params); Assert.assertEquals("",str); } @Test public void test5() throws Exception{ String str = buildQueryParams(null); Assert.assertEquals("",str); } @Test public void test6() throws Exception{ List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("userId","zhtest")); params.add(new BasicNameValuePair("password","123456")); params.add(new BasicNameValuePair("userType","CMA")); request(UrlConstants.LOGIN,this,HttpRequestMethod.Post); //out.println(json.toString()); } @Test public void test7() throws Exception{ List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("pkey","123456")); params.add(new BasicNameValuePair("username","zhtest")); request(UrlConstants.REGISTER,HttpRequestMethod.Post); //out.println(json.toString()); } }