1

I have my class called Main.java which runs the UI. I have another class called VerifyCreds.java which extends AsyncTask. VerifyCreds whole purpose is to run calls to my server on another thread so as to not lockup my UI. I don't have it sublcassed in Main.java because I will have a number of other classes that will be making calls to the server and I would like to have them all use the VerifyCreds class to do it.

What I need to be able to do is pass data back to Main.java from VerifyCreds.java. but getting a error with the below code.

Any help is appreciated! Thanks!

Here is my Main.java code

    package com.coolprograms.zeal;
    <<<imports removed for brevity>>

    public class Main extends Activity 
    {

// Global VAR's
String TAG = "ZEAL";
static Boolean authCode;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    //Grab elements
    final EditText userName = (EditText)findViewById(R.id.usernameBOX);
    final EditText userPWD = (EditText)findViewById(R.id.passwordBOX);
    final Button loginBTN = (Button)findViewById(R.id.loginBTTN);

    //Button listener
    loginBTN.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // Get username and password and add to array
            ArrayList<String> creds = new ArrayList<String>();
            creds.add(userName.getText().toString());
            creds.add(userPWD.getText().toString());

            // Call method that will execute AsyncTask to get creds from server
            verify(creds);  
        }
    });
}

private void verify (ArrayList<String> creds)
{
    // Start AsyncTask to get creds
    new VerifyCredentials(this).execute(creds);
}

public interface credsAuth 
{
    public void authorizedCreds(Boolean authCode);

}

public void verifiedCreds(Context ctx, Boolean serverAuth)
{
    if(serverAuth = true)
    {
        // Move to next screen if we are authorized
        Intent i = new Intent(ctx, Items.class);
        startActivity(i);
    }
}
}

Here is the VerifyCreds.java code

package com.coolprograms.zeal;
<<<imports removed for brevity>>
public class VerifyCredentials extends AsyncTask<ArrayList<String>, Void, Boolean> {

private Context ctx;
ProgressDialog dialog;

public VerifyCredentials(Context applicationContext) 
{
    // TODO Auto-generated constructor stub
    ctx = applicationContext;
    dialog = new ProgressDialog(applicationContext);
}

@Override
protected void onPreExecute()
{
    dialog.setTitle("Please wait");
    dialog.setMessage("Verifying username and password...");
    dialog.show();
}


@Override
protected Boolean doInBackground(ArrayList<String>...creds) 
{   
    //To return
    Boolean serverAnwser = false;

    //Get the creds
    String userID = creds[0].get(0).toString();
    Log.i("ZEAL", "Creds[0]: " + creds[0].get(0).toString());
    String userPWD = creds[0].get(1).toString();
    Log.i("ZEAL", "Creds[1]: " + creds[0].get(1).toString());

    //Get creds from server
    try {
        String serverANW = null;
        URL getCreds = new URL("http://XXX.XXX.XXX.XXX/api/api.php?method=getCreds&id=" + userID + "&pwd=" + userPWD);
        Log.i("ZEAL", "Webservice URL: " + getCreds.toString());

        URLConnection tc = getCreds.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));

        // Get JSON Object
        JSONArray jArray;
        jArray = new JSONArray(in.readLine());

        //Log json object returned
        Log.i("ZEAL", jArray.toString());

        //Check server response
        //for (int i = 0; i < jArray.length(); i++) 
        //{
            JSONObject e = jArray.getJSONObject(0);
            String s = e.getString("RETURN");
            JSONObject jObject = new JSONObject(s);
            serverANW = jObject.getString("RESULT");
            Log.i("ZEAL", "API Result: " + serverANW);
        //}

        if(serverANW.equalsIgnoreCase("True"))
        {
            serverAnwser = true; 
            Log.i("ZEAL", "ServerANW = " + serverANW.toString());
            Log.i("ZEAL", "Setting server anwser to true");
        }
        else
        {
            serverAnwser = false;
            Log.i("ZEAL", "ServerANW = " + serverANW.toString());
            Log.i("ZEAL", "Setting server anwser to false - " + serverAnwser.toString());
        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return serverAnwser;
}

@Override
protected void onPostExecute(final Boolean authCode)
{
    dialog.dismiss();

   // Log what the functions is doing
   Log.d("ZEAL","Server Response: " + authCode.toString());
   Toast.makeText(ctx, "Server says:  " + authCode.toString(), Toast.LENGTH_LONG).show();
   Main m = new Main();
   m.verifiedCreds(m, authCode);

}
}

