Search in sources :

Example 1 with Response

use of com.ichi2.anki.multimediacard.glosbe.json.Response in project Anki-Android by Ramblurr.

the class Connection method doInBackgroundLogin.

private Payload doInBackgroundLogin(Payload data) {
    String username = (String) data.data[0];
    String password = (String) data.data[1];
    BasicHttpSyncer server = new RemoteServer(this, null);
    HttpResponse ret = server.hostKey(username, password);
    String hostkey = null;
    boolean valid = false;
    if (ret != null) {
        data.returnType = ret.getStatusLine().getStatusCode();
        Log.i(AnkiDroidApp.TAG, "doInBackgroundLogin - response from server: " + data.returnType + " (" + ret.getStatusLine().getReasonPhrase() + ")");
        if (data.returnType == 200) {
            try {
                JSONObject jo = (new JSONObject(server.stream2String(ret.getEntity().getContent())));
                hostkey = jo.getString("key");
                valid = (hostkey != null) && (hostkey.length() > 0);
            } catch (JSONException e) {
                valid = false;
            } catch (IllegalStateException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundLogin - empty response from server");
    }
    if (valid) {
        data.success = true;
        data.data = new String[] { username, hostkey };
    } else {
        data.success = false;
    }
    return data;
}
Also used : BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) JSONObject(org.json.JSONObject) HttpResponse(org.apache.http.HttpResponse) JSONException(org.json.JSONException) IOException(java.io.IOException) RemoteServer(com.ichi2.libanki.sync.RemoteServer)

Example 2 with Response

use of com.ichi2.anki.multimediacard.glosbe.json.Response in project Anki-Android by Ramblurr.

the class TranslationActivity method parseJson.

private static ArrayList<String> parseJson(Response resp, String languageCodeTo) {
    ArrayList<String> res = new ArrayList<String>();
    /*
         * The algorithm below includes the parsing of glosbe results. Glosbe.com returns a list of different phrases in
         * source and destination languages. This is done, probably, to improve the reader's understanding. We leave
         * here only the translations to the destination language.
         */
    List<Tuc> tucs = resp.getTuc();
    if (tucs == null) {
        return res;
    }
    for (Tuc tuc : tucs) {
        if (tuc == null) {
            continue;
        }
        List<Meaning> meanings = tuc.getMeanings();
        if (meanings != null) {
            for (Meaning meaning : meanings) {
                if (meaning == null) {
                    continue;
                }
                if (meaning.getLanguage() == null) {
                    continue;
                }
                if (meaning.getLanguage().contentEquals(languageCodeTo)) {
                    String unescappedString = HtmlUtil.unescape(meaning.getText());
                    res.add(unescappedString);
                }
            }
        }
        Phrase phrase = tuc.getPhrase();
        if (phrase != null) {
            if (phrase.getLanguageCode() == null) {
                continue;
            }
            if (phrase.getLanguageCode().contentEquals(languageCodeTo)) {
                String unescappedString = HtmlUtil.unescape(phrase.getText());
                res.add(unescappedString);
            }
        }
    }
    return res;
}
Also used : Tuc(com.ichi2.anki.multimediacard.glosbe.json.Tuc) ArrayList(java.util.ArrayList) Phrase(com.ichi2.anki.multimediacard.glosbe.json.Phrase) Meaning(com.ichi2.anki.multimediacard.glosbe.json.Meaning)

Example 3 with Response

use of com.ichi2.anki.multimediacard.glosbe.json.Response in project Anki-Android by Ramblurr.

the class Feedback method postFeedback.

/**
     * Posting feedback or error info to the server. This is called from the AsyncTask.
     * 
     * @param url The url to post the feedback to.
     * @param type The type of the info, eg Feedback.TYPE_CRASH_STACKTRACE.
     * @param feedback For feedback types this is the message. For error/crash types this is the path to the error file.
     * @param groupId A single time generated ID, so that errors/feedback send together can be grouped together.
     * @param index The index of the error in the list
     * @return A Payload file showing success, response code and response message.
     */
public static Payload postFeedback(String url, String type, String feedback, String groupId, int index, Application app) {
    Payload result = new Payload(null);
    List<NameValuePair> pairs = null;
    if (!isErrorType(type)) {
        pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("type", type));
        pairs.add(new BasicNameValuePair("groupid", groupId));
        pairs.add(new BasicNameValuePair("index", "0"));
        pairs.add(new BasicNameValuePair("message", feedback));
        addTimestamp(pairs);
    } else {
        pairs = Feedback.extractPairsFromError(type, feedback, groupId, index, app);
        if (pairs == null) {
            result.success = false;
            result.result = null;
        }
    }
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("User-Agent", "AnkiDroid");
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(pairs));
        HttpResponse response = httpClient.execute(httpPost);
        Log.e(AnkiDroidApp.TAG, String.format("Bug report posted to %s", url));
        int respCode = response.getStatusLine().getStatusCode();
        switch(respCode) {
            case 200:
                result.success = true;
                result.returnType = respCode;
                result.result = Utils.convertStreamToString(response.getEntity().getContent());
                Log.i(AnkiDroidApp.TAG, String.format("postFeedback OK: %s", result.result));
                break;
            default:
                Log.e(AnkiDroidApp.TAG, String.format("postFeedback failure: %d - %s", response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
                result.success = false;
                result.returnType = respCode;
                result.result = response.getStatusLine().getReasonPhrase();
                break;
        }
    } catch (ClientProtocolException ex) {
        Log.e(AnkiDroidApp.TAG, "ClientProtocolException: " + ex.toString());
        result.success = false;
        result.result = ex.toString();
    } catch (IOException ex) {
        Log.e(AnkiDroidApp.TAG, "IOException: " + ex.toString());
        result.success = false;
        result.result = ex.toString();
    }
    return result;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ClientProtocolException(org.apache.http.client.ClientProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) Payload(com.ichi2.async.Connection.Payload)

Example 4 with Response

use of com.ichi2.anki.multimediacard.glosbe.json.Response in project Anki-Android by Ramblurr.

the class SearchImageActivity method postFinished.

public void postFinished(ImageSearchResponse response) {
    ArrayList<String> theImages = new ArrayList<String>();
    // No loop, just a good construct to break out from
    do {
        if (response == null)
            break;
        if (response.getOk() == false)
            break;
        ResponseData rdata = response.getResponseData();
        if (rdata == null)
            break;
        List<Result> results = rdata.getResults();
        if (results == null)
            break;
        for (Result result : results) {
            if (result == null) {
                continue;
            }
            String url = result.getUrl();
            if (url != null) {
                theImages.add(url);
            }
        }
        if (theImages.size() == 0)
            break;
        proceedWithImages(theImages);
        return;
    } while (false);
    returnFailure(gtxt(R.string.multimedia_editor_imgs_no_results));
}
Also used : ResponseData(com.ichi2.anki.multimediacard.googleimagesearch.json.ResponseData) ArrayList(java.util.ArrayList) Result(com.ichi2.anki.multimediacard.googleimagesearch.json.Result)

Example 5 with Response

use of com.ichi2.anki.multimediacard.glosbe.json.Response in project Anki-Android by Ramblurr.

the class TranslationActivity method showPickTranslationDialog.

private void showPickTranslationDialog() {
    if (mTranslation.startsWith("FAILED")) {
        returnFailure(getText(R.string.multimedia_editor_trans_getting_failure).toString());
        return;
    }
    Gson gson = new Gson();
    Response resp = gson.fromJson(mTranslation, Response.class);
    if (resp == null) {
        returnFailure(getText(R.string.multimedia_editor_trans_getting_failure).toString());
        return;
    }
    if (!resp.getResult().contentEquals("ok")) {
        if (!mSource.toLowerCase(Locale.getDefault()).contentEquals(mSource)) {
            showToastLong(gtxt(R.string.multimedia_editor_word_search_try_lower_case));
        }
        returnFailure(getText(R.string.multimedia_editor_trans_getting_failure).toString());
        return;
    }
    mPossibleTranslations = parseJson(resp, mLangCodeTo);
    if (mPossibleTranslations.size() == 0) {
        if (!mSource.toLowerCase(Locale.getDefault()).contentEquals(mSource)) {
            showToastLong(gtxt(R.string.multimedia_editor_word_search_try_lower_case));
        }
        returnFailure(getText(R.string.multimedia_editor_error_word_not_found).toString());
        return;
    }
    PickStringDialogFragment fragment = new PickStringDialogFragment();
    fragment.setChoices(mPossibleTranslations);
    fragment.setOnclickListener(this);
    fragment.setTitle(getText(R.string.multimedia_editor_trans_pick_translation).toString());
    fragment.show(this.getSupportFragmentManager(), "pick.translation");
}
Also used : Response(com.ichi2.anki.multimediacard.glosbe.json.Response) Gson(com.google.gson.Gson)

Aggregations

IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HttpResponse (org.apache.http.HttpResponse)2 Gson (com.google.gson.Gson)1 Meaning (com.ichi2.anki.multimediacard.glosbe.json.Meaning)1 Phrase (com.ichi2.anki.multimediacard.glosbe.json.Phrase)1 Response (com.ichi2.anki.multimediacard.glosbe.json.Response)1 Tuc (com.ichi2.anki.multimediacard.glosbe.json.Tuc)1 ResponseData (com.ichi2.anki.multimediacard.googleimagesearch.json.ResponseData)1 Result (com.ichi2.anki.multimediacard.googleimagesearch.json.Result)1 Payload (com.ichi2.async.Connection.Payload)1 BasicHttpSyncer (com.ichi2.libanki.sync.BasicHttpSyncer)1 RemoteServer (com.ichi2.libanki.sync.RemoteServer)1 NameValuePair (org.apache.http.NameValuePair)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpClient (org.apache.http.client.HttpClient)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1