【问题标题】:Sending html commands over httpclient android通过httpclient android发送html命令
【发布时间】:2023-03-31 22:55:01
【问题描述】:

我正在尝试通过解析网页以获取相关数据,然后允许用户将数据发送回服务器,从而将 Web 界面转变为 Android 界面。使用简单的 HttpClient 代码,我设法获取了网页,然后解析了必要的信息。我不熟悉发回数据,所以,我希望有人能阐明一下。这是登录页面中的相关 HTML 代码。

<table cellspacing=0 cellpadding=0><tr><td valign=top align=center>


<table cellspacing=0 cellpadding=0 border=0  width=220 align=center class=table_back><tr><td>
<table cellspacing=1 cellpadding=1 border=0 width=100%>
<tr class=table_row1><form action="i.cfm?&1028&p=login&se=4" method=post name=stepform><Td align=right nowrap>&nbsp;Empire Name&nbsp;</td><td>&nbsp;<input type=text name=nic size=16 ></td></tr>
<tr class=table_row2><Td align=right>Password&nbsp;</td><td>&nbsp;<input type=password name=password size=16 ></td></tr>

<tr class=table_row1><Td align=right valign=top>Server</td><td>&nbsp;<select name=server>


<option value="0" >Normal</option>

<option value="1" >Fast</option>

<option value="2" >Slow</option>

<option value="3" >Ultra</option>

<option value="4" selected>RT</option>


</select><font class=smallfont> <a href=javascript:ch('i.cfm?popup=help&type=server');>What is this <img src=i/help.gif></a>
</td></tr>
<tr class=table_row2><Td align=right>&nbsp;IP&nbsp;</td><td>&nbsp;69.47.105.149 <font class=smallfont>(United States of America)</font></td></tr>
<tr class=table_row1><td>&nbsp;</td><td>&nbsp;<input type=submit value="  Login  " ></td></tr>

</td></tr></table></table>

如您所见,需要 3 个输入,即“帝国名称”、“密码”和“服务器”,由 5 个选项组成。假设我已经从我的 Android GUI 中收集了相关信息,我将如何通过 httpClient 将此数据发送回服务器。非常感谢任何帮助。

【问题讨论】:

  • 您正在解析 HTML 来为您的应用程序生成 UI?这似乎是一个可怕的想法。你到底想完成什么?
  • 看起来像是抓取网页以获取表单输入并将它们作为 Android GUI 控件呈现给用户。其实很有趣……
  • 正如 Mchl 所说,我正在抓取网页以获取相关信息。在这个特定页面上,我抓取了网站显示的 IP 地址,  IP   69.47.105.149 (美国) 然后我在 GUI 中以更友好的 UI 格式呈现它。但唯一的问题是以原始网站的方式将我在 GUI 中从用户那里收集的信息发送回服务器。
  • 当网站发生变化,你的应用完全崩溃了,你会怎么做?
  • 我会在那个时候/如果那个时候处理这​​个问题。与此同时,建设性的 cmets 非常受欢迎;)

标签: html android httpclient


【解决方案1】:

如果您要做的不仅仅是抓取一页和/或想要一个开发工具来帮助您,请查看Web Harvest。它可能具有 Android 提供之外的依赖项,但如果您必须使其适应目标平台,它是在 BSD 许可下的。来源是here。

【讨论】:

    【解决方案2】:

    代码可能如下所示:

    private void postDataToServer() throws UnsupportedEncodingException, IOException {
    
        /* 
         * Taken from "action" field in the form. This can be absolute address
         * so take this into account.
         */
        String action = "i.cfm?&1028&p=login&se=4"; 
        /* This the server you want to send info. */
        String yourServer = "http://your.server.com/";
    
        /* This form uses "post" method. */
        HttpPost post = new HttpPost(yourServer + action);
        /* Form parameters. */
        List<NameValuePair> params = new ArrayList<NameValuePair>();
    
        /* This is what the user has entered in the corresponding fields. */
        String nic = getNic();
        String password = getPassword();
        String server = getServer();
    
        params.add(new BasicNameValuePair("nic", nic));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("server", server));
    
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    
        HttpClient client = new DefaultHttpClient();
    
        post.setEntity(entity);
        client.execute(post);
    }
    

    也许这个page 也有帮助。

    【讨论】:

    • 这看起来像我要找的。我会进一步研究,谢谢。
    【解决方案3】:

    我知道一个应用程序只包含一个 webview 来显示移动网站。听起来这就是你应该做的,如果你想最小化原生 android 编程。

    【讨论】:

    • 不是我真正想要的,如果您阅读我在原始问题中的评论,我想在我的 Android 应用程序中摆脱网站的原始布局和设计和重新设计,以便我可以以更友好的 UI 方式呈现。
    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 2014-07-12
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    相关资源
    最近更新 更多