使用HttpClient4.2.5做了一个简单的接口测试的实验,记录下来以后参考。
package cn.test;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class MobileLibInterfaceTest {
private static final int SUCCESS_CODE = 1;
private static final String PRODUCT_ID = "326610";
private static final String PRODUCT_NAME = "三星 GALAXY Note II N7100(16GB)";
//初始化httpClient
HttpClient httpClient = new DefaultHttpClient();
@BeforeClass
public void beforeClass() {
}
@Parameters({"URL"})
@Test
public void productInfo(String URL) throws ClientProtocolException, IOException, JSONException {
//获取httpGet
HttpGet httpGet = new HttpGet(URL);
//执行get请求
HttpResponse response = httpClient.execute(httpGet);
//判断服务器响应的状态码
System.out.println(response.getStatusLine().getStatusCode());
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode(),"状态码不是200,请求没成功");
//读取返回数据
String entity = EntityUtils.toString(response.getEntity());
//返回的数据类型为json,使用org.json jar包来解析
JSONObject json = new JSONObject(entity);
System.out.println(json.get("code"));
Assert.assertEquals(SUCCESS_CODE, json.get("code"),"返回的code不为1,请求失败");
//返回的结果中,data为数组,将其转换为list,获取指定key的值
JSONArray array = json.getJSONArray("data");
for(int i=0; i<array.length(); i++) {
JSONObject data = array.getJSONObject(i);
String phoneID = data.getString("id");
Assert.assertEquals(phoneID, PRODUCT_ID, "返回的产品 型号ID不一致");
String phoneName = data.getString("name");
Assert.assertEquals(phoneName, PRODUCT_NAME, "返回的产品名称不一致");
System.out.println(phoneID+", "+phoneName);
}
}
@AfterClass
public void afterClass() {
//断开连接
httpClient.getConnectionManager().shutdown();
}
}
请求成功后,页面返回的是的json格式的数据
{"code":1,"data":[{"id":"326610","name":"\u4e09\u661f GALAXY Note II N7100\uff0816GB\uff09","picture":"http:\/\/y1.ifengimg.com\/iproduct\/p1\/pics_big\/240\/ceK6fPqR05Yp6_top.jpg","marketPrice":"4699","score":"9","marketTime":"1349020800","mobileStyle":"3G\u624b\u673a\uff0c\u667a\u80fd\u624b\u673a\uff0c\u62cd\u7167\u624b\u673a\uff0c4G\u624b\u673a\uff0c\u5e73\u677f\u624b\u673a","screenSize":"5.500","isTouchScreen":"\u7535\u5bb9\u5c4f\uff0c\u591a\u70b9\u89e6\u63a7","screeType":"HD Super AMOLED","screeLong":"1280x720\u50cf\u7d20","networkType":"\u5355\u5361\u591a\u6a21","GSM":"GSM\uff0cWCDMA\uff0cLTE","dataService":"GPRS\uff0cEDGE\uff0cHSPA","supportfrequency":"2G\uff1aGSM 850\/900\/1800\/1900\r\n3G\uff1aWCDMA 850\/900\/1900\/2100","KeyboardType":"\u865a\u62dfQWERTY\u952e\u76d8","color":"\u4e91\u77f3\u767d\u8272\uff0c\u949b\u91d1\u7070\u8272\uff0c\u94bb\u77f3\u7c89\u8272","long":"151.1x80.5x9.4mm","weight":"180g"}],"msg":null}
最后需要解析json的数据,参考了下面这两篇文章:
http://blog.chinaunix.net/uid-25885064-id-3402449.html
http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/23/3096001.html
接口名称 | 手机对比 | ||
接口示例 | http://digi.tech.ifeng.com/mobile/compare/api?ids=id1_id2_id3 | ||
接口描述 | 使用手机id进行参数对比 | ||
接口协议 | HTTP[POST] | ||
参数说明 | ids | 要对比的手机id序列,用下划线_分隔 | 必填 |
返回值约定(json格式) | 注册失败时,显示相应的失败信息 | 成功时:{"code":1,"message":"\u64cd\u4f5c\u6210\u529f","data":["id1":{“name”:”iphone5”,”price”:4570,”pic”: “http://y1.ifengimg.com/iproduct/p1/top/pics/753/ceLBceL0LElec_top.jpg ”},“id2”:{“name”: “iphone5”, “price”:4570, “pic”: “http://y1.ifengimg.com/iproduct/p1/top/pics/753/ceLBceL0LElec_top.jpg ”},“id3”:{“name”: “iphone5”, “price”:4570, “pic”: “http://y1.ifengimg.com/iproduct/p1/top/pics/753/ceLBceL0LElec_top.jpg”}]} | |
备注 | 返回数据说明 | code:接口调用结果 0-失败 1-成功 message:错误信息 data:手机信息数组 包括手机id和多个属性
|