从android应用程序登录网页 [英] loging into web page from android app

查看:58
本文介绍了从android应用程序登录网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力做到这一点,我确实需要经验丰富的同伴的帮助.

I am trying to do this, and I really need help from expirienced fellows.

  1. 带有2个texbox的应用.一个用于用户名,另一个用于密码.
  2. 登录按钮.
  3. 当用户按下按钮时,登录信息将发送到网页(m.bonbon.hr),并在浏览器中打开该网页.
  4. 首次登录后,将保存登录信息,因此用户无需再次输入该信息.

从哪里开始,请提供任何指导方针,建议,我会采取任何措施.

Where to start from, please any guidelines, advices, i'll take anything.

我主要是通过这种方式创建的

I created this in my main acctivity

DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpost = new HttpPost("https://www.bonbon.hr/registracija?direct=1");

      try {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("email", "login"));
        nvps.add(new BasicNameValuePair("password", "pw"));
        nvps.add(new BasicNameValuePair("autologin", "true"));
        httpost.setEntity(new UrlEncodedFormEntity(nvps));

        }
        catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }
        try {
            HttpResponse response = httpclient.execute(httpost);

            // writing response to log
            Log.d("Http Response:", response.toString());

        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();

        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
        }

在onCreate部分下.所以现在,当我点击我的按钮(已经创建并可以使用)时,我需要使用给定的信息登录到给定的URL ...但是我不知道如何使用asyntask来使它发生问题:(

Under the onCreate part. So now , when i click on my button (already created and ready for use) i need to login to given URL with given information... But I don't know how to makle it happen using asyntask :(

推荐答案

使用两个文本框和一个按钮登录来设计应用程序的布局.

Design the layout of the appliaction with the two text boxes and one button to login.

现在,当按下该按钮时,使用 getText() 获取在两个文本框中输入的文本,然后执行 POST 请求网页URL,并使用HttpClient维护会话.

Now, when that button is pressed get the text entered in the two TextBoxes using getText() and make POST request to the webpage URL and maintain a session using HttpClient.

如果您打算保存用户登录信息,请 SharedPreferences 是最好的方法,您可以将用户名和密码存储在SharedPreference变量中,并在以后使用它们来提出以后的登录请求.

If you are planning to save user login information, SharedPreferences is the best way to do it and you can store the username and the password in the SharedPreference variables and use them later to make later requests to log in.

如果您想进一步了解如何使用HttpClient建立会话,请

And in case you want to know more about how to use HttpClient to establish a session, this question would provide you with a lot of answers.

这篇关于从android应用程序登录网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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