java使用http获取 GET请求接口数据代码

一,postman截图:

二,java代码:

package com.cxqy.officialserver.dto.personalsub;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
* @Author y
* @Date 2022/6/14 15:16
* @Version 1.0
*/

public class GoodsPa {

private String url;
public GoodsPa(String url) {
this.url = url;
}

public void process() {
String res = HttpClientUtils.get((this.url));


JSONObject object = JSONObject.parseObject(res);
System.out.println("object------->"+object);

//里因listDataMap中是数组的形式,所以要JSONArray
JSONArray listDataMap = (JSONArray)getValue(object,"list");

//取出数组中第一个值里面所需要的key对应value

System.out.println(getValue(listDataMap.getJSONObject(0),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(0),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(1),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(1),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(2),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(2),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(3),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(3),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(4),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(4),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(5),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(5),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(6),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(6),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(7),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(7),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(8),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(8),"thumb"));
System.out.println(getValue(listDataMap.getJSONObject(9),"title"));
// System.out.println(getValue(listDataMap.getJSONObject(9),"thumb"));



// List<JSONObject> items = (List<JSONObject>)((JSONObject)object.get("list")).get("title");
// for(JSONObject item : items){
// this.urlList.add(item.getString("title"));
// }


}

public static Object getValue(JSONObject json, String key){
if(json ==null){
return null;
}else {
if(json.isEmpty()){
return null;
}else {
return json.get(key);
}
}
}

public static void main(String[] args) {
String url = "https://new.yunpinshop.com/app/ewei_shopv2_api.php?i=2&r=goods.get_list&page=132&comefrom=wxapp&openid=sns_wa_&mid=110454&merchid=&authkey=&timestamp=1655188745941";
GoodsPa processor = new GoodsPa(url);

processor.process();


}
}


package com.cxqy.officialserver.dto.personalsub;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author yjl
* @Date 2021/12/31 14:56
* @Version 1.0
*/



public class HttpClientUtils {

public static Map<String, List<String>> convertHeaders(Header[] headers) {
Map<String, List<String>> results = new HashMap<String, List<String>>();
for (Header header : headers) {
List<String> list = results.get(header.getName());
if (list == null) {
list = new ArrayList<String>();
results.put(header.getName(), list);
}
list.add(header.getValue());
}
return results;
}

/**
* httpget
* @param url
*/
public static String get(String url) {
return get(url, "UTF-8");
}

public static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);

/**
* httpget
* @param url
*/
public static String get(String url, String charset) {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Connection","keep-alive");
httpGet.setHeader("Host","new.yunpinshop.com");
httpGet.setHeader("Connection","keep-alive");
httpGet.setHeader("Accept","*/*");
return executeRequest(httpGet, charset);
}

/**
* httpget求,增加头参数
* @param url
*/
public static String ajaxGet(String url) {
return ajaxGet(url, "UTF-8");
}

/**
* httpget求,增加头参数
*
* @param url
*/
public static String ajaxGet(String url, String charset) {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("X-Requested-With", "XMLHttpRequest");
return executeRequest(httpGet, charset);
}

/**
* @param url
* @return
*/
public static String ajaxGet(CloseableHttpClient httpclient, String url) {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("X-Requested-With", "XMLHttpRequest");
return executeRequest(httpclient, httpGet, "UTF-8");
}

/**
* httppost求,传递map格式参数
*/
public static String post(String url, Map<String, String> dataMap) {
return post(url, dataMap, "UTF-8");
}

/**
* httppost求,传递map格式参数
*/
public static String post(String url, Map<String, String> dataMap, String charset) {
HttpPost httpPost = new HttpPost(url);
try {
if (dataMap != null) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset);
formEntity.setContentEncoding(charset);
httpPost.setEntity(formEntity);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return executeRequest(httpPost, charset);
}

/**
* httppost求,增加头参数传递map格式参数
*/
public static String ajaxPost(String url, Map<String, String> dataMap) {
return ajaxPost(url, dataMap, "UTF-8");
}

/**
* httppost求,增加头参数传递map格式参数
*/
public static String ajaxPost(String url, Map<String, String> dataMap, String charset) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
try {
if (dataMap != null) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset);
formEntity.setContentEncoding(charset);
httpPost.setEntity(formEntity);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return executeRequest(httpPost, charset);
}

/**
* httppost求,增加头参数传递json格式参数
*/
public static String ajaxPostJson(String url, String jsonString) {
return ajaxPostJson(url, jsonString, "UTF-8");
}

