如何使用JSOUP发布表单登录?

时间:2017-07-01 04:05:09

标签: java android jsoup

我想登录https://spps.getalma.com

以下是表格来源

<form id="login-form" action="/login" method="post" autocomplete="off" class="pure-form pure-form-stacked login-form">
    <label>
        Username
        <input name="username" type="text" class="pure-input-1" placeholder="Username" value="" required="" autofocus="">
    </label>

    <label>
        Password
        <input name="password" type="password" class="pure-input-1" placeholder="Password" value="" required="">
    </label>
    <button type="submit" class="pure-button pure-button-primary">Login</button>
</form>

我正在尝试这个

public String getString(String username, String password) {
    Connection.Response loginForm = null;
    Document document = null;
    try {
        loginForm = Jsoup.connect("https://spps.getalma.com/").method(Connection.Method.GET).execute();
        document = Jsoup.connect("https://spps.getalma.com/login")
                .data("cookieexists", "false").data("username", username)
                .data("password", password).cookies(loginForm.cookies()).post();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String temp = document.title();
    return temp;
}

但是当我启动应用程序并执行此方法时,我的应用程序就会关闭。 我猜它没有正确登录,我不知道我应该做些什么来使它工作..

您可以使用此用户名和密码登录
ID: paul.kim
密码: Dvjpp; [sddeptfdr,rdyrt1

它说这是由: android.os.NetworkOnMainThreadException

请帮帮我。 感谢

1 个答案:

答案 0 :(得分:0)

使用这些方式在类下面调用

    new AsyncCaller().execute();

 private class AsyncCaller extends AsyncTask<Void, Void, String>
    {
        ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //this method will be running on UI thread
            pdLoading.setMessage("Loading...");
            pdLoading.show();
        }
        @Override
        protected String doInBackground(Void... params) {

           String result= getString("your username","your password")


            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
         pdLoading.dismiss();
        Toast.make(context,result,Toas.LENGTH_SHORT).show();
        }

        }
    }