删除打印时,jsoup给出null响应

时间:2015-10-05 18:17:44

标签: java android cookies nullpointerexception jsoup

我正在尝试使用jsoup登录银行网站但是当从代码中删除打印cookie的行时,我收到NullPointerException。我知道我应该验证响应是否为空但问题是程序在我打印cookie时有效。 这是代码:

private class GetHomeTask extends AsyncTask<Void, Void, BitmapDrawable> {
    protected BitmapDrawable doInBackground(Void... nothing) {
        try {

                Response home = Jsoup.connect(HOME_URL + "LoginKaptcha.jpg")
                        .userAgent(USER_AGENT)
                        .validateTLSCertificates(false)
                        .ignoreContentType(true)
                        .method(Method.GET)
                        .execute();
                cookies = home.cookies();
                //System.out.println(cookies); --> if commented, I get a NullPointerException when parsing the login page as pointed below.
                ByteArrayInputStream inputStream = new ByteArrayInputStream(home.bodyAsBytes());
                Bitmap bMap = BitmapFactory.decodeStream(inputStream);
                return new BitmapDrawable(getApplicationContext().getResources(), bMap);
            } catch (IOException e) {

            }
            return null;
        }

    @Override
    protected void onPostExecute(BitmapDrawable bMap) {
        setImage(bMap);
    }
}

public void loginAction(View view) {

    String[] userDetails = new String[3];
    EditText userIdText = (EditText) findViewById(R.id.userIdField);
    userDetails[0] = userIdText.getText().toString();
    EditText userPasswordText = (EditText) findViewById(R.id.userPasswordField);
    userDetails[1] = userPasswordText.getText().toString();
    EditText captchaText = (EditText) findViewById(R.id.captchaField);
    userDetails[2] = captchaText.getText().toString();

    new LoginTask().execute(userDetails);

}

private class LoginTask extends AsyncTask<String[], Void, Response> {
    protected Response doInBackground(String[]... userDetails) {
        Response login = null;
        try {
            login = Jsoup.connect(HOME_URL + "login.action")
                    .data("cardHolder.userId", userDetails[0][0])
                    .data("cardHolder.userPassword", userDetails[0][1])
                    .data("captchaResponse1", userDetails[0][2])
                    .data("instName", instName)
                    .data("__checkbox_rememberMe", "true")
                    .userAgent(USER_AGENT)
                    .cookies(cookies)
                    .referrer(HOME_URL)
                    .validateTLSCertificates(false)
                    .method(Method.POST)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return login;
    }

    @Override
    protected void onPostExecute(Response login) {
        String userName = null;
        try {
            // --> NullPointerException (login is null) occurs here only when I comment the line that prints the cookies.
            userName = login.parse().getElementsByClass("pageTitle").text();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String token = login.url().toString().substring(54);
        cookies = login.cookies();

        Intent intent = new Intent(getApplicationContext(), DisplaySummaryActivity.class);
        intent.putExtra(EXTRA_MESSAGE, userName + "&" + token);
        startActivity(intent);
    }
}

我试图搜索错误,但没有任何类似的内容。有谁知道这是什么问题? 谢谢。

1 个答案:

答案 0 :(得分:0)

问题在于连接超时。 Jsoup默认使用3s,所以我使用Jsoup.connect(url).timeout(10*1000)将其更改为10s。