android - AsyncTask 使用 json 从 php 获取数据
问题描述
我只是Android的初学者。我想将 Android 与 php 连接以进行登录活动,它连接成功,但是每当我输入 emp_id 正确时,它什么都不显示,并且不发送到其他活动,而在错误的凭据上它显示无效的 ID 或密码,有什么问题,JSONobject
因为我在错误的 emp_id 上显示了Toast
msg 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();
}
解决方案
推荐阅读
- apache-spark - 带有 Pyspark 3.2、Hadoop 3.3.1 的 S3FS 连接器挂在 spark.read.load() 上
- angular - 无法绑定到“ngModel”,因为即使在 app.module.ts 中包含 FormsModule 后,它也不是“输入”的已知属性
- ios - 如何将图像更改为结构?SwiftUI
- vim - 有没有办法用键绑定关闭 Vim 内置终端?
- wordpress - 如何在 VScode 上本地预览 wordpress 网站?
- java - Spring Boot 无法从 redis 缓存序列化程序中读取数据。SerializationException:无法反序列化
- c# - DynamoDb SaveAsync 与运行时生成的对象
- python - Pandas 获取列中每个唯一值的行数
- python - 需要帮助将 Python 脚本转换为 PHP
- python - Numpy: How to get the sums of integer array slices based on indexes from another array in a vectorized manner?