我正在开发一个使用asyncTask的基于注册的项目。但是我的params和后台使用任务出错了。
Snippet -
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);
}}
错误讯息 -
错误:(56,50)错误:非法启动类型
中的方法
错误:(65,9)错误: 方法不会覆盖或实现超类型
我该如何解决这些问题? 我尝试在params中更改返回类型,但仍然无法解决错误。
答案 0 :(得分:1)
试试这个
- 参数
<{1}}
Void
void
- 中传递
您需要更改
AsyncTask
方法,只需在onPostExecute()
方法String
参数即可 醇>
更改您的代码,如下面的代码
示例代码
onPostExecute()
的更多信息
答案 1 :(得分:0)
1.改变虚空无效
2.更改您的onPostExecute()
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), "Internet not found", Toast.LENGTH_SHORT).show();
}
答案 2 :(得分:0)
改变词语&#34; void&#34;在class RegisterUser extends AsyncTask <String, void, String> implements abcd.project2.RegisterUser
异步任务使用的三种类型如下:
结果,后台计算结果的类型。
并非所有类型都始终由异步任务使用。要将类型标记为未使用,只需使用类型Void
:
私有类MyTask扩展了AsyncTask {...}