【发布时间】:2018-06-01 11:05:27
【问题描述】:
我有一个asynctask,它正在从我的 oracle 数据库中获取一个 userPin。在我的主线程中,然后在一个按钮上运行asynctask。将数据库中的值分配给名为userPinRetrieved 的变量。
当我调试这个变量时,它正在接收正确的值。但是,当我正常运行应用程序时,它会收到 null。在做了一些研究并使用Thread.Sleep(x) 之后,我可以看到这是由于 asynctask 没有及时将结果返回给主线程和变量。
有人建议我不要使用Thread.Sleep(x),我有什么替代方案?
这是我的代码:
异步任务:
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((line=br.readLine()) != null) {
JSONObject json = new JSONObject(line);
Log.d("Line",line);
if (json.getString("userPin") == null){
userPinRetrieved = "PIN NOT RECEIVED";
Log.d("userpin", userPinRetrieved);
} else {
userPinRetrieved = json.getString("userPin");
Log.d("userpin", userPinRetrieved);
}
}
}
} catch (Exception e) {
Log.v("ErrorAPP", e.toString());
}
return "";
}
@Override
protected void onPostExecute(String userPinRetrieved) {
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
}
登录按钮:
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTaskRunner postReq = new AsyncTaskRunner();
postReq.execute("start");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(UserLogin.this, userPinRetrieved + " " + userPin, Toast.LENGTH_LONG).show();
if (userPin.equals(userPinRetrieved)) {
Toast.makeText(UserLogin.this, "Access Granted!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Toast.makeText(UserLogin.this, "Hello " + employee, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(UserLogin.this, "Access Denied! Incorrect Pin", Toast.LENGTH_SHORT).show();
}
}
});
谁能给点建议?
谢谢
【问题讨论】:
-
你成功了吗?
-
@LeviAlbuquerque 是的,男人在下面使用了您的答案!谢谢
标签: java android multithreading android-asynctask