Android:将字符串传递到asyncTask
我目前正在尝试在我的项目中实现一个线程示例,其中我希望将用户输入的用户名和密码传递到线程中,以便可以检测帐户是否存在,但我在正确实现它时遇到了困难。任何帮助都将不胜感激 这是我收到的一个错误 类“checkPassword2”必须在“AsyncTask”中声明为抽象或实现抽象方法>“doInBackground(Params…) 包com.example.liam.ca3Android:将字符串传递到asyncTask,android,android-asynctask,Android,Android Asynctask,我目前正在尝试在我的项目中实现一个线程示例,其中我希望将用户输入的用户名和密码传递到线程中,以便可以检测帐户是否存在,但我在正确实现它时遇到了困难。任何帮助都将不胜感激 这是我收到的一个错误 类“checkPassword2”必须在“AsyncTask”中声明为抽象或实现抽象方法>“doInBackground(Params…) 包com.example.liam.ca3 import android.app.Activity; import android.content.Intent; im
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.os.AsyncTask;
public class LoginScreen extends Activity {
private final static String TAG = "Sleep";
private static final String defName = "admin";
private static final String defPassword = "admin1";
private ProgressBar mProgressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginscreen);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
final EditText uname = (EditText) findViewById(R.id.username_edittext);
final EditText passwd = (EditText) findViewById(R.id.password_edittext);
final Button loginButton = (Button) findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new checkPassword2().execute(uname, passwd);
if (checkPassword(uname.getText(), passwd.getText())) {
Log.i("8", "accepted password");
// Create an explicit Intent for starting the HelloAndroid Activity
Intent startApp = new Intent(LoginScreen.this,
MainActivity.class);
// Use the Intent to start the HelloAndroid Activity
startActivity(startApp);
} else {
Log.i("8", "failed password");
uname.setText("");
passwd.setText("");
}
}
});
}
private boolean checkPassword(Editable uname, Editable passwd) {
Log.i("2", uname.toString());
Log.i("2", passwd.toString());
if(uname.toString().equalsIgnoreCase(defName) && passwd.toString().equalsIgnoreCase(defPassword))
{
Log.i("8", "this should work");
return true;
}
Log.i("8", "this shouldn't work");
return false;
}
private class checkPassword2 extends AsyncTask<Integer, Integer, String> {
protected int doInBackground(String... values) {
int result = 0;
String nameTry = values[0];
String passTry = values[1];
if(nameTry.equalsIgnoreCase(defName) && passTry.equalsIgnoreCase(defPassword))
{
Log.i("8", "this should work");
result = 1;
}
else {
Log.i("8", "this shouldn't work");
result = 2;
}
for (int i = 1; i < 11; i++) {
sleep();
publishProgress(i * 10);
}
return result;
}
protected void onProgressUpdate(Integer... progress) {
mProgressBar.setProgress(progress[0]);
}
protected void onPostExecute(Long result) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
}
private void sleep() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.e(TAG, e.toString());
}
}
}
}
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.text.Editable;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ProgressBar;
导入android.os.AsyncTask;
公共类登录屏幕扩展活动{
私有最终静态字符串TAG=“Sleep”;
私有静态最终字符串defName=“admin”;
私有静态最终字符串defPassword=“admin1”;
私人ProgressBar mProgressBar;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.loginscreen);
mProgressBar=(ProgressBar)findViewById(R.id.ProgressBar);
最终EditText uname=(EditText)findViewById(R.id.username\u EditText);
最终EditText密码=(EditText)findViewById(R.id.password\u EditText);
最终按钮登录按钮=(按钮)findViewById(R.id.login_按钮);
loginButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
新的checkPassword2().execute(uname,passwd);
if(检查密码(uname.getText(),passwd.getText()){
Log.i(“8”,“接受密码”);
//创建启动HelloAndroid活动的明确意图
Intent startApp=新Intent(LoginScreen.this,
主要活动(课堂);
//使用意图启动HelloAndroid活动
startApp;
}否则{
Log.i(“8”,“密码失败”);
uname.setText(“”);
passwd.setText(“”);
}
}
});
}
私有布尔校验密码(可编辑uname,可编辑passwd){
Log.i(“2”,uname.toString());
Log.i(“2”,passwd.toString());
if(uname.toString().equalsIgnoreCase(defName)和&passwd.toString().equalsIgnoreCase(defPassword))
{
Log.i(“8”,“这应该行”);
返回true;
}
Log.i(“8”,“这不应该起作用”);
返回false;
}
私有类checkPassword2扩展异步任务{
受保护的int DOIN背景(字符串…值){
int结果=0;
字符串nameTry=value[0];
字符串passTry=值[1];
if(nameTry.equalsIgnoreCase(defName)和&passTry.equalsIgnoreCase(defPassword))
{
Log.i(“8”,“这应该行”);
结果=1;
}
否则{
Log.i(“8”,“这不应该起作用”);
结果=2;
}
对于(int i=1;i<11;i++){
睡眠();
出版进度(i*10);
}
返回结果;
}
受保护的void onProgressUpdate(整数…进度){
mProgressBar.setProgress(progress[0]);
}
受保护的void onPostExecute(长结果){
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
}
私人虚空睡眠(){
试一试{
睡眠(500);
}捕捉(中断异常e){
Log.e(标记,e.toString());
}
}
}
}
查看文档:
扩展AsyncTask时,需要指定三个泛型:
- Params是传递给doInBackground()的参数类型
- Progress是传递给onProgressUpdate()和publishProgress()的参数类型
- 结果是传递给onCancell()和onPostExecute()的参数的类型,是doInBackground()的返回类型
- 参数=整数
- 进度=整数
- 结果=字符串
doInBackground(String... values)
Misatch:它需要整数参数,您可以指定字符串参数。但您确实返回了一个整数
onPostExecute(Long result)
不匹配:它需要整数参数,您可以指定长参数
确保方法中的类型符合泛型子句中指定的类型,并且一切正常。您的参数顺序错误
doInBackground()
获取类定义中的第一个参数。另外,onPostExecute()
应该是一个整数
而不是一个长
。多谢了,我实际上事先看过了文档,但在这个过程中把自己弄糊涂了,但你解释它的方式确实有帮助。不过,我还有一个问题,那就是我不确定如何将用户定义的名称和密码传递到线程中<代码>新checkPassword2().execute(uname,passwd)代码>首先,必须将第一个泛型(参数)的类型更改为字符串。其次,在doInBackground(String…values)
方法中,您应该首先检查values
的长度是否为2(values.length==2
),然后用户名是values[0]
,密码是values[1]
。我在代码中实现了相同的一般想法,但这是最初的调用