【问题标题】:Cannot override doInBackground-method (asyncTask)无法覆盖 doInBackground 方法(asyncTask)
【发布时间】: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[]),它会起作用。为什么我不能传递字符串值?

【问题讨论】:

标签: java android-asynctask


【解决方案1】:
public class AttemptLogin extends AsyncTask<String, Void, JSONObject> {...}

【讨论】:

    【解决方案2】:

    把你的代码改成这样的

    public class AttemptLogin extends AsyncTask<String,Void,JSONObject> {
    ...
    

    你必须告诉 AsyncTask 你的返回类型的类,否则它默认为对象

    【讨论】:

      猜你喜欢
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      相关资源
      最近更新 更多