gpt4 book ai didi

android - 如何修复 AsyncTask 参数错误?

转载 作者:行者123 更新时间:2023-11-30 00:00:56 24 4
gpt4 key购买 nike

我正在开发一个使用 asyncTask 的基于注册的项目。但是我在它的参数和后台使用任务上遇到了错误。

fragment -

public class signupActivity extends AppCompatActivity {
EditText edit_name;
EditText edit_usn;
EditText edit_addnum;
EditText edit_pass;
EditText edit_repass;
Button btn_sign;
private static final String REGISTER_URL="http://abcd.000webhostapp.com/signup.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
edit_name=(EditText)findViewById(R.id.id_name);
edit_usn=(EditText)findViewById(R.id.id_usn);
edit_addnum=(EditText)findViewById(R.id.id_add);
edit_pass=(EditText)findViewById(R.id.id_pass);
edit_repass=(EditText)findViewById(R.id.id_repass);
btn_sign=(Button)findViewById(R.id.btn_signup);
btn_sign.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
registerUser();
}
});
}

private void registerUser() {
String name=edit_name.getText().toString().trim().toLowerCase();
String usn=edit_usn.getText().toString().trim().toLowerCase();
String addnum=edit_addnum.getText().toString();
String pass=edit_pass.getText().toString().trim().toLowerCase();
String repass=edit_repass.getText().toString().trim().toLowerCase();
register(name, usn, addnum, pass, repass);
}
private void register(String name,String usn,String addnum,String pass,String repass) {

String urlsuffix = "?name=" + name + "&usn=" + usn + "&ddnum=" + addnum + "&pass=" + pass + "&repass=" + repass;

//Getting **illegal start of type** for void keyword here
class RegisterUser extends AsyncTask <String, void, String> implements abcd.project2.RegisterUser {
ProgressDialog loading;

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(signupActivity.this, "please wait", null, true, true);
}

//Getting **method does not override or implement a method from a supertype** for override here
@Override
protected void onPostExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "Internet not found", Toast.LENGTH_SHORT).show();

}


@Override
protected String doInBackground(String... params) {
String s = params[0];
BufferedReader bufferReader = null;
try {
URL url = new URL(REGISTER_URL + s);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result = bufferReader.readLine();
return result;
} catch (Exception e) {
return null;
}
}
}
RegisterUser ur = new RegisterUser();
ur.execute(urlsuffix);
}
public void openCreateList(View view) {
Intent i = new Intent(this, createActivity.class);
startActivity(i);

}}

错误信息-

Error:(56, 50) error: illegal start of type
Error:(65, 9) error: method does not override or implement a method from a supertype

我该如何解决这些问题?我尝试更改参数中的返回类型,但仍然无法解决错误。

最佳答案

试试这个

  1. Its Void not void in parameter of AsyncTask

  2. You need to change your onPostExecute() method just pass String parameter in onPostExecute() method

像下面的代码一样更改您的代码

示例代码

private void register(String name,String usn,String addnum,String pass,String repass) {

String urlsuffix = "?name=" + name + "&usn=" + usn + "&ddnum=" + addnum + "&pass=" + pass + "&repass=" + repass;

//Getting **illegal start of type** for void keyword here
class RegisterUser extends AsyncTask<String, Void, String> implements abcd.project2.RegisterUser {
ProgressDialog loading;

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(signupActivity.this, "please wait", null, true, true);
}


@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}


@Override
protected String doInBackground(String... params) {
String s = params[0];
BufferedReader bufferReader = null;
try {
URL url = new URL(REGISTER_URL + s);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
bufferReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String result;
result = bufferReader.readLine();
return result;
} catch (Exception e) {
return null;
}
}
}
RegisterUser ur = new RegisterUser();
ur.execute(urlsuffix);
}

您可以阅读更多关于 AsyncTask 的信息

关于android - 如何修复 AsyncTask 参数错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49934134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com