使用HTTP AsyncTask登录

时间:2015-05-20 00:15:22

标签: android android-asynctask

我一直在努力使用AsyncTask。我找不到错误。我不明白为什么AsyncTask不起作用,虽然我所拥有的是String和Boolean这里。请帮忙。谢谢。

onPostExecute部分的错误说:

  

该方法没有覆盖超类的方法。

    public class UserLoginTask extends AsyncTask<String, String, String> {

    private final String mEmail;
    private final String mPassword;

    UserLoginTask(String email, String password) {
        mEmail = email;
        mPassword = password;
    }

    @Override
    protected String doInBackground(String... arg0) {
        try{
            for (String credential : DUMMY_CREDENTIALS) {
                String[] pieces = credential.split(":");
                if (pieces[0].equals(mEmail) && pieces[1].equals(mPassword)) {
                    // Account exists, return true if the password matches.
                    pieces[0]=arg0[0];
                    pieces[1]=arg0[1];
                }
            }
            String username = (String)arg0[0];
            String password = (String)arg0[1];
            Log.i("Username", username);
            Log.i("Password", password);
            String link="http://";
            Log.i("link", link);

            // Prep HTTP for use
            HttpClient client=new DefaultHttpClient();
            HttpPost post = new HttpPost(link);

            // Prep data for sending to server
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            pairs.add(new BasicNameValuePair("username", username));
            pairs.add(new BasicNameValuePair("password", password));
            post.setEntity(new UrlEncodedFormEntity(pairs));

            // Send data to server
            HttpResponse response = client.execute(post);

            String responseText = null;

            // Time to evaluate how server reacted
            try {
                // try to get server response
                responseText = EntityUtils.toString(response.getEntity());
                Log.i("Server Response: ", responseText);
            }
            catch (Exception e) {
                // Failed to get server response, log why it failed.
                e.printStackTrace();
                Log.i("Parse Exception", e + "");
            }
            return responseText;
        }
        catch(Exception e){
            Log.i("exception", "error");
            return new String("Exception: " + e.getMessage());
        }
    }

    @Override
    protected void onPostExecute(final boolean success) {
        mAuthTask = null;
        finish();
        if (success) {
            finish();
            Intent myIntent = new Intent(membership.this, membership_memberonly.class);
            membership.this.startActivity(myIntent);
        } else {
            mPasswordView.setError(getString(R.string.error_incorrect_password));
            mPasswordView.requestFocus();
        }
    }

    @Override
    protected void onCancelled() {
        mAuthTask = null;
    }
}

3 个答案:

答案 0 :(得分:1)

嗯,你有2个doInBackground方法。只有其中一个应该存在。

答案 1 :(得分:0)

在asyntask和protected

中找到两个doInBackground
  

String doInBackground(String ... arg0)

应该是

  

protected String doInBackground(Object ... arg0)

     

pieces [0] = arg0 [0] .toString();
  pieces [1] = arg0 [1] .toString();

而不是String输入。

答案 2 :(得分:0)

如何获得这些覆盖方法?您是否在此覆盖方法中出现任何错误行?

 @Override
            protected String doInBackground(String... arg0) {

            }

//这里你应该只使用布尔返回类型,你不能使用String。否则你可以使用Object代替String,如下面的方法

protected String doInBackground(Object... params) {
        // TODO Auto-generated method stub
        return null;
    }

删除此覆盖方法,如果您无法编写正确的重写方法,请执行以下操作;

右键单击该方法 - 单击Source - override / Implement方法,然后您可以获取方法的指定覆盖方法,

 @Override
            protected Boolean doInBackground(Void... params) {
                for (String credential : DUMMY_CREDENTIALS) {
                    String[] pieces = credential.split(":");
                    if (pieces[0].equals(mEmail)) {
                        // Account exists, return true if the password matches.
                        return pieces[1].equals(mPassword);
                    }
                }
                return false;
            }

在此之后,方法应该存在,然后您将收到错误,并在您的条件中稍后调用finish()方法。