From 49ee7c73e2b557c59ccbe4ff2a9c07fae69ce290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A6=BE=E5=87=A0=E6=B5=B7?= Date: Sat, 1 Aug 2020 23:56:10 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A9=BA=E5=80=BC=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serviceimpl/WebUpdateInfoServiceImpl.java | 23 ++++++------ .../java/cn/celess/blog/util/HttpUtil.java | 36 ------------------- 2 files changed, 12 insertions(+), 47 deletions(-) diff --git a/src/main/java/cn/celess/blog/service/serviceimpl/WebUpdateInfoServiceImpl.java b/src/main/java/cn/celess/blog/service/serviceimpl/WebUpdateInfoServiceImpl.java index 17b53f6..640daad 100644 --- a/src/main/java/cn/celess/blog/service/serviceimpl/WebUpdateInfoServiceImpl.java +++ b/src/main/java/cn/celess/blog/service/serviceimpl/WebUpdateInfoServiceImpl.java @@ -15,11 +15,10 @@ import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONArray; import net.sf.json.JSONObject; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.IOException; -import java.text.SimpleDateFormat; import java.time.Instant; import java.util.ArrayList; import java.util.Date; @@ -88,15 +87,17 @@ public class WebUpdateInfoServiceImpl implements WebUpdateInfoService { JSONObject jsonObject = new JSONObject(); jsonObject.put("lastUpdateTime", DateFormatUtil.get(webUpdateInfoMapper.getLastestOne().getUpdateTime())); 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")); - JSONObject object = array.getJSONObject(0); - JSONObject commit = object.getJSONObject("commit"); - jsonObject.put("lastCommit", commit.getString("message")); - jsonObject.put("committerAuthor", commit.getJSONObject("committer").getString("name")); - SimpleDateFormat sdf = new SimpleDateFormat(); - Instant parse = Instant.parse(commit.getJSONObject("committer").getString("date")); - jsonObject.put("committerDate", DateFormatUtil.get(Date.from(parse))); - jsonObject.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + object.getString("sha")); + String str = HttpUtil.get("https://api.github.com/repos/xiaohai2271/blog-frontEnd/commits?page=1&per_page=1"); + if (!StringUtils.isEmpty(str)) { + JSONArray array = JSONArray.fromObject(str); + JSONObject object = array.getJSONObject(0); + JSONObject commit = object.getJSONObject("commit"); + jsonObject.put("lastCommit", commit.getString("message")); + jsonObject.put("committerAuthor", commit.getJSONObject("committer").getString("name")); + Instant parse = Instant.parse(commit.getJSONObject("committer").getString("date")); + jsonObject.put("committerDate", DateFormatUtil.get(Date.from(parse))); + jsonObject.put("commitUrl", "https://github.com/xiaohai2271/blog-frontEnd/tree/" + object.getString("sha")); + } return jsonObject; } diff --git a/src/main/java/cn/celess/blog/util/HttpUtil.java b/src/main/java/cn/celess/blog/util/HttpUtil.java index 8dcc7fb..a720f38 100644 --- a/src/main/java/cn/celess/blog/util/HttpUtil.java +++ b/src/main/java/cn/celess/blog/util/HttpUtil.java @@ -9,7 +9,6 @@ import okhttp3.Request; import okhttp3.Response; import java.io.IOException; -import java.net.MalformedURLException; import java.util.Objects; /** @@ -20,41 +19,6 @@ import java.util.Objects; public class HttpUtil { 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) { Request request = new Request.Builder() .url(urlStr)