Aaron Paul Mallavo
Aaron Paul Mallavo

Reputation: 11

Error in asyncTask #1

Im having trouble in the asynctask. Having fatal error. This is the part where the error occurs. It tells me String success is unused. The whole code is now posted. Help pleaseIm having trouble in the asynctask. Having fatal error. This is the part where the error occurs. It tells me String success is unused. The whole code is now posted. Help pleaseIm having trouble in the asynctask. Having fatal error. This is the part where the error occurs. It tells me String success is unused. The whole code is now posted. Help pleaseIm having trouble in the asynctask. Having fatal error. This is the part where the error occurs. It tells me String success is unused. The whole code is now posted. Help please

package com.example.ram2;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;

import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity implements OnClickListener {

    EditText user, pass;
    Button mSubmit, mRegister;

    ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();

    private static final String LOGIN_URL = "http://10.0.2.2/FuckAnd/login.php";

    static final String TAG_SUCCESS = "success";
    static final String TAG_MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);

        mSubmit = (Button) findViewById(R.id.login);
        mSubmit.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
            new AttemptLogin().execute();

    }

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

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Logging in...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            pDialog.dismiss();
            finish() ;
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            String username = user.getText().toString();
            String password = pass.getText().toString();
            String success = null;

                try {

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));               
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");

                JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                        params);

                Log.d("Login attempt", json.toString());

                success= json.getString("success");

                   } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    return null;
        }

        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            //JSONObject json;
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            //if (TAG_SUCCESS == null) {

            if (result.equals("success")) {
            //if (success.equals(success)) {
                //Log.d("Login Successful!", json.toString());
                Toast.makeText(Login.this, "Login Successful!", Toast.LENGTH_SHORT).show();
                //Toast.makeText(LoginActivity.this, "Login Successful!", Toast.LENGTH_LONG).show();
                //Intent i = new Intent(LoginActivity.this, PortalContents.class);
                finish();
                //startActivity(i);
                //return null;

}
              else {
                //Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                Toast.makeText(Login.this, "Login Fail!", Toast.LENGTH_LONG).show();
                //return json.getString(TAG_MESSAGE);
                //return json.getString(TAG_MESSAGE);

            }

        }

}

}

Upvotes: 0

Views: 106

Answers (2)

Gravitoid
Gravitoid

Reputation: 1402

You need to return the type String from the doInBackground as the onPostExecute is expecting that. The return from doInBackground also needs to match the third parameter on the AsyncTask declaration which is the expected type of the onPostExecute (each parameter on the AsyncTask needs to match the onPreExecute, doInBackground, and onPostExecute, respectively; be aware that the first two are arrays of the data type).

So your return null is going to cause an issue with the onPostExecute(String result).

More info about AsyncTasks can be found in the developer guide at http://developer.android.com/reference/android/os/AsyncTask.html.

Upvotes: 1

Misagh Emamverdi
Misagh Emamverdi

Reputation: 3674

Try this:

protected String doInBackground(String... args) {
      // TODO Auto-generated method stub
      String username = arg[0];
      String password = arg[1];
      String success = null; //this keeps getting error
//...
}

And:

new Task().execute(user.getText().toString(),pass.getText().toString());

Upvotes: 0

Related Questions