创建 AsyncTask 时出错

发布于 2024-11-24 12:16:48 字数 1695 浏览 1 评论 0原文

我正在创建一个登录表单。当用户登录时,将进入主页。

我创建了一个具有 AsyncTask 的活动。这是我的代码的相关部分:

public class iniTask extends AsyncTask<String, Void, String> {
    private ProgressDialog Dialog = new ProgressDialog(GPSTracerActivity.this);

    protected void onPreExecute() {
        Dialog.setMessage("Connect to server...");
        Dialog.show();
    }

    protected String doInBackground(String... url_req) {
        String url = url_req[0];
        try {
            Log.v("doing background", executeHttpGet(url));
            return executeHttpGet(url);
        } catch(Exception e) { 
            Log.v("Exception doing background","Exception:"+e.getMessage());
            return ""; 
        }
    }

    protected void onPostExecute(String result) { 
        try {
                           Dialog.dismiss();
                        // here when thing go    wrong
                startNewAction(result);     


        } catch(Exception e) {
            Log.v("Exception process response","Exception:"+e.getMessage());
        } 
    }
}

这是 startNewAction(result)

public void startNewAction(String result){
    if (result.substring(0, 6) == "300 OK"){
        Intent i = new Intent(GPSTracerActivity.this, Home.class);
        startActivity(i);
    }
}

任务正确启动,但是当我调用 startNewAction(result) 时, 它不会调用新的活动。为什么?

注意:当我启用 if 结构来测试 string == 300 OK 时,它不起作用!为什么

我在 logcat 中看到这个:

07-16 14:57:23.345: WARN/InputManagerService(37): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40777ee0

I'm creating a login form. When the user logs in, it will lead to the home page.

I create an activity that has an AsyncTask. Here's the relevant part of my code:

public class iniTask extends AsyncTask<String, Void, String> {
    private ProgressDialog Dialog = new ProgressDialog(GPSTracerActivity.this);

    protected void onPreExecute() {
        Dialog.setMessage("Connect to server...");
        Dialog.show();
    }

    protected String doInBackground(String... url_req) {
        String url = url_req[0];
        try {
            Log.v("doing background", executeHttpGet(url));
            return executeHttpGet(url);
        } catch(Exception e) { 
            Log.v("Exception doing background","Exception:"+e.getMessage());
            return ""; 
        }
    }

    protected void onPostExecute(String result) { 
        try {
                           Dialog.dismiss();
                        // here when thing go    wrong
                startNewAction(result);     


        } catch(Exception e) {
            Log.v("Exception process response","Exception:"+e.getMessage());
        } 
    }
}

Here's startNewAction(result):

public void startNewAction(String result){
    if (result.substring(0, 6) == "300 OK"){
        Intent i = new Intent(GPSTracerActivity.this, Home.class);
        startActivity(i);
    }
}

The task starts correctly, but when I call startNewAction(result),
it does not call a new activity. Why?

NOTE : when i enable if structure to test string == 300 OK it is not work ! why

I see this in logcat:

07-16 14:57:23.345: WARN/InputManagerService(37): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40777ee0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2024-12-01 12:16:48

在 onPostExecute 中,首先关闭对话框。

Dialog.dismiss();

In your onPostExecute dismiss the dialog first.

Dialog.dismiss();
漆黑的白昼 2024-12-01 12:16:48

我刚刚找到了解决方案,由于我比较字符串的方式而出现错误,

应该是,

      if (result.substring(0, 6).equals("300 OK") ){
                Intent i = new Intent(GPSTracerActivity.this, Home.class);
                startActivity(i);               
      }

无论如何,感谢您的想法!

I just found the solution,there is an error because of the way i compare the string,

It should be,

      if (result.substring(0, 6).equals("300 OK") ){
                Intent i = new Intent(GPSTracerActivity.this, Home.class);
                startActivity(i);               
      }

Anyway, Thanks for ideas !!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文