无法覆盖doInBackground方法(asyncTask)

时间:2016-10-25 10:03:19

标签: java android-asynctask

为什么我不能覆盖我班级中的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声明为abstract或在'AsyncTask'中实现抽象方法'doInBackground(Params ...)

如果我将doInBackground的参数更改为(Object []),它会起作用。为什么我无法传递String值?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

将您的代码更改为此类

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

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