csh_34 2013-06-18 07:00 采纳率: 0%
浏览 2577
已采纳

从 web 页中获取 text

我想从web中获取text,我使用indexOf 和subString编写代码,但是不能运行。

int index = response_str.indexOf("Remote IP Address:");
index += "Remote IP Address:".length();
index += "</div><br /><div id=\"value1\">".length();
int end = response_str.indexOf("</div><br /><br />", index);
String strIP = response_str.substring(index, end);      
Log.d("","Hello" + strIP  );

这是示例文本,我想获得49.156.53.152

<body>
<div id="title">Remote IP Address:</div><br /><div id="value1">**49.156.53.152**</div><br /><br />
<div id="title">UserAgent:</div><br /><div id="value2">Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11</div><br /><br />
<!-- Everyone of CCorp employees, Good luck ! --><br />
</body>
  • 写回答

2条回答

  • Curie-87 2013-06-19 02:41
    关注

    你应该把 html page 转换成 sting,并且使用 regex / string 操作来获取所需的数据。

    try {
                    if(!url_text.getText().toString().trim().equalsIgnoreCase("")){
                        textView.setText("");
                        HttpClient client = new DefaultHttpClient();
                        HttpGet request = new HttpGet(url_text.getText().toString());
                        // Get the response
                        ResponseHandler<String> responseHandler = new BasicResponseHandler();
                        String response_str = client.execute(request, responseHandler);
                        textView.setText(response_str);
                    }else{
                        Toast.makeText(getApplicationContext(), "URL String empty.", Toast.LENGTH_LONG).show();
                    }
                }
                catch (Exception e) {
                    System.out.println("Some error occured.");
                    textView.setText(e.getMessage());
                }
    

    可能最简单的方法是使用 sting.split 函数

    String[] separated = response_str.split("**");
    separated[0]; // part before the **
    separated[1]; // your needed ip string
    separated[2]; // part after the second **
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考