使用jsoup POST方法在android系统 [英] using jsoup post method in android

查看:377
本文介绍了使用jsoup POST方法在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想是这样that.i有一个按钮和文本框2当用户把自己的用户名和密码,然后点击登录按钮,然后将要执行的动作登录,然后用户欢迎,并转到另一页。我的code为这样的:

I want something like that.i have a button and 2 textbox when user put their username and password and then click on login button then login action will be performed and then welcome the user and go to another page .my code was like that:

try {
                Connection.Response res = Jsoup.connect("URL")
                        .data("log", "abcd", "pwd", "12345", "wp-submit", "প্রবেশ", "redirect_to", "url", "testcookie", "1")
                        .method(Method.POST)
                        .execute();
                Map<String, String> cookies = res.cookies();

                Document doc2 = Jsoup
                    .connect("new_url")
                    .cookies(cookies)
                    .get();

                s = doc2.text().toString();
                t.setText(s);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                t.setText("no");
            }

但登录操作不能是成功的,在这里它总是显示无。
我如何才能成功地做到了?

But login action cant be successful and here it always shows "no". How can i successfully do that???

推荐答案

我觉得这是线程问题。因为你的互联网的要求是在主UI线程。测试:

i think it is threading problem. because your internet request is on main UI-Thread. test this:

private class AsyncExecution extends AsyncTask<Void, Void, Void>{
    boolean tracker = false;
    String s = "";
    @Override
    protected Void doInBackground(Void... params) {
       try {
           Connection.Response res = Jsoup.connect("http://www.kuetlive.com/wp-login.php")
                        .data("log", "abcd", "pwd", "12345", "wp-submit", "প্রবেশ", "redirect_to", "http://www.kuetlive.com/wp-admin/", "testcookie", "1")
                        .method(Method.POST)
                        .settimeout(60000)//time set for the connection 1 min
                        .execute();
                Map<String, String> cookies = res.cookies();

                Document doc2 = Jsoup
                    .connect("http://www.kuetlive.com/wp-admin/profile.php")
                    .cookies(cookies)
                    .get();

                s = doc2.text().toString();
                tracker = true;

       } catch (Exception e) {
           // TODO Auto-generated catch block
           Log.e("tag", e.toString());
           tracker = false;

       }
     }
  --- // }  // --- i add this by mistake, delete this

    @Override
    protected void onPostExecute(Void result) {
       if(tracker){
            t.setText(s);
       }else{
            t.setText("no");
       }
    }

}

和称呼其为

new AsyncExecution().execute();

编辑:
你无法对主UI线程互联网请求。这就是为什么ü需要一个不同的线程。现在ü可以实现一个简单的线程,而不是的AsyncTask 。那么为什么我建议ü使用 AyncTask 。答案是,你无法从一个简单的线程更新UI。这就是为什么u需要的AsyncTask ,因为的AsyncTask 插上U灵活地更新UI,但在UI身边不同的线程中执行的方法-Thread。

Edited: you cannot perform an internet request on main UI-Thread. that is why u need a different thread. now u could implement a simple thread instead of AsyncTask. then why i suggest u to use AyncTask. the answer is, u cannot update your UI from a simple Thread. that is why u need AsyncTask, because AsyncTask gives u flexibility to update UI but execute your method in different Thread beside UI-Thread.

这篇关于使用jsoup POST方法在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