Friday, 29 April 2016

aquiry

public void asyncJson() {
        ConstantData.Displayprogress(Login.this);

        //perform a Google search in just a few lines of code
        JSONObject postJson = new JSONObject();

        JSONObject obj = new JSONObject();

        JSONArray array = new JSONArray();

        TelephonyManager telephonyManager = (TelephonyManager) Login.this                .getSystemService(Context.TELEPHONY_SERVICE);


        try {

            obj.put("userid", "" + etemail.getText());
            obj.put("password", "" + etpassword.getText());
            obj.put("deviceId", "" + telephonyManager.getDeviceId());


            Log.i("json", obj.toString());


        } catch (Exception e) {
            // TODO: handle exception        }


        final AQuery aq = new AQuery(Login.this);


        aq.post(ConstantData.LoginSevice, obj, JSONObject.class, new AjaxCallback<JSONObject>() {

            @Override            public void callback(String url, JSONObject json, AjaxStatus status) {


                if (json != null) {

                    try {
                        ConstantData.progressclose(Login.this);

                        JsonResult jsonResult = new JsonResult();
                        JsonElement json2 = new JsonParser()
                                .parse(json.toString());

                        Type type = new TypeToken<JsonResult>() {
                        }.getType();
                        jsonResult = new Gson().fromJson(json2,
                                type);

                        if (jsonResult.getResult().equalsIgnoreCase("success")) {

                            if (jsonResult.getData().isToken()) {


                                SharedPreferences editor = getSharedPreferences(
                                        ConstantData.MyPREFERENCES, 0);

                                SharedPreferences.Editor editor2 = editor
                                        .edit();

                                Intent intent = new Intent(Login.this, FragmentContainerActivity.class);

                                if (editor.getString("userid", "").equalsIgnoreCase(""))
                                    intent.putExtra("isfirsttime", true);
                                else                                    intent.putExtra("isfirsttime", false);


                                if (ck_reamind.isChecked())
                                    editor2.putBoolean("checked", true);
                                else                                    editor2.putBoolean("checked",
                                            false);

                                editor2.putString("userid", etemail                                        .getText().toString());
                                editor2.putString("password", etpassword                                        .getText().toString());
                                editor2.commit();

                                String urll = jsonResult.getData().getProfile().getUpLoadPhoto();

                                urll = urll.replace("~", "");


                                Log.v("path is", ConstantData.DISPLAY_PICTUR_URL + urll);

                                new LoadImage().execute(ConstantData.DISPLAY_PICTUR_URL + urll);


                                if (ck_reamind.isChecked())
                                    Util.setSharePrefranceData(Login.this, jsonResult.getData().getProfile().getJsonString(), ConstantData.SHARED_PREFERENCES_Profile_Object);


                                startActivity(intent);
                                finish();


//                                Toast.makeText(aq.getContext(), jsonData.getString(ConstantData.MESSAGE), Toast.LENGTH_LONG).show();                            } else {

                                ConstantData.DisplayAlert("Message", jsonResult.getData().getMessage(), Login.this);

                            }
                        }

                    } catch (Exception ex) {
                        Toast.makeText(aq.getContext(), "Error :" + json.toString(), Toast.LENGTH_LONG).show();
                    }


                } else {
                    ConstantData.progressclose(Login.this);
                    //ajax error, show error code                    Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
                }
            }
        });

    }

No comments:

Post a Comment