/**
* httppost求,增加头参数传递json格式参数
*/
public static String ajaxPostJson(String url, String jsonString, String charset) {
HttpPost httpPost = new HttpPost(url); httpPost.setHeader
("X-Requested-With", "XMLHttpRequest"); StringEntity stringEntity =

new StringEntity(jsonString, charset);// 中文乱码问题
stringEntity.setContentEncoding(charset); stringEntity.setContentType
("application/json"); httpPost.setEntity
(stringEntity);
return executeRequest(httpPost, charset);
}

/**
* 行一http求,传递HttpGetHttpPost参数
*/
public static String executeRequest(HttpUriRequest httpRequest) {
return executeRequest(httpRequest, "UTF-8");
}

/**
* 行一http求,传递HttpGetHttpPost参数
*/
public static String executeRequest(HttpUriRequest httpRequest, String charset) {
CloseableHttpClient httpclient;
if ("https".equals(httpRequest.getURI().getScheme())) {
httpclient = createSSLInsecureClient();
} else {
httpclient = HttpClients.createDefault();
}
String result = "";
try {
try {
CloseableHttpResponse response = httpclient.execute(httpRequest); HttpEntity entity =
null;
try {
entity = response.getEntity(); result = EntityUtils.
toString(entity, charset);
} finally {
EntityUtils.consume(entity); response.close
();
}
} finally {
httpclient.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}

public static String executeRequest(CloseableHttpClient httpclient, HttpUriRequest httpRequest, String charset) {
String result = "";
try {
try {
CloseableHttpResponse response = httpclient.execute(httpRequest); HttpEntity entity =
null;
try {
entity = response.getEntity(); result = EntityUtils.
toString(entity, charset);
} finally {
EntityUtils.consume(entity); response.close
();
}
} finally {
httpclient.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return result;
}

/**
* SSL接,防止证书问题报错sslexception异常!!!!!!
*/
public static CloseableHttpClient createSSLInsecureClient() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build(); SSLConnectionSocketFactory sslsf =
new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (GeneralSecurityException ex) {
throw new RuntimeException(ex);
}
}
}

三,输出结果:

得到筛选后数据。

 

 

 

使用postman循环调用接口插入数据库

 

 

优化代码:

业务层:

 

@Override
public boolean addGoods() {


Integer s = aPcUserMapper.selectLog();
int page = s+1;
if (page <= 160 ){


String url = "https://new.yunpinshop.com/app/ewei_shopv2_api.php?i=2&r=goods.get_list&page="+page+"&comefrom=wxapp&openid=sns_wa_&mid=110454&merchid=&authkey=&timestamp=1655188745941";

String res = HttpClientUtils.get(url);

JSONObject object = JSONObject.parseObject(res);
System.out.println("object------->" + object);
//这里因为listDataMap中是数组的形式,所以要转成JSONArray来取
JSONArray listDataMap = (JSONArray) getValue(object, "list");
//取出数组中第一个值里面所需要的key对应的value


List<GoodsEntity> dd = new ArrayList<>();
GoodsEntity good = new GoodsEntity();
good.setGoodsName((String) getValue(listDataMap.getJSONObject(0), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(0), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(1), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(1), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(2), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(2), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(3), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(3), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(4), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(4), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(5), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(5), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(6), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(6), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(7), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(7), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(8), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(8), "thumb"));
dd.add(good);
aPcUserMapper.addGoods(good);
good.setGoodsName((String) getValue(listDataMap.getJSONObject(9), "title"));
good.setPic((String) getValue(listDataMap.getJSONObject(9), "thumb"));
dd.add(good);
int i = aPcUserMapper.addGoods(good);
if (i>0){
GoodLog goodLog =new GoodLog();
goodLog.setLog(s);
int is = aPcUserMapper.addGoodsLog(goodLog);
if (is > 0){
return true;
}
}

// List<JSONObject> items = (List<JSONObject>)((JSONObject)object.get("list")).get("title");
// for(JSONObject item : items){
// this.urlList.ad\'
//
// '
// uiuiuuid(item.getString("title"));
// }
}
return true;

}

public static Object getValue (JSONObject json, String key){
if (json == null) {
return null;
} else {
if (json.isEmpty()) {
return null;
} else {
return json.get(key);
}
}
}

 



 
posted @ 2022-06-14 17:12  风骚羊肉串  阅读(242)  评论(0编辑  收藏  举报