首页 > 解决方案 > AsyncTask 使用 json 从 php 获取数据

问题描述

我只是Android的初学者。我想将 Android 与 php 连接以进行登录活动,它连接成功,但是每当我输入 emp_id 正确时,它什么都不显示,并且不发送到其他活动,而在错误的凭据上它显示无效的 ID 或密码,有什么问题,JSONobject因为我在错误的 emp_id 上显示了Toastmsg JSONobject,但在有效的 empid 上显示了一个 msg,但未显示 toast msg

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

    empid = (TextView) findViewById(R.id.empid);
    emppsd = (TextView) findViewById(R.id.emppsd);
    Button btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            userLogin();
        }
    });

}

private void userLogin() {
    //first getting the values
    final String emp_id = empid.getText().toString();
    final String password = emppsd.getText().toString();

    //validating inputs
    if (TextUtils.isEmpty(emp_id)) {
        empid.setError("Please enter your username");
        emppsd.requestFocus();
        return;
    }

    if (TextUtils.isEmpty(password)) {
        emppsd.setError("Please enter your password");
        emppsd.requestFocus();
        return;
    }
    class UserLogin extends AsyncTask<Void, Void, String> {

        ProgressBar progressBar;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressBar.setVisibility(View.GONE);


            try {
                //converting response to json object
                JSONObject obj = new JSONObject(s);



                //if no error in response
                if (!obj.getBoolean("error")) {

                    Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                    //getting the user from the response
                    JSONObject userJson = obj.getJSONObject("user");


                    //creating a new user object
                    User user = new User(
                            userJson.getString("emp_name"),
                            userJson.getString("designation"),
                            userJson.getString("phn no"),
                            userJson.getString("empid")
                    );


                    //storing the user in shared preferences
                    SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);

                    //starting the main activity
                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                  Toast.makeText(login.this,"your credential is correct",Toast.LENGTH_LONG).show();

                } else {
                    Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected String doInBackground(Void... voids) {
            //creating request handler object
            RequestHandler requestHandler = new RequestHandler();

            //creating request parameters
            HashMap<String, String> params = new HashMap<>();
            params.put("emp_id", emp_id);
            params.put("password", password);

            //returing the response
            return requestHandler.sendPostRequest(URLs.URL_LOGIN, params);
        }
    }

    UserLogin ul = new UserLogin();
    ul.execute();
}

标签: android

解决方案


推荐阅读