package fuckTest;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.poi.ss.formula.functions.T;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class httpTest {
public static void main(String[] args) {
String url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php";
Map<String, Object> map = new HashMap<>();
map.put("resource_id", "6899");
map.put("query", "失信被执行人名单");
map.put("cardNum", "");
map.put("iname", "李小明");
map.put("areaName", "湖北");
map.put("ie", "utf-8");
map.put("oe", "utf-8");
map.put("format", "json");
map.put("t", "1524537973200");
map.put("cb", "jQuery110207319777455577083_1524537959352");
map.put("_", "1524537959354");
String str= httpClientGet(url, map, "UTF-8");
str=str.substring(str.indexOf("(")+1,str.lastIndexOf(");"));
Gson gson = new Gson();
Map<Object, Object> firstMap = gson.fromJson(str, HashMap.class);
List<Map<String,Map<String,Object>>> list = (List<Map<String, Map<String, Object>>>) firstMap.get("data");
if (list != null && list.size() > 0) {
Map<String,Map<String,Object>> data=list.get(0);
List<Map<String, Object>> result = (List<Map<String, Object>>) data.get("result");
List<DiscreditData> disList = new ArrayList<>();
for (Map<String, Object> maps : result) {
DiscreditData discreditData = new DiscreditData();
discreditData.setIname((String) maps.get("iname"));
discreditData.setPublishDate((String) maps.get("publishDate"));
discreditData.setCourtName((String) maps.get("courtName"));
discreditData.setAreaName((String) maps.get("areaName"));
discreditData.setCaseCode((String) maps.get("caseCode"));
discreditData.setDuty((String) maps.get("duty"));
discreditData.setPerformance((String) maps.get("performance"));
discreditData.setDisruptTypeName((String) maps.get("disruptTypeName"));
disList.add(discreditData);
}
System.out.println(disList.toString());
}
}
/**
* @Description:使用HttpClient发送get请求
*/
public static String httpClientGet(String urlParam, Map<String, Object> params, String charset) {
StringBuffer resultBuffer = null;
HttpClient client = new DefaultHttpClient();
BufferedReader br = null;
// 构建请求参数
StringBuffer sbParams = new StringBuffer();
if (params != null && params.size() > 0) {
for (Entry<String, Object> entry : params.entrySet()) {
sbParams.append(entry.getKey());
sbParams.append("=");
try {
sbParams.append(URLEncoder.encode(String.valueOf(entry.getValue()), charset));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
sbParams.append("&");
}
}
if (sbParams != null && sbParams.length() > 0) {
urlParam = urlParam + "?" + sbParams.substring(0, sbParams.length() - 1);
}
HttpGet httpGet = new HttpGet(urlParam);
try {
HttpResponse response = client.execute(httpGet);
// 读取服务器响应数据
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String temp;
resultBuffer = new StringBuffer();
while ((temp = br.readLine()) != null) {
resultBuffer.append(temp);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
br = null;
throw new RuntimeException(e);
}
}
}
return resultBuffer.toString();
}
}