Zookey90

Untitled

Apr 2nd, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1.  private void getVolleyTranslations(final String language) {
  2.  
  3.  
  4.  
  5.         StringRequest req = new StringRequest(Request.Method.POST, URLManager.GET_TRANSLATIONS, new Response.Listener<String>() {
  6.             @Override
  7.             public void onResponse(String response) {
  8.                 String translations = response.toString();
  9.                 Log.d(TAG, "translations"+translations);
  10.                 //Save to the database
  11.  
  12.                 //Parse translations
  13.                 try {
  14.                     JSONArray jsonArray = new JSONArray(translations);
  15.  
  16.                     for (int i = 0; i < jsonArray.length(); i++) {
  17.                         JSONObject jsonObject = jsonArray.getJSONObject(i);
  18.                         int id = jsonObject.getInt("id");
  19.                         String name = jsonObject.getString("name");
  20.                         String version = jsonObject.getString("version");
  21.                         String code = jsonObject.getString("code");
  22.                         String platform = jsonObject.getString("platform");
  23.                         JSONObject translationsObj = jsonObject.getJSONObject("translations");
  24.  
  25.                         Languages languages = new Languages(id, name, version, code, platform, translationsObj.toString());
  26.  
  27.                         if(db.getLanguagesCount() > 0) {
  28.                             // DB has languages
  29.                             Log.d("TAG", "DB has langs");
  30.                             //We have saved that language in DB, but we need to update it
  31.                             if (db.getLanguage(Integer.parseInt(serverUserLanguage)) != null) {
  32.                                 Log.d("TAG", "We have saved that language in DB, but we need to update it");
  33.                                 db.updateTranslation(languages);
  34.                             } else {
  35.                                 //We don't have saved that language in DB, so we need to add it
  36.                                 //Add language translations
  37.                                 Log.d("TAG", "We don't have saved that language in DB, so we need to add it");
  38.                                 db.addLanguage(languages);
  39.                             }
  40.                         }
  41.                         else{
  42.                             // DB does not have languages, we need to add it
  43.                             Log.d("TAG", "DB does not have languages, we need to add it");
  44.                             db.addLanguage(languages);
  45.                         }
  46.  
  47.                     }
  48.  
  49.                     Languages lang = db.getLanguage(Utils.getUserLanguage(getApplicationContext()));
  50.                     Log.d("TAG", "lang id: " + lang.getId());
  51.                     Log.d("TAG", "lang name: " + lang.getName());
  52.                     Log.d("TAG", "lang version: " + lang.getVersion());
  53.                     Log.d("TAG", "lang code: " + lang.getCode());
  54.                     Log.d("TAG", "lang translations: " + lang.getTranslationsObj());
  55.  
  56.  
  57.                     Log.d("TAG", "Broj redova u tabeli je: " + String.valueOf(db.getLanguagesCount()));
  58.  
  59.  
  60.                     //After that open MainActivity and shows correct language
  61.                     Intent i = new Intent(getApplicationContext(), MainActivity.class);
  62.                     startActivity(i);
  63.  
  64.                 } catch (JSONException e) {
  65.                     JSONObject jsonObject = null;
  66.                     try {
  67.                         jsonObject = new JSONObject(translations);
  68.                         String error = jsonObject.getString("error");
  69.                         JSONObject err = new JSONObject(error);
  70.                         String errStr = err.getString("error");
  71.                         Log.d(TAG, "Imamo error:" + errStr);
  72.                         Intent i = new Intent(getApplicationContext(), LoginActivity.class);
  73.                         startActivity(i);
  74.                     } catch (JSONException e1) {
  75.                         e1.printStackTrace();
  76.                     }
  77.  
  78.                     e.printStackTrace();
  79.                 }
  80.  
  81.             }
  82.         }, new Response.ErrorListener() {
  83.             @Override
  84.             public void onErrorResponse(VolleyError error) {
  85.                 if (error instanceof TimeoutError || error instanceof NoConnectionError) {
  86.                      Log.d("TAG", "v1: "+error);
  87.                 } else if (error instanceof AuthFailureError) {
  88.                     Log.d("TAG", "v2: "+error);
  89.                 } else if (error instanceof ServerError) {
  90.                     Log.d("TAG", "v3: "+error);
  91.                 } else if (error instanceof NetworkError) {
  92.                     Log.d("TAG", "v4: "+error);
  93.                 } else if (error instanceof ParseError) {
  94.                     Log.d("TAG", "v5: "+error);
  95.                 }
  96.             }
  97.         }) {
  98.  
  99.             @Override
  100.             protected Map<String, String> getParams() throws AuthFailureError {
  101.                 HashMap<String, String> mParams = new HashMap<String, String>();
  102.                 mParams.put(PPCConstants.USER_LANGUAGES, language);
  103.                 mParams.put(PPCConstants.USER_PLATFORM, "3");
  104.                 return mParams;
  105.             }
  106.  
  107.             /**
  108.              * Passing some request headers
  109.              */
  110.             @Override
  111.             public Map<String, String> getHeaders() throws AuthFailureError {
  112.                 HashMap<String, String> headers = new HashMap<String, String>();
  113.  
  114.                 //Adding data to the header
  115.                 headers.put(PPCConstants.HEADER_KEY_CONTENT_TYPE, PPCConstants.HEADER_VALUE_CONTENT_TYPE);
  116.                 headers.put(PPCConstants.HEADER_KEY_CONTENT_LENGTH, "23");
  117.                 headers.put(PPCConstants.HEADER_KEY_PLATFORM, PPCConstants.HEADER_VALUE_PLATFORM_NAME);
  118.                 headers.put(PPCConstants.HEADER_KEY_DEVICE_ID, "123android321");
  119.                 headers.put(PPCConstants.HEADER_KEY_APP_VERSION, Utils.getAppVersionName(getApplicationContext()));
  120.                 headers.put(PPCConstants.HEADER_KEY_ACCESS_TOKEN, Utils.checkAccessToken(getApplicationContext()));
  121.  
  122.                 return headers;
  123.  
  124.             }
  125.  
  126.         };
  127.  
  128.         // Adding request to request queue
  129.         AppController.getInstance().addToRequestQueue(req);
  130.     }
Advertisement
Add Comment
Please, Sign In to add comment