ERROR:

05-23 15:18:57.190: E/AndroidRuntime(25475): FATAL EXCEPTION: main
05-23 15:18:57.190: E/AndroidRuntime(25475): java.lang.NullPointerException
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.content.ComponentName.<init>(ComponentName.java:75)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.content.Intent.<init>(Intent.java:3174)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at com.coolprograms.zeal.Main.verifiedCreds(Main.java:85)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at com.coolprograms.zeal.VerifyCredentials.onPostExecute(VerifyCredentials.java:117)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at com.coolprograms.zeal.VerifyCredentials.onPostExecute(VerifyCredentials.java:1)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.os.AsyncTask.finish(AsyncTask.java:602)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.os.Looper.loop(Looper.java:137)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at android.app.ActivityThread.main(ActivityThread.java:4424)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at java.lang.reflect.Method.invoke(Method.java:511)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-23 15:18:57.190: E/AndroidRuntime(25475):    at dalvik.system.NativeStart.main(Native Method)
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
Tom
  • 183
  • 3
  • 14

2 Answers2

1

You could create an interface, let Main implement that interface, pass Main as a constructor parameter to the asynctask and have it call Main with the parameter you want to pass back.

The Interface:

public interface CredListener() {
  void verify(Boolean status);
}

In the class definition declare that Main implements CredListener

class Main extends Activity implements CredListener {
...

The new Constructor for VerifyCredentials():

public VerifyCredentials(CredListener listener, Context applicationContext) 
{
  this.listener = lister; // remember the listener

  ctx = applicationContext;
  dialog = new ProgressDialog(applicationContext);
}

In onPostExecute(final Boolean authCode) give the result to the listener:

listener.verify(authCode);

This is a more loose coupling of Main to VerifyCredentials.

Dirk Jäckel
  • 2,979
  • 3
  • 29
  • 47
  • So is there a "standard" way of doing this? I mean should I be doing like Xi said and making asynctask a subclass of Main and any other classes that needs to connect to the server? – Tom May 24 '12 at 01:13
  • No! Don't subclass. Main should implement the Interface. As in "class Main implements CredListener" – Dirk Jäckel May 24 '12 at 04:53
  • Where do I put the Interface CreditListner() at? I thought it would go in the VerifyCredentials class but everywhere I put it it errors with: Syntax error on token "interface", @ expected – Tom May 24 '12 at 20:46
  • Put it either in its own File or inside the class definition of VerifyCredentials() as an inner Interface. – Dirk Jäckel May 24 '12 at 20:50
  • I added "public" to the definition so it can go in its own file. – Dirk Jäckel May 24 '12 at 20:52
  • on, added it to it's own file, but now I can't add this.listener = listener. There is no this.listener – Tom May 24 '12 at 21:01
  • You need a member variable of type CreditListner called listener in VerifyCredentials – Dirk Jäckel May 24 '12 at 21:09
  • @DirkJäckel: Sorry, old thread, but I'm confused: how can the interface be inner to the AsyncTask `VerifyCredentials` if `Main` needs to implement it? Otherwise I get: /The constructor VerifyCredentials(Context, Main) is undefined/ – Luis A. Florit Aug 07 '13 at 03:00
0

You can pass your activity as a parameter to the AsyncTask. In your Main class:

private void verify (ArrayList<String> creds)
{
    // Start AsyncTask to get creds
    VerifyCredentials vc = new VerifyCredentials(this);
    vc.mActivity = this;
    vs.execute(creds);
}

Add this member variable to your VerifyCredentials:

public Main mActivity;

To invoke the calling Activity's method in postExecute():

mActivity.verifiedCreds(m, authCode);

I wouldn't recommend this though. I would just create a sub-class of VerifyCredentials in the Main Class.

Xi 张熹
  • 10,492
  • 18
  • 58
  • 86
  • You should not pass Main as Main. You should create an interface with a callback method. Main should then implement that interface and be passed as such. – Dirk Jäckel May 23 '12 at 22:50