【发布时间】:2017-03-07 07:52:08
【问题描述】:
为什么我不能在我的班级中覆盖 doInBackground 方法?
public class AttemptLogin extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... args) {
String email = args[2];
String password = args[1];
String name= args[0];
ArrayList params = new ArrayList();
params.add(new BasicNameValuePair("username", name));
params.add(new BasicNameValuePair("password", password));
if(email.length()>0)
params.add(new BasicNameValuePair("email",email));
JSONObject json = jsonParser.makeHttpRequest(URL, "POST", params);
return json;
}
protected void onPostExecute(JSONObject result) {
// dismiss the dialog once product deleted
//Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
try {
if (result != null) {
Toast.makeText(getApplicationContext(),result.getString("message"),Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
错误: 类 AttemptLogin 必须声明为抽象或在 'AsyncTask' 中实现抽象方法 'doInBackground(Params...)
如果我将 doInBackground 的参数更改为 (Object[]),它会起作用。为什么我不能传递字符串值?
【问题讨论】:
-
<>中的这 3 个参数很重要。 docs.oracle.com/javase/tutorial/java/generics/types.html