From 69be0c48fbe169ef30ca6cb1d1d0280c79aa4fc7 Mon Sep 17 00:00:00 2001 From: seven332 Date: Thu, 7 Apr 2016 02:23:10 +0800 Subject: [PATCH] Put save parse error to AppConfig --- .../java/com/hippo/ehviewer/AppConfig.java | 33 ++++++++++ .../com/hippo/ehviewer/client/EhEngine.java | 61 +++++++++---------- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/AppConfig.java b/app/src/main/java/com/hippo/ehviewer/AppConfig.java index 5dd05e0bf..cb4df0d15 100644 --- a/app/src/main/java/com/hippo/ehviewer/AppConfig.java +++ b/app/src/main/java/com/hippo/ehviewer/AppConfig.java @@ -20,9 +20,15 @@ import android.content.Context; import android.os.Environment; import android.support.annotation.Nullable; +import com.hippo.ehviewer.client.exception.ParseException; +import com.hippo.util.ReadableTime; import com.hippo.yorozuya.FileUtils; +import com.hippo.yorozuya.IOUtils; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; public class AppConfig { @@ -129,4 +135,31 @@ public class AppConfig { public static File createTempFile() { return FileUtils.createTempFile(getTempDir(), null); } + + public static void saveParseErrorBody(ParseException e) { + File dir = getExternalParseErrorDir(); + if (null == dir) { + return; + } + + File file = new File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".txt"); + OutputStream os = null; + try { + os = new FileOutputStream(file); + String message = e.getMessage(); + String body = e.getBody(); + if (null != message) { + os.write(message.getBytes("utf-8")); + os.write('\n'); + } + if (null != body) { + os.write(body.getBytes("utf-8")); + } + os.flush(); + } catch (IOException e1) { + // Ignore + } finally { + IOUtils.closeQuietly(os); + } + } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java index 9826b9417..7260e586e 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -36,6 +36,7 @@ import com.hippo.ehviewer.client.parser.ForumsParser; import com.hippo.ehviewer.client.parser.GalleryApiParser; import com.hippo.ehviewer.client.parser.GalleryDetailParser; import com.hippo.ehviewer.client.parser.GalleryListParser; +import com.hippo.ehviewer.client.parser.GalleryPageParser; import com.hippo.ehviewer.client.parser.GalleryTokenApiParser; import com.hippo.ehviewer.client.parser.ProfileParser; import com.hippo.ehviewer.client.parser.RateGalleryParser; @@ -44,9 +45,7 @@ import com.hippo.ehviewer.client.parser.TorrentParser; import com.hippo.ehviewer.client.parser.VoteCommentParser; import com.hippo.ehviewer.client.parser.WhatsHotParser; import com.hippo.network.StatusCodeException; -import com.hippo.util.ReadableTime; import com.hippo.yorozuya.AssertUtils; -import com.hippo.yorozuya.IOUtils; import org.json.JSONArray; import org.json.JSONObject; @@ -54,9 +53,6 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; import java.util.ArrayList; import java.util.List; @@ -87,33 +83,6 @@ public class EhEngine { sEhFilter = EhFilter.getInstance(); } - private static void saveParseErrorBody(ParseException e) { - File dir = AppConfig.getExternalParseErrorDir(); - if (null == dir) { - return; - } - - File file = new File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".txt"); - OutputStream os = null; - try { - os = new FileOutputStream(file); - String message = e.getMessage(); - String body = e.getBody(); - if (null != message) { - os.write(message.getBytes("utf-8")); - os.write('\n'); - } - if (null != body) { - os.write(body.getBytes("utf-8")); - } - os.flush(); - } catch (IOException e1) { - // Ignore - } finally { - IOUtils.closeQuietly(os); - } - } - private static void throwException(Call call, int code, @Nullable Headers headers, @Nullable String body, Exception e) throws Exception { if (call.isCanceled()) { @@ -132,7 +101,7 @@ public class EhEngine { throw new EhException(body); } else { if (Settings.getSaveParseErrorBody()) { - saveParseErrorBody((ParseException) e); + AppConfig.saveParseErrorBody((ParseException) e); } throw new EhException(GetText.getString(R.string.error_parse_error)); } @@ -834,4 +803,30 @@ public class EhEngine { return result; } + + public static GalleryPageParser.Result getGalleryPage(@Nullable EhClient.Task task, + OkHttpClient okHttpClient, String url) throws Exception { + Log.d(TAG, url); + Request request = new EhRequestBuilder(url, null != task ? task.getEhConfig() : Settings.getEhConfig()).build(); + Call call = okHttpClient.newCall(request); + + // Put call + if (null != task) { + task.setCall(call); + } + + String body = null; + Headers headers = null; + int code = -1; + try { + Response response = call.execute(); + code = response.code(); + headers = response.headers(); + body = response.body().string(); + return GalleryPageParser.parse(body); + } catch (Exception e) { + throwException(call, code, headers, body, e); + throw e; + } + } }