How to make Asynctask postExecute send variable back to main class

戏子无情 提交于 2020-01-03 03:43:04

问题


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)

回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/10727362/how-to-make-asynctask-postexecute-send-variable-back-to-main-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!