1

我正在尝试创建一个本机登录屏幕,其中用户名和密码框在处理请求时滑出屏幕(如果登录不成功,则滑回屏幕)。

为了实现这一点,我定义了我的动画 ( DropDownAnimation) 并将其分配给我的LinearLayout(页脚)。当用户单击登录按钮时,我启动动画,然后调用一个函数 ( tryLogin()),该函数启动一个AsyncTask. AsyncTask处理创建和发送登录请求以及获取JSONObject响应的所有工作。

但是,我的问题是slideDown动画直到完成后才开始AsyncTask。这在成功登录时看起来并没有那么糟糕,但在登录失败时,这意味着LinearLayout永远不会向下滑动 - 它会跳到屏幕底部,开始slideUp动画回到原来的位置。

这似乎是与这个问题类似的问题,但我没有使用,而且我bindService()所有的非 UI 代码(对我而言)似乎都包含在其中AsyncTask。LogCat 告诉我:

06-24 04:37:35.141: I/Choreographer(5347): Skipped 137 frames! The application may be doing too much work on its main thread.

我假设那些是页脚会向下滑动的帧 - 但我无法弄清楚我在主线程上执行的东西在哪里。这是我的 LoginPage 和 LoginTask 代码。

登录页面.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_page);

    login = (Button) findViewById(R.id.login);
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    footer = (LinearLayout) findViewById(R.id.footer);

    // We must wait for the layout to be finalised before trying to find heights.
    ViewTreeObserver vto = footer.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            initAnimations();
        }
    });

    loading = (TextView) findViewById(R.id.loading);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String mUsername = username.getText().toString();
            String mPassword = password.getText().toString();

                            // Neither of these two things happen until after LoginTask is done.
            footer.startAnimation(slideDown);
            loading.setVisibility(TextView.VISIBLE);

            tryLogin(mUsername, mPassword);
        }
    });
}

protected void tryLogin(String mUsername, String mPassword) {
    Exception e;
    String loginUrl = getString(R.string.login_url);
    String clientId = getString(R.string.client_id);
    String clientSecret = getString(R.string.client_secret);
    LoginTask loginTask = (LoginTask) new LoginTask().execute(mUsername, mPassword, loginUrl, clientId, clientSecret);
    if ((e = loginTask.getException()) != null) {
        Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    } else {
        JSONObject response;
        try {
            response = loginTask.get();
            Log.d("login", response.toString());
            if (!response.has("access_token")) {
                loading.setVisibility(TextView.INVISIBLE);
                footer.startAnimation(slideUp);
                Toast.makeText(this, "Login error", Toast.LENGTH_LONG).show();
            } else {
                Intent i = new Intent(this, FullscreenWebView.class);
                i.putExtra("accessToken", response.get("access_token").toString());
                startActivity(i);
                overridePendingTransition(0, 0);
            }
        } catch (InterruptedException e1) {
            e1.printStackTrace();
            Thread.currentThread().interrupt();
        } catch (ExecutionException e1) {
            e1.printStackTrace();
        } catch (JSONException e1) {
            e1.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

登录任务.java

class LoginTask extends AsyncTask<String, Void, JSONObject> {
    private Exception exception;

    @Override
    protected JSONObject doInBackground(String... params) {
        HttpURLConnection connection;
        OutputStreamWriter request = null;

        URL url = null;
        JSONObject response = null;
        String parameters = "grant_type=password&username="+params[0]+"&password="+params[1]+"&client_id="+params[3]+"&client_secret="+params[4];

        try {
            url = new URL(params[2]);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
            connection.setRequestMethod("POST");

            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            request.close();

            // username or password is probably wrong
            Log.d("login", ""+connection.getResponseCode());
            if (connection.getResponseCode() != 200) {
                return new JSONObject();
            }
            String line = "";
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            Log.d("login", sb.toString());
            response = new JSONObject(sb.toString());

            isr.close();
            reader.close();
        } catch (Exception e) {
            this.exception = e;
        }

        return response;
    }
}

我也尝试过成为LoginTask的成员类LoginPage,并在方法中启动动画onPreExecute(),但这并没有改变任何东西。

任何帮助深表感谢!

4

1 回答 1

5

当您使用 时AsyncTask.get(),您正在阻塞 UI 线程。当动画在 UI 线程上运行时,它看起来好像没有运行(实际上它被您的长时间运行tryLogin方法阻止了)。

相反,您应该将依赖于结果的代码从其方法LoginTask移至其onPostExecute方法:

protected void tryLogin(String mUsername, String mPassword) {
    String loginUrl = getString(R.string.login_url);
    String clientId = getString(R.string.client_id);
    String clientSecret = getString(R.string.client_secret);
    new LoginTask().execute(mUsername, mPassword, 
        loginUrl, clientId, clientSecret);
}

登录任务.java

class LoginTask extends AsyncTask<String, Void, JSONObject> {
    private Exception exception;

    @Override
    protected JSONObject doInBackground(String... params) {
        // Unchanged
    }

    public void onPostExecute(JSONObject response) {
        if (exception != null) {
            Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        } else {
            Log.d("login", response.toString());
            if (!response.has("access_token")) {
                loading.setVisibility(TextView.INVISIBLE);
                footer.startAnimation(slideUp);
                Toast.makeText(this, "Login error", Toast.LENGTH_LONG).show();
            } else {
                Intent i = new Intent(this, FullscreenWebView.class);
                i.putExtra("accessToken", response.get("access_token").toString());
                startActivity(i);
                overridePendingTransition(0, 0);
            }
        }
    }
}
于 2013-06-24T04:46:43.533 回答