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;
}
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;
}
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;
}
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));
}
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");
}
Aggregations