Advertisement
stevekamau

Untitled

Nov 28th, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.45 KB | None | 0 0
  1.  public void makeRequest(String uri, String json) {
  2.         try {
  3.             HttpClient client = new DefaultHttpClient();
  4.             HttpPost httpPost = new HttpPost(uri);
  5.             httpPost.setEntity(new StringEntity(json));
  6.             httpPost.setHeader("Accept", "application/json");
  7.             httpPost.setHeader(getResources().getString(R.string.api_token), getApiKey());
  8.             httpPost.setHeader("class", "Business");
  9.             httpPost.setHeader("attendant_name", ap.getAttendantName());
  10.             httpPost.setHeader("attendant_id", getattendant_id());
  11.             httpPost.setHeader("Content-type", "application/json");
  12.             Log.d("request_headers", "api token:" + getApiKey() + "/");
  13.             Log.d("request_made", "true");
  14.             HttpResponse response = client.execute(httpPost);
  15.  
  16.             String responseStr = EntityUtils.toString(response.getEntity());
  17.             Log.d("request_made3", responseStr);
  18.  
  19.             Object jsonObj = new JSONTokener(responseStr).nextValue();
  20.             if (jsonObj instanceof JSONObject) {
  21.                 JSONObject jsonObject = (JSONObject) jsonObj;
  22.                 //further actions on jsonObjects
  23.                 final String message = jsonObject.getString("message");
  24.                 runOnUiThread(new Runnable() {
  25.                     @Override
  26.                     public void run() {
  27.                         Crouton.makeText(MainActivity.this, message,
  28.                                 Style.ALERT, R.id.mysframe).show();
  29.  
  30.                     }
  31.                 });
  32.  
  33.             } else if (jsonObj instanceof JSONArray) {
  34.                 //further actions on jsonArray
  35.                 JSONArray jArray = new JSONArray(responseStr);
  36.                 for (int i = 0; i < jArray.length(); i++) {
  37.                     JSONObject obj = jArray.getJSONObject(i);
  38.                     Boolean success = obj.getBoolean("success");
  39.  
  40.                     if (success) {
  41.                         //send firebase push notification here
  42.                         sh.deletePendingSales();
  43.                         Log.d("request_made3", "reached");
  44.  
  45.                         //get the version number from server
  46.                         Integer minimum_version_code = obj.getInt("minimum-version-code");
  47.                         Integer version_code = obj.getInt("version-code");
  48.                         whats_new = obj.getString("whats-new");
  49.                         minimum_whats_new = obj.getString("minimum-whats-new");
  50.                         //save the subscription values to preferences
  51.                         ap.setExpirationDate(obj.getString("expiring_on"));
  52.                         ap.setPackageName(obj.getString("package_name"));
  53.                         ap.setRequestedAt(obj.getString("requested_at"));
  54.                         //fetch version code from the phone
  55.                         PackageManager manager = getPackageManager();
  56.                         try {
  57.                             PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
  58.                             if (version_code > info.versionCode) {
  59.                                 if (minimum_version_code > info.versionCode) {
  60.                                     //force update required
  61.                                     runOnUiThread(new Runnable() {
  62.                                         @Override
  63.                                         public void run() {
  64.                                             //forceUpdate(minimum_whats_new);
  65.                                             fireBaseNotificationDialog(minimum_whats_new, "force_update");
  66.                                         }
  67.                                     });
  68.                                 } else {
  69.                                     //soft update required
  70.                                     runOnUiThread(new Runnable() {
  71.                                         @Override
  72.                                         public void run() {
  73.                                             //softUpdate(minimum_whats_new);
  74.                                             fireBaseNotificationDialog(whats_new, "soft_update");
  75.                                         }
  76.                                     });
  77.                                 }
  78.                             }
  79.  
  80.                         } catch (PackageManager.NameNotFoundException e) {
  81.                             e.printStackTrace();
  82.                         }
  83.  
  84.                         JSONArray recordArray = obj.getJSONArray("data");
  85.                         for (int record_i = 0; record_i < recordArray.length(); record_i++) {
  86.                             JSONObject record_obj = recordArray.getJSONObject(record_i);
  87.                             String time_code = record_obj.getString("time_code");
  88.                             String business_id = record_obj.getString("business_id");
  89.                             String name = record_obj.getString("name");
  90.                             String quantity = record_obj.getString("quantity");
  91.                             String attendant_name = record_obj.getString("attendant_name");
  92.                             String attendant_id = record_obj.getString("attendant_id");
  93.                             //send alert to devices
  94.                             SendPushNotifications spn = new SendPushNotifications();
  95.                             spn.triggerFirebaseNotificationRequest(time_code, business_id, name, quantity,
  96.                                     attendant_name, attendant_id);
  97.                             //replace _ in keys with -
  98.                             sh.addSaleKey(time_code);
  99.                             Log.d("request_made5", name);
  100.                         }
  101.  
  102.                     /*
  103.                 SendPushNotifications spn=new SendPushNotifications();
  104.                 spn.triggerFirebaseNotificationRequest();*/
  105.                         Log.d("request_made6", "reached");
  106.                         //if sync is successful then we simply abort further requests
  107.                         //this is to avoid sending multiple requests
  108.                         // httpPost.abort();
  109.  
  110.                     } else {
  111.                         //sync failed, update status to pending so sale is not deleted
  112.                         sh.updatePendingSalesToProcessing("pending");
  113.                         //sync has failed
  114.                         final String failed = obj.getString("message");
  115.                         runOnUiThread(new Runnable() {
  116.                             @Override
  117.                             public void run() {
  118.                                 Crouton.makeText(MainActivity.this, failed,
  119.                                         Style.ALERT, R.id.mysframe).show();
  120.  
  121.                             }
  122.                         });
  123.                         //logout
  124.                         if (obj.getString("key").equals("auth")) {
  125.                             AppController.getInstance()
  126.                                     .getRequestQueue().getCache()
  127.                                     .clear();
  128.                             SharedPreferences spref2 = getSharedPreferences(
  129.                                     "ACCOUNT", MODE_PRIVATE);
  130.                             SharedPreferences.Editor editor = spref2.edit();
  131.                             editor.putBoolean("loggedin", false);
  132.                             editor.putString("apiKey", "");
  133.                             editor.commit();
  134.                             editor.apply();
  135.                             Intent intent = new Intent(getApplicationContext(), Login.class);
  136.                             intent.putExtra("loggedOut", failed);
  137.                             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
  138.                             startActivity(intent);
  139.                             finish();
  140.                         }
  141.  
  142.                     }
  143.                 }
  144.             }
  145.             Log.d("request_made1", responseStr);
  146.            
  147.         } catch (UnsupportedEncodingException e) {
  148.             //sync failed, update status to pending so sale is not deleted
  149.             sh.updatePendingSalesToProcessing("pending");
  150.             e.printStackTrace();
  151.         } catch (ClientProtocolException e) {
  152.             doSyncing = true;
  153.             //sync failed, update status to pending so sale is not deleted
  154.             sh.updatePendingSalesToProcessing("pending");
  155.             //sync failed
  156.             runOnUiThread(new Runnable() {
  157.                 @Override
  158.                 public void run() {
  159.                     Crouton.makeText(MainActivity.this, "Syncing failed, retry later",
  160.                             Style.ALERT, R.id.mysframe).show();
  161.                 }
  162.             });
  163.             e.printStackTrace();
  164.         } catch (IOException e) {
  165.             //sync failed, update status to pending so sale is not deleted
  166.             sh.updatePendingSalesToProcessing("pending");
  167.             doSyncing = true;
  168.             //sync failed so we rest the syncing icon
  169.             runOnUiThread(new Runnable() {
  170.                 @Override
  171.                 public void run() {
  172.                     Crouton.makeText(MainActivity.this, "Syncing failed, retry later",
  173.                             Style.ALERT, R.id.mysframe).show();
  174.                 }
  175.             });
  176.             e.printStackTrace();
  177.         } catch (JSONException e) {
  178.             //sync failed, update status to pending so sale is not deleted
  179.             sh.updatePendingSalesToProcessing("pending");
  180.             e.printStackTrace();
  181.  
  182.         }
  183.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement