数据集本地有数据,帆软无法展示
package com.fr.io;
import com.alibaba.fastjson.JSONObject;
import com.fr.data.AbstractTableData;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ArrayTableDataDemo extends AbstractTableData {
/**
* 定义程序数据集的列名与数据保存位置
*/
private String columnNames;
private Object rowData;
/**
* 实现构建函数,在构建函数中准备数据
*/
public ArrayTableDataDemo() {
String host = "https://weather01.market.alicloudapi.com";
String path = "/day15";
String method = "GET";
String appcode = "16402e107ea64c5284946ed0e90b9fd5";
Map headers = new HashMap();
headers.put("Authorization", "APPCODE " + appcode);
Map querys = new HashMap();
querys.put("area","宁波");
try {
HttpResponse response = doGet(host, path, method, headers, querys);
String str = EntityUtils.toString(response.getEntity());
JSONObject object = (JSONObject) JSONObject.parse(str);
JSONArray jsonArray = JSONArray.fromObject(object.getJSONObject("showapi_res_body").get("dayList"));
Object datas =new String4];
for (int i = 0; i < jsonArray.size(); i++) {
net.sf.json.JSONObject oct = jsonArray.getJSONObject(i);
datas0]= oct.getString("area");
datas1]= oct.getString("daytime");
datas2]= oct.getString("day_weather_pic");
datas3]= oct.getString("night_air_temperature");
this.rowData = datas;
}
String columnName = new String4];
columnName0]="城市";
columnName1]="时间";
columnName2]="天气";
columnName3]="气温";
this.columnNames = columnName;
} catch (Exception e) {
e.printStackTrace();
}
}
//实现ArrayTableData的其他四个方法,因为AbstractTableData已经实现了hasRow方法
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int columnIndex) {
return columnNames;
}
@Override
public int getRowCount() {
return rowData.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return rowData;
}
public static HttpResponse doGet(String host, String path, String method, Map headers, Map querys) throws Exception {
HttpClient httpClient = wrapClient(host);
HttpGet request = new HttpGet(buildUrl(host, path, querys));
Iterator var7 = headers.entrySet().iterator();
while (var7.hasNext()) {
Map.Entry e = (Map.Entry) var7.next();
request.addHeader((String) e.getKey(), (String) e.getValue());
}
return httpClient.execute(request);
}
private static HttpClient wrapClient(String host) {
HttpClient httpClient = new DefaultHttpClient();
if (host.startsWith("https://")) {
sslClient(httpClient);
}
return httpClient;
}
private static void sslClient(HttpClient httpClient) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public X509Certificate getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate xcs, String str) {
}
public void checkServerTrusted(X509Certificate xcs, String str) {
}
};
ctx.init((KeyManager) null, new TrustManager{tm}, (SecureRandom) null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = httpClient.getConnectionManager();
SchemeRegistry registry = ccm.getSchemeRegistry();
registry.register(new Scheme("https", 443, ssf));
} catch (KeyManagementException var6) {
throw new RuntimeException(var6);
} catch (NoSuchAlgorithmException var7) {
throw new RuntimeException(var7);
}
}
private static String buildUrl(String host, String path, Map querys) throws UnsupportedEncodingException {
StringBuilder sbUrl = new StringBuilder();
sbUrl.append(host);
if (!StringUtils.isBlank(path)) {
sbUrl.append(path);
}
if (null != querys) {
StringBuilder sbQuery = new StringBuilder();
Iterator var5 = querys.entrySet().iterator();
while (var5.hasNext()) {
Map.Entry query = (Map.Entry) var5.next();
if (0 < sbQuery.length()) {
sbQuery.append("&");
}
if (StringUtils.isBlank((String) query.getKey()) && !StringUtils.isBlank((String) query.getValue())) {
sbQuery.append((String) query.getValue());
}
if (!StringUtils.isBlank((String) query.getKey())) {
sbQuery.append((String) query.getKey());
if (!StringUtils.isBlank((String) query.getValue())) {
sbQuery.append("=");
sbQuery.append(URLEncoder.encode((String) query.getValue(), "utf-8"));
}
}
}
if (0 < sbQuery.length()) {
sbUrl.append("?").append(sbQuery);
}
}
return sbUrl.toString();
}
}