예제 #1
0
  public void sendViaHttpClient() {
    final Long startTime = System.currentTimeMillis();
    try {
      if (PODTYPE.POD_REJECTED != PodType) {
        File file = new File(filePath);

        // defensive check

        if (!file.exists()) {
          Log.d("UploadPhoto", "File does not exists");
          LocationDB.getInstance().deleteFromPhotoTable(filePath);
          ConsumerForEverythingElse.getInstance().removeFromQueue(UploadPhotoTask.this);
          return;
        }
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        byte[] arr = Utils.bitmapToBytes(bitmap, CompressFormat.JPEG, 75);
        encodedImage = Base64.encodeToString(arr, Base64.DEFAULT);
      }
      JSONObject body = new JSONObject();
      body.put(Constants.DEALER_ID, dealerId);
      body.put("img", encodedImage);
      body.put(Constants.POD_TYPE, PodType);
      body.put(Constants.GRID, grId);
      Location location = GPSTracker.getInstance().getLastKnownLocation();
      Log.d("locattt", location + "");
      if (location != null) {
        body.put(Constants.LOCATION, location.getLatitude() + "," + location.getLongitude());
      }
      body.put(Constants.STS, System.currentTimeMillis() / 1000);
      StringLocEntity entity = new StringLocEntity(body.toString(), this);
      String URL = Constants.HTTP_STRING + Constants.DEV_STAGING_HOST + Constants.UPLOAD_PHOTO;

      RequestParams params =
          new RequestBuilder().AddToken(true).setEntity(entity).setUrl(URL).build();
      HTTPManager.post(
          params,
          new IResponse() {

            @Override
            public void onSuccess(String response) {
              Log.d(
                  "UploadPhotoSuccess",
                  response + "Time Taken" + (System.currentTimeMillis() - startTime) / 1000 + "");
              LocationDB.getInstance().deleteFromPhotoTable(filePath);
              ConsumerForEverythingElse.getInstance().removeFromQueue(UploadPhotoTask.this);
            }

            @Override
            public void onFailure(int i) {
              ConsumerForEverythingElse.getInstance().toggleNetworkState();
            }
          });
    } catch (JSONException e) {
      e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
  }
  @Override
  public void run() {
    String URL = Constants.HTTP_STRING + Constants.DEV_STAGING_HOST + Constants.UPDATE_LOC;

    JSONObject body = new JSONObject();
    try {
      body.put(Constants.LOCATION, location.toJsonString());

      body.put(Constants.STS, timeStamp);
      body.put(
          Constants.GR_GROUP_ID,
          LocationSharedPreference.getInstance().getData(Constants.GR_GROUP_ID, ""));
      Log.d("Location changed", body.toString());
      StringEntity entity = new StringEntity(body.toString());

      RequestParams params =
          new RequestBuilder().setEntity(entity).AddToken(true).setUrl(URL).build();

      HTTPManager.post(
          params,
          new IResponse() {

            @Override
            public void onSuccess(String response) {
              Log.d(
                  "SendLocToServer",
                  "Thread is " + Thread.currentThread() + "res is >>>" + response);
              LocationDB.getInstance().deleteFromLocationTable(location);
              ConsumerLocation.getInstance().removeFromQueue(SendLocationToServer.this);
            }

            @Override
            public void onFailure(int i) {
              Log.w("SendLocToServer", "Request Failed: " + i);
              ConsumerLocation.getInstance().toggleNetworkState();
            }
          });

    } catch (Exception e) {
      e.printStackTrace();
    }
  }