Android:如何登录网站

时间:2014-01-18 08:58:02

标签: android forms login android-asynctask

我正在学习如何登录网站,但我仍然无法成功。

它似乎没有成功登录,因为返回html内容仍然是登录页面而不是成功的登录页面。

我选择了网站来练习,以下是我的核心代码。非常感谢。

public class TestAyncTask extends AsyncTask<Void, Void, Void> {

    private final String url = "http://dinbendon.net/do";
    private DefaultHttpClient client;
    private String CAPTCHA; 
    private String strResult; 

    public TestAyncTask() {

        client = new DefaultHttpClient();

    }

    @Override
    protected Void doInBackground(Void... params) {

        initial(url);                   

        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("username", "guest"));      
        list.add(new BasicNameValuePair("password", "guest"));
        list.add(new BasicNameValuePair("result", CAPTCHA));

        try {

            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
            HttpResponse httpResponse = client.execute(httpPost);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {

                strResult = EntityUtils.toString(httpResponse.getEntity());

                Log.d("Test", strResult);


            }

            List<Cookie> cookies = client.getCookieStore().getCookies();
            for (int i = 0; i < cookies.size(); i++) {
                Log.e("Test", cookies.get(i).getName() + ": " + cookies.get(i).getValue());
            }

        } catch (Exception e) {

            Log.e("Test", e.toString());

        }

        return null;

    }

    private void initial(String url) {

        int sum = 0;
        try {

            HttpGet get = new HttpGet(url);
            HttpResponse response = client.execute(get);
            HttpEntity resEntity = response.getEntity();
            InputStream is = resEntity.getContent();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader in = new BufferedReader(isr);

            String line = "";
            while ((line = in.readLine()) != null) {

                if (line.contains("<td style=\"width: 6em;\" class=\"alignRight\">")) {

                    int start = line.indexOf("\">") + "\">".length();
                    int end = line.indexOf("=<", start);
                    String result = line.substring(start, end);

                    if (result.contains("加")) {

                        sum = Integer.valueOf(result.split("加")[0]) + Integer.valueOf(result.split("加")[1].split("等於")[0]);

                    } else if (result.contains("+")) {

                        sum = Integer.valueOf(result.split("\\+")[0]) + Integer.valueOf(result.split("\\+")[1]);

                    } else if (result.contains("+")) {

                        sum = Integer.valueOf(result.split("+")[0]) + Integer.valueOf(result.split("+")[1]);

                    } else {

                        Log.e("Test", "Not matched");

                    }

                    Log.e("Test", "sum: " + sum);

                    break;

                }

            }

        } catch (Exception e) {

            Log.e("Test", e.toString());

        }

        CAPTCHA = String.valueOf(sum);

    }

    @Override
    protected void onPostExecute(Void result) {

        textview.setText(strResult);
        client.getConnectionManager().shutdown();

    }

}

1 个答案:

答案 0 :(得分:0)

textview.setText(strResult);
doInbackground中的

是不可能的。您无法从后台线程更新ui。