0

我想从网络上获取文本,我编写了代码,使用 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>
4

4 回答 4

0

您必须使用带有 Java 的 Javascript 接口对象来从 Html 网页中获取所有数据。

final WebView webview = (WebView) findViewById(R.id.browser);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
                    "('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
        }
    });

    webview.loadUrl("http://android-in-action.com/index.php?post/" +
            "Common-errors-and-bugs-and-how-to-solve-avoid-them");
}

class MyJavaScriptInterface {

    private Context ctx;

    MyJavaScriptInterface(Context ctx) {
        this.ctx = ctx;
    }

    public void showHTML(String html) {
        new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
                .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
    }

}
于 2012-11-15T07:46:52.907 回答
0

您可以将 html 页面转换为 sting。并使用正则表达式/字符串操作来获取需要的数据

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 **
于 2012-11-15T07:51:20.263 回答
0

你应该尝试使用它。

    myString = String.subString("<div id=\"value1\">", YOUR_ORIGINAL_STRING.len);
    String required = myString.endsWith("</div>");
于 2012-11-15T08:44:34.057 回答
0

你可以使用JSoup

Document doc = Jsoup.connect(URL_TO_HTML_PAGE).get();
Elements newsHeadlines = doc.getElementsByAttribute("value1");
String ip = newsHeadlines[0].text().split("**")[1];

最后一行是基于 Greezer 的帖子。我个人会将其替换为与所有可能的 IP 地址匹配的简单正则表达式模式。

就像是:

\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

于 2012-11-16T08:40:46.807 回答