Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void makeRequest(String uri, String json) {
- try {
- HttpClient client = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(uri);
- httpPost.setEntity(new StringEntity(json));
- httpPost.setHeader("Accept", "application/json");
- httpPost.setHeader(getResources().getString(R.string.api_token), getApiKey());
- httpPost.setHeader("class", "Business");
- httpPost.setHeader("attendant_name", ap.getAttendantName());
- httpPost.setHeader("attendant_id", getattendant_id());
- httpPost.setHeader("Content-type", "application/json");
- Log.d("request_headers", "api token:" + getApiKey() + "/");
- Log.d("request_made", "true");
- HttpResponse response = client.execute(httpPost);
- String responseStr = EntityUtils.toString(response.getEntity());
- Log.d("request_made3", responseStr);
- Object jsonObj = new JSONTokener(responseStr).nextValue();
- if (jsonObj instanceof JSONObject) {
- JSONObject jsonObject = (JSONObject) jsonObj;
- //further actions on jsonObjects
- final String message = jsonObject.getString("message");
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Crouton.makeText(MainActivity.this, message,
- Style.ALERT, R.id.mysframe).show();
- }
- });
- } else if (jsonObj instanceof JSONArray) {
- //further actions on jsonArray
- JSONArray jArray = new JSONArray(responseStr);
- for (int i = 0; i < jArray.length(); i++) {
- JSONObject obj = jArray.getJSONObject(i);
- Boolean success = obj.getBoolean("success");
- if (success) {
- //send firebase push notification here
- sh.deletePendingSales();
- Log.d("request_made3", "reached");
- //get the version number from server
- Integer minimum_version_code = obj.getInt("minimum-version-code");
- Integer version_code = obj.getInt("version-code");
- whats_new = obj.getString("whats-new");
- minimum_whats_new = obj.getString("minimum-whats-new");
- //save the subscription values to preferences
- ap.setExpirationDate(obj.getString("expiring_on"));
- ap.setPackageName(obj.getString("package_name"));
- ap.setRequestedAt(obj.getString("requested_at"));
- //fetch version code from the phone
- PackageManager manager = getPackageManager();
- try {
- PackageInfo info = manager.getPackageInfo(getPackageName(), 0);
- if (version_code > info.versionCode) {
- if (minimum_version_code > info.versionCode) {
- //force update required
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- //forceUpdate(minimum_whats_new);
- fireBaseNotificationDialog(minimum_whats_new, "force_update");
- }
- });
- } else {
- //soft update required
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- //softUpdate(minimum_whats_new);
- fireBaseNotificationDialog(whats_new, "soft_update");
- }
- });
- }
- }
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
- JSONArray recordArray = obj.getJSONArray("data");
- for (int record_i = 0; record_i < recordArray.length(); record_i++) {
- JSONObject record_obj = recordArray.getJSONObject(record_i);
- String time_code = record_obj.getString("time_code");
- String business_id = record_obj.getString("business_id");
- String name = record_obj.getString("name");
- String quantity = record_obj.getString("quantity");
- String attendant_name = record_obj.getString("attendant_name");
- String attendant_id = record_obj.getString("attendant_id");
- //send alert to devices
- SendPushNotifications spn = new SendPushNotifications();
- spn.triggerFirebaseNotificationRequest(time_code, business_id, name, quantity,
- attendant_name, attendant_id);
- //replace _ in keys with -
- sh.addSaleKey(time_code);
- Log.d("request_made5", name);
- }
- /*
- SendPushNotifications spn=new SendPushNotifications();
- spn.triggerFirebaseNotificationRequest();*/
- Log.d("request_made6", "reached");
- //if sync is successful then we simply abort further requests
- //this is to avoid sending multiple requests
- // httpPost.abort();
- } else {
- //sync failed, update status to pending so sale is not deleted
- sh.updatePendingSalesToProcessing("pending");
- //sync has failed
- final String failed = obj.getString("message");
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Crouton.makeText(MainActivity.this, failed,
- Style.ALERT, R.id.mysframe).show();
- }
- });
- //logout
- if (obj.getString("key").equals("auth")) {
- AppController.getInstance()
- .getRequestQueue().getCache()
- .clear();
- SharedPreferences spref2 = getSharedPreferences(
- "ACCOUNT", MODE_PRIVATE);
- SharedPreferences.Editor editor = spref2.edit();
- editor.putBoolean("loggedin", false);
- editor.putString("apiKey", "");
- editor.commit();
- editor.apply();
- Intent intent = new Intent(getApplicationContext(), Login.class);
- intent.putExtra("loggedOut", failed);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- finish();
- }
- }
- }
- }
- Log.d("request_made1", responseStr);
- } catch (UnsupportedEncodingException e) {
- //sync failed, update status to pending so sale is not deleted
- sh.updatePendingSalesToProcessing("pending");
- e.printStackTrace();
- } catch (ClientProtocolException e) {
- doSyncing = true;
- //sync failed, update status to pending so sale is not deleted
- sh.updatePendingSalesToProcessing("pending");
- //sync failed
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Crouton.makeText(MainActivity.this, "Syncing failed, retry later",
- Style.ALERT, R.id.mysframe).show();
- }
- });
- e.printStackTrace();
- } catch (IOException e) {
- //sync failed, update status to pending so sale is not deleted
- sh.updatePendingSalesToProcessing("pending");
- doSyncing = true;
- //sync failed so we rest the syncing icon
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Crouton.makeText(MainActivity.this, "Syncing failed, retry later",
- Style.ALERT, R.id.mysframe).show();
- }
- });
- e.printStackTrace();
- } catch (JSONException e) {
- //sync failed, update status to pending so sale is not deleted
- sh.updatePendingSalesToProcessing("pending");
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement