前言:没有前言,,,,,,哦,可以弄个postman,测试接口可不可以用,安装:https://www.cnblogs.com/mafly/p/postman.html
一、获取assess_token
package 包名;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* 获取token类
*/
public class AuthService {
public static String getAuth() {
// 获取token地址
String authHost = "这里写地址";
try {
URL realUrl = new URL(authHost);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.err.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
/**
* 返回结果示例
*/
System.err.println("result:" + result);
JSONObject jsonObject = new JSONObject(result);
String data = jsonObject.getString("data");
jsonObject = new JSONObject(data);
String access_token = jsonObject.getString("Token");//这个key值看你传来的数据token的key值
return access_token;
} catch (Exception e) {
System.err.printf("获取token失败!");
e.printStackTrace(System.err);
}
return null;
}
}
二、http工具类
package 包名;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* http 工具类
*/
public class HttpUtil {
public static String post(String requestUrl, String accessToken, String params)
throws Exception {
String contentType = "application/x-www-form-urlencoded";
return HttpUtil.post(requestUrl, accessToken, contentType, params);
}
public static String post(String requestUrl, String accessToken, String contentType, String params)
throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
}
public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
throws Exception {
String url = requestUrl + "&accessToken=" + accessToken;
return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
}
public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
throws Exception {
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(params.getBytes(encoding));//这里有时候可以这样out.writeChars(params);
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
in = new BufferedReader(
new InputStreamReader(connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.err.println("result:" + result);
return result;
}
}
三、写个方法调用它们
public object getInfo(){ String accessToken = AuthService.getAuth(); String url = null; String param = "";//这个我没有什么值传,就设为空 try { url = "对应的接口地址";//需要注意的是这个如果在地址有添加参数,需要对参数进行编码转码URLEncoder.encode("参数","UTF-8") //获取接口传来的信息 String result = HttpUtil.post(url, accessToken, param); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
四、剩下的就是对数据的处理,获取出来的数据可能很多,密密麻麻的看得很乱,可以网上找个json格式化工具:http://www.bejson.com/,格式化一下就清晰了,然后可能要对复杂的json数据做处理,可以看一下这篇文章:https://blog.csdn.net/qq_42815754/article/details/83448029,下面是我对应的数据的处理:
//获取接口传来的信息
String result = HttpUtil.post(url, accessToken, param);
JSONObject jsonObject = new JSONObject(result);
//用json取data值
String data = jsonObject.getString("data");
jsonObject = new JSONObject(data);
//再通过data取dataList的json大数组
JSONArray jsonArray = jsonObject.getJSONArray("dataList");
略