Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void getVolleyTranslations(final String language) {
- StringRequest req = new StringRequest(Request.Method.POST, URLManager.GET_TRANSLATIONS, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- String translations = response.toString();
- Log.d(TAG, "translations"+translations);
- //Save to the database
- //Parse translations
- try {
- JSONArray jsonArray = new JSONArray(translations);
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- int id = jsonObject.getInt("id");
- String name = jsonObject.getString("name");
- String version = jsonObject.getString("version");
- String code = jsonObject.getString("code");
- String platform = jsonObject.getString("platform");
- JSONObject translationsObj = jsonObject.getJSONObject("translations");
- Languages languages = new Languages(id, name, version, code, platform, translationsObj.toString());
- if(db.getLanguagesCount() > 0) {
- // DB has languages
- Log.d("TAG", "DB has langs");
- //We have saved that language in DB, but we need to update it
- if (db.getLanguage(Integer.parseInt(serverUserLanguage)) != null) {
- Log.d("TAG", "We have saved that language in DB, but we need to update it");
- db.updateTranslation(languages);
- } else {
- //We don't have saved that language in DB, so we need to add it
- //Add language translations
- Log.d("TAG", "We don't have saved that language in DB, so we need to add it");
- db.addLanguage(languages);
- }
- }
- else{
- // DB does not have languages, we need to add it
- Log.d("TAG", "DB does not have languages, we need to add it");
- db.addLanguage(languages);
- }
- }
- Languages lang = db.getLanguage(Utils.getUserLanguage(getApplicationContext()));
- Log.d("TAG", "lang id: " + lang.getId());
- Log.d("TAG", "lang name: " + lang.getName());
- Log.d("TAG", "lang version: " + lang.getVersion());
- Log.d("TAG", "lang code: " + lang.getCode());
- Log.d("TAG", "lang translations: " + lang.getTranslationsObj());
- Log.d("TAG", "Broj redova u tabeli je: " + String.valueOf(db.getLanguagesCount()));
- //After that open MainActivity and shows correct language
- Intent i = new Intent(getApplicationContext(), MainActivity.class);
- startActivity(i);
- } catch (JSONException e) {
- JSONObject jsonObject = null;
- try {
- jsonObject = new JSONObject(translations);
- String error = jsonObject.getString("error");
- JSONObject err = new JSONObject(error);
- String errStr = err.getString("error");
- Log.d(TAG, "Imamo error:" + errStr);
- Intent i = new Intent(getApplicationContext(), LoginActivity.class);
- startActivity(i);
- } catch (JSONException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- if (error instanceof TimeoutError || error instanceof NoConnectionError) {
- Log.d("TAG", "v1: "+error);
- } else if (error instanceof AuthFailureError) {
- Log.d("TAG", "v2: "+error);
- } else if (error instanceof ServerError) {
- Log.d("TAG", "v3: "+error);
- } else if (error instanceof NetworkError) {
- Log.d("TAG", "v4: "+error);
- } else if (error instanceof ParseError) {
- Log.d("TAG", "v5: "+error);
- }
- }
- }) {
- @Override
- protected Map<String, String> getParams() throws AuthFailureError {
- HashMap<String, String> mParams = new HashMap<String, String>();
- mParams.put(PPCConstants.USER_LANGUAGES, language);
- mParams.put(PPCConstants.USER_PLATFORM, "3");
- return mParams;
- }
- /**
- * Passing some request headers
- */
- @Override
- public Map<String, String> getHeaders() throws AuthFailureError {
- HashMap<String, String> headers = new HashMap<String, String>();
- //Adding data to the header
- headers.put(PPCConstants.HEADER_KEY_CONTENT_TYPE, PPCConstants.HEADER_VALUE_CONTENT_TYPE);
- headers.put(PPCConstants.HEADER_KEY_CONTENT_LENGTH, "23");
- headers.put(PPCConstants.HEADER_KEY_PLATFORM, PPCConstants.HEADER_VALUE_PLATFORM_NAME);
- headers.put(PPCConstants.HEADER_KEY_DEVICE_ID, "123android321");
- headers.put(PPCConstants.HEADER_KEY_APP_VERSION, Utils.getAppVersionName(getApplicationContext()));
- headers.put(PPCConstants.HEADER_KEY_ACCESS_TOKEN, Utils.checkAccessToken(getApplicationContext()));
- return headers;
- }
- };
- // Adding request to request queue
- AppController.getInstance().addToRequestQueue(req);
- }
Advertisement
Add Comment
Please, Sign In to add comment