Search in sources :

Example 1 with CustomSyncServerUrlException

use of com.ichi2.libanki.sync.CustomSyncServerUrlException in project Anki-Android by ankidroid.

the class Connection method doInBackgroundLogin.

private Payload doInBackgroundLogin(Payload data) {
    String username = (String) data.data[0];
    String password = (String) data.data[1];
    HostNum hostNum = (HostNum) data.data[2];
    RemoteServer server = new RemoteServer(this, null, hostNum);
    Response ret;
    try {
        ret = server.hostKey(username, password);
    } catch (UnknownHttpResponseException e) {
        Timber.w(e);
        data.success = false;
        data.resultType = ERROR;
        data.result = new Object[] { e.getResponseCode(), e.getMessage() };
        return data;
    } catch (CustomSyncServerUrlException e2) {
        Timber.w(e2);
        data.success = false;
        data.resultType = CUSTOM_SYNC_SERVER_URL;
        data.result = new Object[] { e2 };
        return data;
    } catch (Exception e2) {
        Timber.w(e2);
        // Ask user to report all bugs which aren't timeout errors
        if (!timeoutOccurred(e2)) {
            AnkiDroidApp.sendExceptionReport(e2, "doInBackgroundLogin");
        }
        data.success = false;
        data.resultType = CONNECTION_ERROR;
        data.result = new Object[] { e2 };
        return data;
    }
    String hostkey = null;
    boolean valid = false;
    if (ret != null) {
        data.returnType = ret.code();
        Timber.d("doInBackgroundLogin - response from server: %d, (%s)", data.returnType, ret.message());
        if (data.returnType == 200) {
            try {
                JSONObject response = new JSONObject(ret.body().string());
                hostkey = response.getString("key");
                valid = (hostkey != null) && (hostkey.length() > 0);
            } catch (JSONException e) {
                Timber.w(e);
                valid = false;
            } catch (IllegalStateException | IOException | NullPointerException e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        Timber.e("doInBackgroundLogin - empty response from server");
    }
    if (valid) {
        data.success = true;
        data.data = new String[] { username, hostkey };
    } else {
        data.success = false;
    }
    return data;
}
Also used : JSONException(com.ichi2.utils.JSONException) HostNum(com.ichi2.libanki.sync.HostNum) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) CustomSyncServerUrlException(com.ichi2.libanki.sync.CustomSyncServerUrlException) JSONException(com.ichi2.utils.JSONException) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) CustomSyncServerUrlException(com.ichi2.libanki.sync.CustomSyncServerUrlException) JSONObject(com.ichi2.utils.JSONObject) RemoteServer(com.ichi2.libanki.sync.RemoteServer)

Aggregations

MediaSyncException (com.ichi2.anki.exception.MediaSyncException)1 UnknownHttpResponseException (com.ichi2.anki.exception.UnknownHttpResponseException)1 CustomSyncServerUrlException (com.ichi2.libanki.sync.CustomSyncServerUrlException)1 HostNum (com.ichi2.libanki.sync.HostNum)1 RemoteServer (com.ichi2.libanki.sync.RemoteServer)1 JSONException (com.ichi2.utils.JSONException)1 JSONObject (com.ichi2.utils.JSONObject)1 IOException (java.io.IOException)1 Response (okhttp3.Response)1