时间信息实现
由于每次访问都有获取token,所以就写了一个工具类
String getToken(String xh,String pwd) 获取token值
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
public class Data {
public String getToken(String xh,String pwd) throws URISyntaxException, IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder("http://zswxyjw.minghuaetc.com/znlykjdxswxy/app.do");
uriBuilder.setParameter("method","authUser").setParameter("xh",xh).setParameter("pwd",pwd);
HttpGet httpGet = new HttpGet(uriBuilder.build());
CloseableHttpResponse response = httpClient.execute(httpGet);
String s = null;
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
s = EntityUtils.toString(entity, "utf-8");
}
JSONObject obj = new JSONObject();
obj = obj.fromObject(s);
Object token = obj.get("token");
return token.toString();
}
}
数据的获取
强智的API返回的是一个JOSN文件,通过火狐浏览器可以直接解析
- 代码实现
1.导入jar包
2.代码实现
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
s = EntityUtils.toString(entity, "utf-8");
}
JSONObject obj = new JSONObject();
obj = obj.fromObject(s);
Object token = obj.get("token");
我创建的是Maven项目,所以jar包只需要在配置文件中加入配置信息即可
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.3</version>
</dependency>
时间信息的具体实现
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
public class Test2 {
public static void main(String[] args) throws URISyntaxException, IOException {
String token = new Data().getToken("学号","密码");
CloseableHttpClient httpClient = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder("http://zswxyjw.minghuaetc.com/znlykjdxswxy/app.do");
uriBuilder.setParameter("method","getCurrentTime").setParameter("currDate","2019-05-27");//YYYY-MM_DD 这里特别注意,MM DD一定要两位数,不足前面补零
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.setHeader("token",token);
CloseableHttpResponse response = httpClient.execute(httpGet);
String s = null;
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
s = EntityUtils.toString(entity, "utf-8");
System.out.println(s);
}
}
}
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NTkwMzI3MDMsImF1ZCI6IjIwMTc3NTgzIn0.i0PW59S18U7WDk7wyKKZgEa3e2o3fQwi_XeIxipSM9U
GET
http://zswxyjw.minghuaetc.com/znlykjdxswxy/app.do?method=getCurrentTime&currDate=2019-05-27
HTTP/1.1
{“zc”:14,“e_time”:“2019-06-01”,“s_time”:“2019-05-26”,“xnxqh”:“2018-2019-2”}