Doc App WebService

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, IOException, JSONException, RequestFailedException{
return request(url, (List<NameValuePair>)null, method);
}

public JSONObject request(String url, List<NameValuePair> params) throws ClientProtocolException, IOException, JSONException, RequestFailedException{
return requestJson(url, params, HttpRequestMethod.Get);
}


public JSONObject request(String url, List<NameValuePair> params, HttpRequestMethod method) throws ClientProtocolException, IOException, JSONException, RequestFailedException{
return requestJson(url, 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, null, render, method);
}

public void request(String url, List<NameValuePair> params, ActivityRender render){
request(url, params, render, 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, params, 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, List<NameValuePair> params, HttpRequestMethod method) throws ClientProtocolException, IOException, JSONException, RequestFailedException{
JSONObject json = null;

HttpResponse response = requestResponse(url, params, 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, List<NameValuePair> params, HttpRequestMethod method) throws ClientProtocolException, 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("", 8000, "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, String msg, 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, params, 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, params, this, HttpRequestMethod.Post);
// out.println(json.toString());
}

@Test
public void test7() throws Exception{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pkey", "zhtest"));
params.add(new BasicNameValuePair("password", "123456"));
params.add(new BasicNameValuePair("username", "zhtest"));

request(UrlConstants.REGISTER, params, this, HttpRequestMethod.Post);
// out.println(json.toString());
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值