请求地址由参数加参数签名形式生成,例如:
1.构建url
1)首先构建参数对
public static HashMap<String, String> cinemas() { HashMap<String, String> params = new HashMap<String, String>(); params.put("method", "getCinemas"); params.put("uid", StartTest.USERNAME); return params; }
2)生成包含参数签名的参数对
public static List<NameValuePair> getFormparams (HashMap<String, String> params) { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); for (Entry<String, String> e : params.entrySet()) { formparams.add(new BasicNameValuePair(e.getKey(), e.getValue())); } formparams.add(new BasicNameValuePair("time_stamp", String.valueOf(new Date().getTime()))); formparams.add(new BasicNameValuePair("enc", GetENC.getEnc(formparams, StartTest.PASSWORD))); return formparams; }
3)生成请求URL
public static URI getUrl(List<NameValuePair> formparams, String HOST, String PATH) { URI uri = null; try { uri = new URIBuilder().setScheme("http").setHost(HOST) .setPath(PATH).setParameters(formparams).build(); } catch (URISyntaxException e) { e.printStackTrace(); } return uri; }
2.构建完URL后发送请求
public static HttpEntity getEntity(HashMap<String, String> params, String HOST, String PATH) { HttpEntity entity = null; CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 创建httpget请求. HttpGet httpget = new HttpGet(getUrl(getFormparams(params), HOST, PATH)); // 执行get请求 CloseableHttpResponse response = httpclient.execute(httpget); try { // 将响应放入entity载体 entity = response.getEntity(); if (entity != null) { entity = new BufferedHttpEntity(entity); if(StartTest.log) { ResultsLog.writefile(EntityUtils.toString(entity, "UTF-8"), StartTest.path); } } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 断开连接 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return entity; }
3.解析目标JSON数据,返回目标数组
public static String[] targetArray(String sourcejson, String targetfield) { String targetArray[] = null; JSONObject js = JSONObject.fromObject(sourcejson); if (sourcejson.contains("data")) { JSONArray datajsonArray = js.getJSONArray("data"); targetArray = new String[datajsonArray.size()]; for(int i=0; i<datajsonArray.size(); i++){ targetArray[i] = (JSONObject.fromObject(datajsonArray.getString(i))).getString(targetfield); } } else { targetArray[0] = js.getString("errCode") + "----" + js.getString("errMsg"); } return targetArray; }
4.执行请求URL并解析返回JSON数据,获取目标值构成数组
public static String[] targetValueArray(HashMap<String, String> params, String targetfield) { String targetarray[] = null; HttpEntity entity = GetPost.getEntity(params, StartTest.ROUND_HOST, StartTest.ROUND_PATH); try { targetarray = JSONParsing.targetArray( EntityUtils.toString(entity, "UTF-8"), targetfield); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return targetarray; }
至此从构建URL-->发送get请求-->解析JSON数据获取目标值已完成
时间: 2024-10-30 16:09:43