HTTPClient获取接口数据返回JSON

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liyaohui_szz/article/details/80495924
public static String getDataJson(String url, Integer pageNo, Integer pageSize, String search) {
		HttpPost httpPost = null;
		try {
			CloseableHttpClient httpClient = HttpClients.createDefault();
			httpPost = new HttpPost(url);
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();//设置请求和传输超时时间
			httpPost.setConfig(requestConfig);
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			list.add(new BasicNameValuePair("pageNo", pageNo.toString()));
			list.add(new BasicNameValuePair("pageSize", pageSize.toString()));
			list.add(new BasicNameValuePair("search", search));
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"utf-8");
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(httpPost);
			if(response != null){
				
				HttpEntity resEntity = response.getEntity();
				if(resEntity != null){
					return EntityUtils.toString(resEntity,"utf-8");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		}finally {
			httpPost.releaseConnection();
		}
		
		return "";
	}

将字符串转化为JSON取数据:

JSON.parseObject(httpResult.getResultString()).getJSONObject("result").getInteger("data")


展开阅读全文

没有更多推荐了,返回首页