奇怪:共享首选项代码后意图不工作

时间:2016-11-10 11:10:19

标签: android web-services android-studio android-intent sharedpreferences

我有一些奇怪的问题。意图在共享首选项代码之后无效。如果我在共享首选项代码之前写了 SAME 意图,那么它正在运行。以下是我的代码......

检查 fetchData()方法...

Login_Activity

    public class Login_Activity extends AppCompatActivity implements View.OnClickListener {

    // Widgets
    private Spinner spnr_login_type;
    private EditText edt_login_id, edt_pwd;
    private Button btn_login, btn_regi;
    private ProgressDialog progressDialog;

    // Variables
    private String url = "login.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Checking User Already Login OR Not
        if (Preferences.getPreferences(getApplicationContext()).contains(Preferences.KEY_USER_NAME)) {
            Intent myIntent = new Intent(Login_Activity.this, Home_Activity.class);
            startActivity(myIntent);
            finish();
        }
        find_view_by_id();
        registerClickListeners();
        progressDialog = new ProgressDialog(this);
        progressDialog.setIndeterminate(true);
    }

    private void find_view_by_id() {
        spnr_login_type = (Spinner) findViewById(R.id.spnr_login_type);
        edt_login_id = (EditText) findViewById(R.id.edt_login_id);
        edt_pwd = (EditText) findViewById(R.id.edt_pwd);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_regi = (Button) findViewById(R.id.btn_regi);
    }

    private void registerClickListeners() {
        btn_login.setOnClickListener(this);
        btn_regi.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                if (Commom_Methods.isNetworkAvailable(getApplicationContext())) {
                    fetchData();
                } else {
                    Toast.makeText(Login_Activity.this, "Please, Coonect to internet!!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_regi:
                Intent myIntent = new Intent(Login_Activity.this, Regi_Activity.class);
                startActivity(myIntent);
                break;
        }
    }

    private void fetchData() {
        StringRequest stringReq = new StringRequest(Request.Method.POST, Constants.base_url + url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e("Resqq:", response);
                progressDialog.dismiss();
                if (response != null) {
                    try {
                        JSONObject jo = new JSONObject(response);
                        if (jo.has("success")) {
                            JSONObject joObj = jo.getJSONObject("user");

                            // Storing To Shared Preferences
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_SL_ID, joObj.getString("sl_id"));
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_TUTION_CENTER_SL, joObj.getString("tution_center_sl"));
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_BATCH_SL, joObj.getString("batch_sl"));
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_BATCH_GRUP_SL, joObj.getString("batch_grup_sl"));
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_CO_SL, joObj.getString("co_sl"));
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_DRANGE_SL, joObj.getString("drange_sl"));

                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_REG_NO, joObj.getString("reg_no"));
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_USER_NAME, joObj.getString("name"));
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_USER_EMAIL, joObj.getString("email"));
                            Preferences.writeString(getApplicationContext(),
                                    Preferences.KEY_USER_MOB, joObj.getString("mobile_no"));

                            Intent myIntent = new Intent(Login_Activity.this, Home_Activity.class);
                            startActivity(myIntent);
                            finish();

                        } else {
                            Toast.makeText(getApplicationContext(), jo.getString("error_msg"), Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(Login_Activity.this, "Server error! Server sent empty data!! Try again later.", Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                progressDialog.dismiss();
                Toast.makeText(getApplicationContext(), "Error:" + error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("type", spnr_login_type.getSelectedItem().toString().toLowerCase());
                params.put("reg_no", edt_login_id.getText().toString());
                params.put("password", edt_pwd.getText().toString());
                return params;
            }
        };
        RequestQueue queue = Volley.newRequestQueue(this);
        queue.add(stringReq);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Fetching your details!");
        progressDialog.show();
        //progressDialog.show(new ContextThemeWrapper(this, R.style.DialogCustom), "Fetching your details!", "");
    }
}

以下是我的回复......

    {
  "success": true,
  "user": {
    "sl_id": "1",
    "reg_no": "21711101",
    "name": "Akash Bharadwaj N",
    "photo": "",
    "email": "",
    "type": "student"
  },
  "api_key": "APIkey Here"
}

1 个答案:

答案 0 :(得分:0)

if (Preferences.getPreferences(getApplicationContext()).contains(Preferences.KEY_USER_NAME)) 

必须返回false 检查其布尔值