额。。因项目需求所以都是get方式请求的,当然换post也是很简单的,大家应该都能看懂,
废话不多说,直接上代码。
public String getAllGroups(){
// 请求接口地址
String url = "http://127.0.0.1:8008/sss/bbb";
HttpClient httpclient = null;
GetMethod post = null;
String result = "";
String jsonResult = null;
try {
//创建连接
httpclient = new HttpClient();
post = new GetMethod(url);
// 设置编码方式
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 执行请求
httpclient.executeMethod(post);
// 判断网络连接状态码是否正常(0--200都数正常)
if (post.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 接口返回信息
result = new String(post.getResponseBody(), "UTF-8");
}else{
System.out.println("返回错误的状态码为:"+post.getStatusLine().getStatusCode());
}
jsonResult = JSONObject.fromObject(result).getString("groups");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
post.releaseConnection();
((SimpleHttpConnectionManager) httpclient.getHttpConnectionManager()).shutdown();
}
return jsonResult;
}
public String getStopList(){
String url = "http://127.0.0.1:8008/sss/bbb";
String result="";
String jsonResult = null;
try {
// 根据地址获取请求
HttpGet request = new HttpGet(url);//这里发送get请求
// 获取当前客户端对象
DefaultHttpClient httpClient = new DefaultHttpClient();
// 通过请求对象获取响应对象
HttpResponse response = httpClient.execute(request);
// 判断网络连接状态码是否正常(0--200都数正常)
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result= EntityUtils.toString(response.getEntity(),"utf-8");
jsonResult = JSONObject.fromObject(result).getString("stops");
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonResult;
}
这个是带参数并且返回结果有做处理的,json数据格式下面也会给出。
public HashMap<String, Object> getStopInfo(String param){
HashMap<String, Object> map = new HashMap<>();
// 请求接口地址
String url = "http://127.0.0.1:8008/sss/bbb"+"?bundle="+param;
// 请求参数
HttpClient httpclient = null;
GetMethod post = null;
String result= "";
String jsonResult = null;
try {
//创建连接
httpclient = new HttpClient();
post = new GetMethod(url);
// 设置编码方式
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 执行请求
httpclient.executeMethod(post);
// 判断网络连接状态码是否正常(0--200都数正常)
if (post.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 接口返回信息
result = new String(post.getResponseBody(), "UTF-8");
JSONObject jb = new JSONObject();
JSONObject jb1 = jb.fromObject(result);
JSONArray ja = JSONArray.fromObject(jb1.get("StopInfo"));
Iterator<Object> it = ja.iterator();
while (it.hasNext()) {
JSONObject ob = (JSONObject) it.next();
String bundle = ob.get("bundle")+"";
String owner = ob.get("owner")+"";
String inportCount = ob.get("inportCount")+"";
String outportCount = ob.get("outportCount")+"";
String groups = ob.get("groups")+"";
String properties = ob.get("properties")+"";
String name = ob.get("name")+"";
String description = ob.get("description")+"";
map.put("bundle", bundle);
map.put("owner", owner);
map.put("inportCount", inportCount);
map.put("outportCount", outportCount);
map.put("groups", groups);
map.put("properties", properties);
map.put("name", name);
map.put("description", description);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
post.releaseConnection();
((SimpleHttpConnectionManager) httpclient.getHttpConnectionManager()).shutdown();
}
return map;
}
上面的json数据格式:
如果有问题或者大家有什么好的代码欢迎留言提出。