空值异常

This commit is contained in:
禾几海
2020-08-01 23:56:10 +08:00
parent 4a65df2a7d
commit 49ee7c73e2
2 changed files with 12 additions and 47 deletions

View File

@@ -15,11 +15,10 @@ import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@@ -88,15 +87,17 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("lastUpdateTime", DateFormatUtil.get(webUpdateInfoMapper.getLastestOne().getUpdateTime())); jsonObject.put("lastUpdateTime", DateFormatUtil.get(webUpdateInfoMapper.getLastestOne().getUpdateTime()));
jsonObject.put("lastUpdateInfo", webUpdateInfoMapper.getLastestOne().getUpdateInfo()); jsonObject.put("lastUpdateInfo", webUpdateInfoMapper.getLastestOne().getUpdateInfo());
JSONArray array = JSONArray.fromObject(HttpUtil.get("https://api.github.com/repos/xiaohai2271/blog-frontEnd/commits?page=1&per_page=1")); String str = HttpUtil.get("https://api.github.com/repos/xiaohai2271/blog-frontEnd/commits?page=1&per_page=1");
JSONObject object = array.getJSONObject(0); if (!StringUtils.isEmpty(str)) {
JSONObject commit = object.getJSONObject("commit"); JSONArray array = JSONArray.fromObject(str);
jsonObject.put("lastCommit", commit.getString("message")); JSONObject object = array.getJSONObject(0);
jsonObject.put("committerAuthor", commit.getJSONObject("committer").getString("name")); JSONObject commit = object.getJSONObject("commit");
SimpleDateFormat sdf = new SimpleDateFormat(); jsonObject.put("lastCommit", commit.getString("message"));
Instant parse = Instant.parse(commit.getJSONObject("committer").getString("date")); jsonObject.put("committerAuthor", commit.getJSONObject("committer").getString("name"));
jsonObject.put("committerDate", DateFormatUtil.get(Date.from(parse))); Instant parse = Instant.parse(commit.getJSONObject("committer").getString("date"));
jsonObject.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + object.getString("sha")); jsonObject.put("committerDate", DateFormatUtil.get(Date.from(parse)));
jsonObject.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + object.getString("sha"));
}
return jsonObject; return jsonObject;
} }

View File

@@ -9,7 +9,6 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Objects; import java.util.Objects;
/** /**
@@ -20,41 +19,6 @@ import java.util.Objects;
public class HttpUtil { public class HttpUtil {
private static final OkHttpClient CLIENT = new OkHttpClient(); private static final OkHttpClient CLIENT = new OkHttpClient();
/*public static String get(String urlStr) throws IOException {
StringBuffer sb = new StringBuffer();
HttpURLConnection urlConnection = null;
try {
URL url = new URL(urlStr);
//打开http
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
try (
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)
) {
//将bufferReader的值给放到buffer里
String str = null;
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
}
} finally {
//断开连接
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return sb.toString();
}
*/
public static String get(String urlStr) { public static String get(String urlStr) {
Request request = new Request.Builder() Request request = new Request.Builder()
.url(urlStr) .url(urlStr)