Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除打印时,jsoup给出空响应_Java_Android_Cookies_Nullpointerexception_Jsoup - Fatal编程技术网

Java 删除打印时,jsoup给出空响应

Java 删除打印时,jsoup给出空响应,java,android,cookies,nullpointerexception,jsoup,Java,Android,Cookies,Nullpointerexception,Jsoup,我试图使用jsoup登录到银行网站,但当从代码中删除打印cookie的行时,我得到了一个NullPointerException。我知道我应该验证响应是否为空,但问题是当我打印cookies时程序工作。 代码如下: private class GetHomeTask extends AsyncTask<Void, Void, BitmapDrawable> { protected BitmapDrawable doInBackground(Void... nothing) {

我试图使用jsoup登录到银行网站,但当从代码中删除打印cookie的行时,我得到了一个NullPointerException。我知道我应该验证响应是否为空,但问题是当我打印cookies时程序工作。 代码如下:

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);
    }
}
私有类GetHomeTask扩展了AsyncTask{
受保护的BitmapDrawable doInBackground(无效…无){
试一试{
Response home=Jsoup.connect(home\u URL+“LoginKaptcha.jpg”)
.userAgent(USER\u AGENT)
.ValidateTSCertificates(错误)
.ignoreContentType(true)
.method(method.GET)
.execute();
cookies=home.cookies();
//System.out.println(cookies);-->如果对其进行注释,则在解析登录页面时会出现NullPointerException,如下所示。
ByteArrayInputStream inputStream=新建ByteArrayInputStream(home.bodyAsBytes());
位图bMap=BitmapFactory.decodeStream(inputStream);
返回新的BitmapDrawable(getApplicationContext().getResources(),bMap);
}捕获(IOE异常){
}
返回null;
}
@凌驾
受保护的void onPostExecute(位图可绘制bMap){
setImage(bMap);
}
}
公共作废登录(查看){
String[]userDetails=新字符串[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]=captchattext.getText().toString();
新建LoginTask().execute(userDetails);
}
私有类LoginTask扩展异步任务{
受保护的响应doInBackground(字符串[]…userDetails){
响应登录=null;
试一试{
login=Jsoup.connect(HOME\u URL+“login.action”)
.数据(“持卡人.用户ID”,用户详细信息[0][0])
.数据(“持卡人.用户密码”,用户详细信息[0][1])
.data(“CaptCharResponse1”,用户详细信息[0][2])
.数据(“instName”,instName)
.data(“\u复选框\u rememberMe”,“true”)
.userAgent(USER\u AGENT)
.饼干(饼干)
.推荐人(主页地址)
.ValidateTSCertificates(错误)
.method(method.POST)
.execute();
}捕获(IOE异常){
e、 printStackTrace();
}
返回登录;
}
@凌驾
受保护的void onPostExecute(响应登录){
字符串userName=null;
试一试{
//-->NullPointerException(登录为null)仅在我对打印cookie的行进行注释时才会在此处发生。
userName=login.parse().getElementsByClass(“pageTitle”).text();
}捕获(IOE异常){
e、 printStackTrace();
}
字符串标记=login.url().toString().substring(54);
cookies=login.cookies();
Intent Intent=new Intent(getApplicationContext(),DisplaySummaryActivity.class);
intent.putExtra(额外消息,用户名+“&”+令牌);
星触觉(意向);
}
}
我试图搜索错误,但没有类似的错误。有人知道问题出在哪里吗?
谢谢。

问题在于连接超时。JToice使用3S作为默认值,所以我使用<代码> jTo.Connect(URL)。TimeOutt(10×1000)< /C> > /P>可能知道的副本是NulLoPoExtExchange,问题是为什么代码中的一个打印会影响它。如果您找到NPE当前所在的位置,您可能会发现为什么。回答这个问题是不可能的,因为你没有提供任何信息。(stacktrace,指向导致它的行的指针,您自己调试的结果,以查看哪个变量为空以及为什么为空),通过做这些事情并提供这些信息,您很有可能会自己找到解决方案。首先是LoginTask不可避免地引发异常,并捕获了
catch(IOException)
,因此它似乎与您提到的打印无关。是的,但是如果我保持打印,不使用这个解决方案,代码就会工作。不知道为什么。