Add save html content when parse error

This commit is contained in:
seven332 committed 2016-03-13 19:46:20 +08:00
1 parent be8f78fa4f
commit 5fe5737769
17 files changed
+175 -10

No files matched your search

@@ -32,6 +32,7 @@ public class AppConfig {
private static final String TEMP = "temp";
private static final String IMAGE = "image";
private static final String CRASH = "crash";
private static final String PARSE_ERROR = "parse_error";
private static Context sContext;
@@ -91,6 +92,11 @@ public class AppConfig {
return getDirInExternalAppDir(CRASH);
}
@Nullable
public static File getExternalParseErrorDir() {
return getDirInExternalAppDir(PARSE_ERROR);
}
@Nullable
public static File createExternalTempFile() {
return FileUtils.createTempFile(getExternalTempDir(), null);
@@ -99,10 +99,6 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx
Analytics.start(this);
}
if (BETA) {
Settings.putBetaUpdateChannel(true);
}
// Update version code
try {
PackageInfo pi= getPackageManager().getPackageInfo(getPackageName(), 0);
@@ -539,14 +539,28 @@ public class Settings {
****** Crash
********************/
private static final String KEY_CRASH_FILENAME = "crash_filename";
private static final String VALUE_CRASH_FILENAME = null;
private static final String DEFAULT_CRASH_FILENAME = null;
public static String getCrashFilename() {
return getString(KEY_CRASH_FILENAME, VALUE_CRASH_FILENAME);
return getString(KEY_CRASH_FILENAME, DEFAULT_CRASH_FILENAME);
}
@SuppressLint("CommitPrefEdits")
public static void putCrashFilename(String value) {
sSettingsPre.edit().putString(KEY_CRASH_FILENAME, value).commit();
}
/********************
****** Advanced
********************/
public static final String KEY_SAVE_PARSE_ERROR_BODY = "save_parse_error_body";
private static final boolean DEFAULT_SAVE_PARSE_ERROR_BODY = EhApplication.BETA;
public static boolean getSaveParseErrorBody() {
return getBoolean(KEY_SAVE_PARSE_ERROR_BODY, DEFAULT_SAVE_PARSE_ERROR_BODY);
}
public static void putSaveParseErrorBody(boolean value) {
putBoolean(KEY_SAVE_PARSE_ERROR_BODY, value);
}
}
@@ -20,8 +20,10 @@ import android.support.annotation.Nullable;
import android.util.Log;
import android.util.Pair;
import com.hippo.ehviewer.AppConfig;
import com.hippo.ehviewer.GetText;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.Settings;
import com.hippo.ehviewer.client.data.GalleryComment;
import com.hippo.ehviewer.client.data.GalleryDetail;
import com.hippo.ehviewer.client.data.GalleryInfo;
@@ -41,11 +43,17 @@ import com.hippo.ehviewer.client.parser.SignInParser;
import com.hippo.ehviewer.client.parser.TorrentParser;
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;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
@@ -71,6 +79,33 @@ public class EhEngine {
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
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()) {
@@ -88,6 +123,9 @@ public class EhEngine {
if (body != null && !body.contains("<")){
throw new EhException(body);
} else {
if (Settings.getSaveParseErrorBody()) {
saveParseErrorBody((ParseException) e);
}
throw new EhException(GetText.getString(R.string.error_parse_error));
}
}
@@ -34,6 +34,7 @@ import android.widget.ListAdapter;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.ui.fragment.AboutFragment;
import com.hippo.ehviewer.ui.fragment.AdvancedFragment;
import com.hippo.ehviewer.ui.fragment.DownloadFragment;
import com.hippo.ehviewer.ui.fragment.EhFragment;
import com.hippo.ehviewer.ui.fragment.ReadFragment;
@@ -50,6 +51,7 @@ public final class SettingsActivity extends TrackedPreferenceActivity {
EhFragment.class.getName(),
ReadFragment.class.getName(),
DownloadFragment.class.getName(),
AdvancedFragment.class.getName(),
AboutFragment.class.getName(),
};
@@ -43,7 +43,6 @@ public class AboutFragment extends PreferenceFragment
private static final String KEY_LICENSE = "license";
private static final String KEY_DONATE = "donate";
private static final String KEY_CHECK_FOR_UPDATES = "check_for_updates";
private static final String KEY_BETA_UPDATE_CHANNEL = "beta_update_channel";
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -59,10 +58,8 @@ public class AboutFragment extends PreferenceFragment
Preference license = findPreference(KEY_LICENSE);
Preference donate = findPreference(KEY_DONATE);
Preference checkForUpdate = findPreference(KEY_CHECK_FOR_UPDATES);
Preference betaUpdateChannel = findPreference(KEY_BETA_UPDATE_CHANNEL);
author.setSummary(getString(R.string.settings_about_author_summary).replace('$', '@'));
betaUpdateChannel.setDefaultValue(EhApplication.BETA);
author.setOnPreferenceClickListener(this);
googlePlus.setOnPreferenceClickListener(this);
@@ -0,0 +1,31 @@
/*
* Copyright 2016 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui.fragment;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import com.hippo.ehviewer.R;
public class AdvancedFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.advanced_settings);
}
}