欢迎使用CSDN-markdown编辑器

来源:互联网 发布:更改mac屏幕下面的图标 编辑:程序博客网 时间:2024/05/20 18:47

最近需要写一些API测试的小玩意儿,百度了一下,试着在java上使用httpClient写一些自动调用方法,类似爬虫的一个东西,但和爬虫的需求不同。

这是我自己写的一个util工具类:

[java] view plaincopy在CODE上查看代码片派生到我的代码片

import java.io.IOException;  import org.apache.http.HttpResponse;  import org.apache.http.ParseException;  import org.apache.http.client.ClientProtocolException;  import org.apache.http.client.HttpClient;  import org.apache.http.client.methods.HttpGet;  import org.apache.http.client.methods.HttpPost;  import org.apache.http.impl.client.HttpClientBuilder;  import org.apache.http.util.EntityUtils;  import org.json.JSONException;  import org.json.JSONObject;  public class HttpUtil {      private HttpClient mClient;      private HttpResponse mRes = null;      private String method, url;      private JSONObject json,data;      private static final String errorMessage = "Something wrong!";      private static final String invalidKeyMessage = "no such key exists!";        public HttpUtil(String method, String url){          this.method = method;          this.url = url;      }      public void excute(){          mClient = HttpClientBuilder.create().build();          try {              switch(method){              case "GET":                  mRes = mClient.execute(new HttpGet(url));                  break;              case "POST":                  mRes = mClient.execute(new HttpPost(url));              }              json = new JSONObject(EntityUtils.toString(mRes.getEntity()));              data = json.getJSONObject("data");          } catch (ClientProtocolException e) {              // TODO Auto-generated catch block              e.printStackTrace();          } catch (IOException e) {              // TODO Auto-generated catch block              e.printStackTrace();          } catch (ParseException e) {              // TODO Auto-generated catch block              e.printStackTrace();          } catch (JSONException e) {              // TODO Auto-generated catch block              e.printStackTrace();          }      }      /*      * 检查返回值是否有效,检查json值是否有效      */      private boolean checkValid(){          return mRes == null || json == null;      }      /*      * 取得服务端回复的状态.      */      public String getStatus(){          if (checkValid()) return errorMessage;          return mRes.getStatusLine().toString();      }      /*      * 取得状态码      */      public String getStatusCode(){          if (checkValid()) return errorMessage;          return Integer.valueOf(mRes.getStatusLine().getStatusCode()).toString();      }      /*      * 取得返回的message信息      */      public String getMessage(){          if (checkValid()) return errorMessage;          try {              return json.getString("message");          } catch (JSONException e) {              // TODO Auto-generated catch block              e.printStackTrace();          }          return errorMessage;      }      /*      * 返回整个data信息,字符串表示      */      public String getDataString(){          if (checkValid()) return errorMessage;          return data.toString();      }      /*      * 默认返回数据是json格式,主json中有一个子树data,默认data是我们的要处理的json数据      * 此处返回data的一个键key的值      */      public String getDataItem(String key){          if (checkValid()) return errorMessage;          if (data.isNull(key)) return key+": "+invalidKeyMessage;          try {              return data.getString(key);          } catch (JSONException e) {              // TODO Auto-generated catch block              e.printStackTrace();          }          return errorMessage;      }  }  

使用方法:
[java] view plaincopy在CODE上查看代码片派生到我的代码片

              method = "GET";  url = server_url+"v1/passport/login?account=18516005292&passwd=123456&ct=4&app=1&v=4";  HttpUtil hu = new HttpUtil(method, url);  hu.excute();  

在工具类,为了解析json数据,需要用到一个json处理包json.org.jar,下载地址

对于需要解析的json字符串jsonString,直接使用

[java] view plaincopy在CODE上查看代码片派生到我的代码片

JSONObject json = new JSONObject(jsonString);  

即可创建一个json对象,使用

json.getString(key)可以获取json一级节点的key的字符串。

要访问二级或以上的json节点,只需要递归构造和调用即可。

0 0
原创粉丝点击