Esempio n. 1
0
    @Override
    protected void onPostExecute(String sResponse) {
      try {
        if (dialog.isShowing()) dialog.dismiss();

        if (sResponse != null) {
          Log.d("Json", sResponse);
          JSONObject jObj = new JSONObject(sResponse);

          boolean flag = false;
          flag = jObj.has("flag") ? jObj.getBoolean("flag") : false;
          String message = jObj.has("message") ? jObj.getString("message") : "";
          if (flag) {

            Toast.makeText(RegisterActivity.this, "Successfully Registered", Toast.LENGTH_LONG)
                .show();
            JSONObject dataObj = jObj.getJSONObject("data");
            Log.d("data", dataObj.toString());
            UserDataPreferences.saveToken(
                RegisterActivity.this, JSONData.getString(dataObj, "token"));
            UserDataPreferences.saveUserInfo(RegisterActivity.this, dataObj.toString());
            Intent mainActivity = new Intent(RegisterActivity.this, MainActivity.class);
            mainActivity.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mainActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mainActivity);
            overridePendingTransition(R.anim.animation, R.anim.animation2);
            closeActivity();
          } else {
            if (message.equals("")) {
              AppConstant.showSingleButtonAlertDialog(
                  RegisterActivity.this, "Error", "Something went wrong.\nPlease try again");
            } else {
              AppConstant.showSingleButtonAlertDialog(RegisterActivity.this, "Error", message);
            }
          }
        }
      } catch (Exception e) {
        AppConstant.showNetworkError(RegisterActivity.this);
        Log.e(e.getClass().getName(), e.getMessage(), e);
      }
    }
Esempio n. 2
0
    @Override
    protected Void doInBackground(Void... params) {
      JSONParser jParser = new JSONParser(context);

      try {
        if (type == 1) {
          jsonStringer =
              new JSONStringer()
                  .object()
                  .key("email")
                  .value(email)
                  .key("password")
                  .value(pass)
                  .endObject();
          data =
              jParser.sendPostReqLogin(
                  Constants.api_v1 + Constants.api_login, jsonStringer.toString());
        } else if (type == 2) {
          jsonStringer =
              new JSONStringer()
                  .object()
                  .key("facebook_id")
                  .value(user_id)
                  .key("first_name")
                  .value(firstName)
                  .key("email")
                  .value(email)
                  .key("image")
                  .value(imageUrl)
                  .endObject();
          data =
              jParser.sendPostReqLogin(
                  Constants.api_v1 + Constants.api_facebook_login, jsonStringer.toString());
        } else if (type == 3) {
          jsonStringer =
              new JSONStringer()
                  .object()
                  .key("google_id")
                  .value(user_id)
                  .key("first_name")
                  .value(firstName)
                  .key("email")
                  .value(email)
                  .key("image")
                  .value(imageUrl)
                  .endObject();
          data =
              jParser.sendPostReqLogin(
                  Constants.api_v1 + Constants.api_google_login, jsonStringer.toString());
        }
        responseCode = Integer.parseInt(data[0]);
        if (responseCode == 200) {
          jsonObjects = new JSONObject(data[1]);
          message = JSONData.getString(jsonObjects, "message");
          flag = JSONData.getBoolean(jsonObjects, "flag");
          if (flag) {
            JSONObject dataObj = jsonObjects.getJSONObject("data");
            UserDataPreferences.saveToken(LoginActivity.this, dataObj.getString("token"));
            UserDataPreferences.saveUserInfo(LoginActivity.this, dataObj.toString());
            Calendar calendar = Calendar.getInstance();
            UserDataPreferences.saveLoginDate(LoginActivity.this, calendar.getTimeInMillis());

            final int cartCount =
                Integer.parseInt(
                    dataObj.has("cart_count")
                        ? (dataObj.isNull("cart_count") ? "0" : dataObj.getString("cart_count"))
                        : "0");
            if (cartCount > 0) {
              UserDataPreferences.saveCartCount(LoginActivity.this, cartCount);
            }
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return null;
    }