目录
如何调用http接口
方法有很多
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
/**
* @author: wangmingxin02
* @create: 2019-02-25
**/
public class HttpUtil {
public static String get(String url) {
HttpURLConnection connection = null;
String response = "";
int code = -1;
try {
connection = (HttpURLConnection) new URL(url).openConnection();
if (connection != null) {
connection.setRequestMethod("GET");
response = readStream(connection);
code = connection.getResponseCode();
} else {
}
} catch (Exception e) {
} finally {
if (connection != null) {
try {
connection.disconnect();
} catch (Exception e) {
}
}
}
return response;
}
private static String readStream(HttpURLConnection connection) throws IOException {
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
String result = null;
StringBuilder sb = new StringBuilder();
InputStream is = null;
try {
is = new BufferedInputStream(connection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("utf-8")));
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
result = sb.toString();
} catch (Exception e) {
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
return result;
}
}
解析json串
http接口返回json串
解析json串获取自己需要的信息
推荐使用阿里的fastjson
https://github.com/alibaba/fastjson
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
常用api
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。
Joda Time
https://www.joda.org/joda-time/
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
Joda-Time provides a quality replacement for the Java date and time classes.
Joda-Time提供了很多方便的日期时间的工具类(转换、计算等)
举个例子,可以做时间生成器
//给一个起始时间和结束时间,生成之间的所有时间
public static List<DateTime> getDateRange(DateTime start, DateTime end) {
List<DateTime> ret = new ArrayList<DateTime>();
DateTime tmp = start;
while(tmp.isBefore(end) || tmp.equals(end)) {
ret.add(tmp);
tmp = tmp.plusDays(1);
}
return ret;
}
统计接口的平均耗时
@Test
public void testMethodCost() throws Exception {
int count = 0;
int costSum = 0;
while(true) {
long start = System.currentTimeMillis();
// ---调用需要统计的接口; ---
long end = System.currentTimeMillis();
long cost = end-start;
System.out.println("cost="+cost);
System.out.println("ret="+ret);
Thread.sleep(200);
costSum+=cost;
count++;
if(count == 1000){
break;
}
}
System.out.println("接口平均耗时:"+costSum/count);
}
循环调用接口1000次,每次沉睡200ms
git push的小技巧
分支 feature/a 拉到本地,做代码修改,但是不直接push到远端的这个分支,
push到基于这个远端分支拉取的新分支
git push origin feature/a:feature/a1
注意:如果远端没有a1分支,会自动帮你拉取一个分支,如果已经存在,则直接push过去
maven做单元测试
用一个插件
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>false</skipTests>
<includes>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
include测试包含的类
maven-surefire-plugin 的test目标会自动执行测试源码路径下符合命名模式的测试类。
默认测试源代码路径: src/test/java/
终端输入mvn test 就会自动执行test目录下的所有匹配的测试类
跑完所有的测试类,会给你一个结果,跑了多少个testcase,出错了几个,失败了几个,哪一个方法的哪一行出错,
并会生成log文件。
说点什么
您将是第一位评论人!