improve error reporting a bit
This commit is contained in:
parent
c28adf42b3
commit
4ec76ca88d
@ -51,20 +51,21 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
protected boolean m_canUseProgress = false;
|
protected boolean m_canUseProgress = false;
|
||||||
protected Context m_context;
|
protected Context m_context;
|
||||||
private SharedPreferences m_prefs;
|
private SharedPreferences m_prefs;
|
||||||
|
protected String m_lastErrorMessage;
|
||||||
|
|
||||||
protected ApiError m_lastError;
|
protected ApiError m_lastError;
|
||||||
|
|
||||||
public ApiRequest(Context context) {
|
public ApiRequest(Context context) {
|
||||||
m_context = context;
|
m_context = context;
|
||||||
|
|
||||||
m_prefs = PreferenceManager.getDefaultSharedPreferences(m_context);
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(m_context);
|
||||||
|
|
||||||
m_api = m_prefs.getString("ttrss_url", "").trim();
|
m_api = m_prefs.getString("ttrss_url", "").trim();
|
||||||
m_transportDebugging = m_prefs.getBoolean("transport_debugging", false);
|
m_transportDebugging = m_prefs.getBoolean("transport_debugging", false);
|
||||||
m_lastError = ApiError.NO_ERROR;
|
m_lastError = ApiError.NO_ERROR;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void execute(HashMap<String,String> map) {
|
public void execute(HashMap<String,String> map) {
|
||||||
@ -73,7 +74,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
else
|
else
|
||||||
super.execute(map);
|
super.execute(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getErrorMessage() {
|
public int getErrorMessage() {
|
||||||
switch (m_lastError) {
|
switch (m_lastError) {
|
||||||
case NO_ERROR:
|
case NO_ERROR:
|
||||||
@ -117,7 +118,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
return R.string.error_unknown;
|
return R.string.error_unknown;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected JsonElement doInBackground(HashMap<String, String>... params) {
|
protected JsonElement doInBackground(HashMap<String, String>... params) {
|
||||||
|
|
||||||
@ -125,12 +126,12 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
m_lastError = ApiError.NETWORK_UNAVAILABLE;
|
m_lastError = ApiError.NETWORK_UNAVAILABLE;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
|
|
||||||
String requestStr = gson.toJson(new HashMap<String,String>(params[0]));
|
String requestStr = gson.toJson(new HashMap<String,String>(params[0]));
|
||||||
byte[] postData = null;
|
byte[] postData = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
postData = requestStr.getBytes("UTF-8");
|
postData = requestStr.getBytes("UTF-8");
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
@ -138,50 +139,50 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* disableConnectionReuseIfNecessary(); */
|
/* disableConnectionReuseIfNecessary(); */
|
||||||
|
|
||||||
if (m_transportDebugging) Log.d(TAG, ">>> (" + requestStr + ") " + m_api);
|
if (m_transportDebugging) Log.d(TAG, ">>> (" + requestStr + ") " + m_api);
|
||||||
|
|
||||||
/* ApiRequest.trustAllHosts(m_prefs.getBoolean("ssl_trust_any", false),
|
/* ApiRequest.trustAllHosts(m_prefs.getBoolean("ssl_trust_any", false),
|
||||||
m_prefs.getBoolean("ssl_trust_any_host", false)); */
|
m_prefs.getBoolean("ssl_trust_any_host", false)); */
|
||||||
|
|
||||||
URL url;
|
URL url;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
url = new URL(m_api + "/api/");
|
url = new URL(m_api + "/api/");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
m_lastError = ApiError.INVALID_URL;
|
m_lastError = ApiError.INVALID_URL;
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
String httpLogin = m_prefs.getString("http_login", "").trim();
|
String httpLogin = m_prefs.getString("http_login", "").trim();
|
||||||
String httpPassword = m_prefs.getString("http_password", "").trim();
|
String httpPassword = m_prefs.getString("http_password", "").trim();
|
||||||
|
|
||||||
if (httpLogin.length() > 0) {
|
if (httpLogin.length() > 0) {
|
||||||
if (m_transportDebugging) Log.d(TAG, "Using HTTP Basic authentication.");
|
if (m_transportDebugging) Log.d(TAG, "Using HTTP Basic authentication.");
|
||||||
|
|
||||||
conn.setRequestProperty("Authorization", "Basic " +
|
conn.setRequestProperty("Authorization", "Basic " +
|
||||||
Base64.encodeToString((httpLogin + ":" + httpPassword).getBytes("UTF-8"), Base64.NO_WRAP));
|
Base64.encodeToString((httpLogin + ":" + httpPassword).getBytes("UTF-8"), Base64.NO_WRAP));
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.setDoInput(true);
|
conn.setDoInput(true);
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
conn.setUseCaches(false);
|
conn.setUseCaches(false);
|
||||||
conn.setRequestMethod("POST");
|
conn.setRequestMethod("POST");
|
||||||
conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
|
conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
|
||||||
|
|
||||||
OutputStream out = conn.getOutputStream();
|
OutputStream out = conn.getOutputStream();
|
||||||
out.write(postData);
|
out.write(postData);
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
m_responseCode = conn.getResponseCode();
|
m_responseCode = conn.getResponseCode();
|
||||||
m_responseMessage = conn.getResponseMessage();
|
m_responseMessage = conn.getResponseMessage();
|
||||||
|
|
||||||
switch (m_responseCode) {
|
switch (m_responseCode) {
|
||||||
case HttpURLConnection.HTTP_OK:
|
case HttpURLConnection.HTTP_OK:
|
||||||
StringBuffer response = new StringBuffer();
|
StringBuffer response = new StringBuffer();
|
||||||
@ -193,31 +194,31 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
int contentLength = conn.getHeaderFieldInt("Api-Content-Length", -1);
|
int contentLength = conn.getHeaderFieldInt("Api-Content-Length", -1);
|
||||||
|
|
||||||
m_canUseProgress = (contentLength != -1);
|
m_canUseProgress = (contentLength != -1);
|
||||||
|
|
||||||
while ((read = in.read(buf)) >= 0) {
|
while ((read = in.read(buf)) >= 0) {
|
||||||
response.append(buf, 0, read);
|
response.append(buf, 0, read);
|
||||||
total += read;
|
total += read;
|
||||||
publishProgress(Integer.valueOf(total), Integer.valueOf(contentLength));
|
publishProgress(Integer.valueOf(total), Integer.valueOf(contentLength));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_transportDebugging) Log.d(TAG, "<<< " + response);
|
if (m_transportDebugging) Log.d(TAG, "<<< " + response);
|
||||||
|
|
||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new JsonParser();
|
||||||
|
|
||||||
JsonElement result = parser.parse(response.toString());
|
JsonElement result = parser.parse(response.toString());
|
||||||
JsonObject resultObj = result.getAsJsonObject();
|
JsonObject resultObj = result.getAsJsonObject();
|
||||||
|
|
||||||
m_apiStatusCode = resultObj.get("status").getAsInt();
|
m_apiStatusCode = resultObj.get("status").getAsInt();
|
||||||
|
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
|
|
||||||
switch (m_apiStatusCode) {
|
switch (m_apiStatusCode) {
|
||||||
case API_STATUS_OK:
|
case API_STATUS_OK:
|
||||||
return result.getAsJsonObject().get("content");
|
return result.getAsJsonObject().get("content");
|
||||||
case API_STATUS_ERR:
|
case API_STATUS_ERR:
|
||||||
JsonObject contentObj = resultObj.get("content").getAsJsonObject();
|
JsonObject contentObj = resultObj.get("content").getAsJsonObject();
|
||||||
String error = contentObj.get("error").getAsString();
|
String error = contentObj.get("error").getAsString();
|
||||||
|
|
||||||
if (error.equals("LOGIN_ERROR")) {
|
if (error.equals("LOGIN_ERROR")) {
|
||||||
m_lastError = ApiError.LOGIN_FAILED;
|
m_lastError = ApiError.LOGIN_FAILED;
|
||||||
} else if (error.equals("API_DISABLED")) {
|
} else if (error.equals("API_DISABLED")) {
|
||||||
@ -231,7 +232,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Unknown API error: " + error);
|
Log.d(TAG, "Unknown API error: " + error);
|
||||||
m_lastError = ApiError.API_UNKNOWN;
|
m_lastError = ApiError.API_UNKNOWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@ -252,27 +253,31 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
|
|||||||
m_lastError = ApiError.HTTP_OTHER_ERROR;
|
m_lastError = ApiError.HTTP_OTHER_ERROR;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.disconnect();
|
conn.disconnect();
|
||||||
return null;
|
return null;
|
||||||
} catch (javax.net.ssl.SSLPeerUnverifiedException e) {
|
} catch (javax.net.ssl.SSLPeerUnverifiedException e) {
|
||||||
m_lastError = ApiError.SSL_REJECTED;
|
m_lastError = ApiError.SSL_REJECTED;
|
||||||
|
m_lastErrorMessage = e.getMessage();
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
m_lastError = ApiError.IO_ERROR;
|
m_lastError = ApiError.IO_ERROR;
|
||||||
|
m_lastErrorMessage = e.getMessage();
|
||||||
|
|
||||||
if (e.getMessage() != null) {
|
if (e.getMessage() != null) {
|
||||||
if (e.getMessage().matches("Hostname [^ ]+ was not verified")) {
|
if (e.getMessage().matches("Hostname [^ ]+ was not verified")) {
|
||||||
m_lastError = ApiError.SSL_HOSTNAME_REJECTED;
|
m_lastError = ApiError.SSL_HOSTNAME_REJECTED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (com.google.gson.JsonSyntaxException e) {
|
} catch (com.google.gson.JsonSyntaxException e) {
|
||||||
m_lastError = ApiError.PARSE_ERROR;
|
m_lastError = ApiError.PARSE_ERROR;
|
||||||
|
m_lastErrorMessage = e.getMessage();
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
m_lastError = ApiError.OTHER_ERROR;
|
m_lastError = ApiError.OTHER_ERROR;
|
||||||
|
m_lastErrorMessage = e.getMessage();
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -381,7 +381,13 @@ public class FeedCategoriesFragment extends BaseFeedlistFragment implements OnIt
|
|||||||
if (m_lastError == ApiError.LOGIN_FAILED) {
|
if (m_lastError == ApiError.LOGIN_FAILED) {
|
||||||
m_activity.login(true);
|
m_activity.login(true);
|
||||||
} else {
|
} else {
|
||||||
m_activity.toast(getErrorMessage());
|
|
||||||
|
if (m_lastErrorMessage != null) {
|
||||||
|
m_activity.toast(getString(getErrorMessage()) + "\n" + m_lastErrorMessage);
|
||||||
|
} else {
|
||||||
|
m_activity.toast(getErrorMessage());
|
||||||
|
}
|
||||||
|
|
||||||
//m_activity.setLoadingStatus(getErrorMessage(), false);
|
//m_activity.setLoadingStatus(getErrorMessage(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -570,7 +570,13 @@ public class FeedsFragment extends BaseFeedlistFragment implements OnItemClickLi
|
|||||||
if (m_lastError == ApiError.LOGIN_FAILED) {
|
if (m_lastError == ApiError.LOGIN_FAILED) {
|
||||||
m_activity.login(true);
|
m_activity.login(true);
|
||||||
} else {
|
} else {
|
||||||
m_activity.toast(getErrorMessage());
|
|
||||||
|
if (m_lastErrorMessage != null) {
|
||||||
|
m_activity.toast(getString(getErrorMessage()) + "\n" + m_lastErrorMessage);
|
||||||
|
} else {
|
||||||
|
m_activity.toast(getErrorMessage());
|
||||||
|
}
|
||||||
|
|
||||||
//m_activity.setLoadingStatus(getErrorMessage(), false);
|
//m_activity.setLoadingStatus(getErrorMessage(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -565,7 +565,13 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
|
|||||||
if (m_lastError == ApiError.LOGIN_FAILED) {
|
if (m_lastError == ApiError.LOGIN_FAILED) {
|
||||||
m_activity.login(true);
|
m_activity.login(true);
|
||||||
} else {
|
} else {
|
||||||
m_activity.toast(getErrorMessage());
|
|
||||||
|
if (m_lastErrorMessage != null) {
|
||||||
|
m_activity.toast(getString(getErrorMessage()) + "\n" + m_lastErrorMessage);
|
||||||
|
} else {
|
||||||
|
m_activity.toast(getErrorMessage());
|
||||||
|
}
|
||||||
|
|
||||||
//m_activity.setLoadingStatus(getErrorMessage(), false);
|
//m_activity.setLoadingStatus(getErrorMessage(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1148,6 +1148,20 @@ public class OnlineActivity extends CommonActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setLoadingStatus(String status, boolean showProgress) {
|
||||||
|
TextView tv = (TextView) findViewById(R.id.loading_message);
|
||||||
|
|
||||||
|
if (tv != null) {
|
||||||
|
tv.setText(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
View loadingContainer = findViewById(R.id.loading_container);
|
||||||
|
|
||||||
|
if (loadingContainer != null) {
|
||||||
|
loadingContainer.setVisibility(status.equals("") ? View.GONE : View.VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void logout() {
|
protected void logout() {
|
||||||
setSessionId(null);
|
setSessionId(null);
|
||||||
|
|
||||||
@ -1627,6 +1641,12 @@ public class OnlineActivity extends CommonActivity {
|
|||||||
// Unknown method means old tt-rss, in that case we assume API 0 and continue
|
// Unknown method means old tt-rss, in that case we assume API 0 and continue
|
||||||
|
|
||||||
setLoadingStatus(getErrorMessage(), false);
|
setLoadingStatus(getErrorMessage(), false);
|
||||||
|
|
||||||
|
if (m_lastErrorMessage != null) {
|
||||||
|
setLoadingStatus(getString(getErrorMessage()) + "\n\n" + m_lastErrorMessage, false);
|
||||||
|
} else {
|
||||||
|
setLoadingStatus(getErrorMessage(), false);
|
||||||
|
}
|
||||||
|
|
||||||
if (m_listener != null) {
|
if (m_listener != null) {
|
||||||
m_listener.OnLoginFailed();
|
m_listener.OnLoginFailed();
|
||||||
@ -1667,7 +1687,12 @@ public class OnlineActivity extends CommonActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setSessionId(null);
|
setSessionId(null);
|
||||||
setLoadingStatus(getErrorMessage(), false);
|
|
||||||
|
if (m_lastErrorMessage != null) {
|
||||||
|
setLoadingStatus(getString(getErrorMessage()) + "\n\n" + m_lastErrorMessage, false);
|
||||||
|
} else {
|
||||||
|
setLoadingStatus(getErrorMessage(), false);
|
||||||
|
}
|
||||||
|
|
||||||
loginFailure();
|
loginFailure();
|
||||||
}
|
}
|
||||||
|
@ -118,7 +118,12 @@ public class HeadlinesRequest extends ApiRequest {
|
|||||||
if (m_lastError == ApiError.LOGIN_FAILED) {
|
if (m_lastError == ApiError.LOGIN_FAILED) {
|
||||||
m_activity.login();
|
m_activity.login();
|
||||||
} else {
|
} else {
|
||||||
m_activity.toast(getErrorMessage());
|
|
||||||
|
if (m_lastErrorMessage != null) {
|
||||||
|
m_activity.toast(m_activity.getString(getErrorMessage()) + "\n" + m_lastErrorMessage);
|
||||||
|
} else {
|
||||||
|
m_activity.toast(getErrorMessage());
|
||||||
|
}
|
||||||
//m_activity.setLoadingStatus(getErrorMessage(), false);
|
//m_activity.setLoadingStatus(getErrorMessage(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user