1

一个Activity(SignInActivity) 正在调用一个方法,在FunkcjeAPI该方法中执行一个AsyncTask.

AsyncTask应该显示一个ProgressDialog使用呼叫Activity。我不知道如何给Activity构造函数一个正确的。我尝试了很多东西,阅读了很多关于 SO 的教程和问题,但我找不到解决方案。FunkcjeAPI不是一个活动,所以我不能写new Logowanie(this).execute(argumenty);


AsyncTask调用代码:

public class FunkcjeAPI {

    static String dozwrotu = null;

public static String zalogujSie(final String nick, final String haslo)
{
    String[] argumenty = {nick, haslo};
    new Logowanie(/* WHAT HERE ? */).execute(argumenty); // HELP ME IN THAT LINE !!!!!!!!!!!!!
    return dozwrotu;


}

我的AsyncTask课程代码(在FunkcjeAPI课堂上):

private class Logowanie extends AsyncTask<String, Void, String>
    {

        Activity wywolujaceActivity;

        public Logowanie(Activity wywolujaceActivity) {
            this.wywolujaceActivity = wywolujaceActivity;
        }

        @SuppressWarnings("deprecation")
        @Override
        protected void onPreExecute() {
            wywolujaceActivity.showDialog(SignInActivit.PLEASE_WAIT_DIALOG);
        }

        @Override
        protected String doInBackground(final String... argi) {
            final JSONParser jParser = new JSONParser();
            new Thread(new Runnable() {
            public void run() {     
            final String json = jParser.getJSONFromUrl("http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + argi[0] + "&password=" + argi[1] + "&imei=");
            Handler mainHandler = new Handler(Looper.getMainLooper());
            mainHandler.post(new Runnable() {

                @Override
                public void run() {
                    JSONObject jObject;
                    try {

                        jObject = new JSONObject(json);
                        Log.wtf("Link", "http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + argi[0] + "&password=" + argi[1] + "&imei=");
                        Log.wtf("Link", json);
                        String error = jObject.getString("error");
                        if(error == "You reached daily query limit !") { nadajWartosc("You reached daily query limit !"); }
                        if(error == "0") {nadajWartosc(jObject.getString("token"));}
                        if(error == "1") {nadajWartosc("1");}
                        if(error == "Invalid username") {nadajWartosc("Invalid username");}
                        if(error == "Invalid password") {nadajWartosc("Invalid password");}
                        if(error == "This user is already logged in !") {nadajWartosc("This user is already logged in !");}
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                    catch (NullPointerException e)
                    {
                        e.printStackTrace();
                    }

                }

            });   
                }}).start();
            return dozwrotu;
        }

        @Override
        protected void onPostExecute(String result) {
            wywolujaceActivity.removeDialog(SignInActivit.PLEASE_WAIT_DIALOG);

        }


    }
4

1 回答 1

1

zalogujSie()向采用 的方法添加一个参数Activity,然后使用此参数启动 AsyncTask:

public static String zalogujSie(Activity activity, final String nick, final String haslo)
{
    // .....
    new Logowanie(activity).execute(argumenty); 
    return dozwrotu;
}

然后你会像这样从活动中调用这个方法:

FunkcjeAPI.zalogujSie(this, "Nick", "Haslo"); 
于 2013-07-28T12:39:19.357 回答