Login via HTTP AsyncTask

I struggled with AsyncTask. I could not find a mistake. I do not understand why AsyncTask does not work, although all I have is String and Boolean below. Please help me. Thank.

Error in the onPostExecute part:

The method did not override the method from its superclass.

    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;
    }
}
+4
source share
3 answers

Well, you have 2 doInBackground methods. Only one of them must exist.

+1
source

Found two doInBackground in your asyntask and protected

String doInBackground (String ... arg0)

it should be

protected String doInBackground (Object ... arg0)

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

instead of entering a string.

0
source

? ?

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

            }

// here you should use only a type of type Boolean, you cannot use String. otherwise you can use Object instead of String, as shown below mothod

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

Remove this override method. If you were unable to write the correct overridden methods, then do the following:

right click on the method - click on "Source - override / implement", then you can get override methods for your method,

 @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;
            }

After this method must exist, then you will receive an error message. Also call the finish () method later in your state.

0
source

Source: https://habr.com/ru/post/1588900/


All Articles