概述
现今很多Web应用开发都是分前后台的,后台开发提供接口调用返回Json对象,前台使用JS框架去加载后台返回的Json.本文以实例简述如何通过HttpClient测试这样的后台接口
处理Json对象的基本API
JSON包中最常用的两个类就是JSONObject和JSONArray,具体可以参考JSON for java入门总结
如下是自己模仿的简单例子:
package com.james.json; import org.json.JSONArray; import org.json.JSONObject; public class JsonTest { public static void main(String[] args) { // Test JSONObject. JSONObject jsonobj = new JSONObject("{'name':'jingshou','age':30}"); String name = jsonobj.getString("name"); int age = jsonobj.getInt("age"); System.out.println(jsonobj.toString()); System.out.println(name+":"+age); System.out.println("**********"); // Test JSONArray. JSONArray jsonarray = new JSONArray("[{'name':'jingshou','age':30},{'name':'xiaohong','age':29}]"); for(int i=0;i<jsonarray.length();i++){ JSONObject jo = jsonarray.getJSONObject(i); System.out.println(jo); String name1 = jo.getString("name"); int age1 = jo.getInt("age"); System.out.println("name1= "+name1); System.out.println("age1= "+age1); } } }
运行结果如下:
{"age":30,"name":"jingshou"} jingshou:30 ********** {"age":30,"name":"jingshou"} name1= jingshou age1= 30 {"age":29,"name":"xiaohong"} name1= xiaohong age1= 29
从以上例子我们看到的基本事实是:
- 可以通过字符串直接构造一个JSONObject
- JSONObject里的key在显式传入的时候是用单引号包裹起来的,但是打印出来的时候依然是我们期望的双引号
使用httpclient处理API返回
如下例子演示如何使用httpClient获取API返回的JSON字符串以及处理:
package com.james.json; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject; public class SimpleServiceTest { public static void main(String[] args) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost("http://jingshou.com/admin/searchUser.action?search_loginid=jingshou"); CloseableHttpResponse response = httpclient.execute(httppost); try { HttpEntity myEntity = response.getEntity(); System.out.println(myEntity.getContentType()); System.out.println(myEntity.getContentLength()); String resString = EntityUtils.toString(myEntity); // 使用返回的字符串直接构造一个JSONObject JSONObject jsonobj = new JSONObject(resString); System.out.println(jsonobj.toString()); // 获取返回对象中"resultSize的值" int resutltSize = jsonobj.getInt("resultSize"); System.out.println("Search Results Size is: "+ resutltSize); // 获取"clients"的值,它是一个JSONArray JSONArray jsonarray = jsonobj.getJSONArray("clients"); System.out.println(jsonarray.toString()); } finally { response.close(); } } }
运行结果如下:
Content-Type: text/plain; charset=UTF-8 -1 {"resultSize":1,"clients":[{"expirationDate":0,"reqStatus":0,"mostRecentActivity":1388376890000,"clientName":"Jingshou Li","company":"Pending","internal":true,"clientId":"jingshou","salesNames":"","disabled":false}]} Search Results Size is: 1 [{"expirationDate":0,"reqStatus":0,"mostRecentActivity":1388376890000,"clientName":"Jingshou Li","company":"Pending","internal":true,"clientId":"jingshou","salesNames":"","disabled":false}]
小结:
- 通过API返回的JSON字符串直接构造JSON对象
- 如果要读取JSONObject内部数据,需要事先知道对象的结构,所以以上处理方法不具有通用性,只能处理特定的返回
补充学习:
- http://cgs1999.iteye.com/blog/1608003
- http://cgs1999.iteye.com/blog/1609756
原创文章,转载请注明出处,原文地址:使用httpClient进行接口测试