2012-11-15 232 views
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> 

回答

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 ** 
+0

這是得到的所有文本,我只想得到49.156.53.152 – phucloi89

+0

看到我的編輯,我包括分裂功能 – Greezer

+0

什麼是分離[];我不明白你的想法 – phucloi89

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(); 
    } 

} 
+0

我只是想49.156.53.152,有更多的1級? – phucloi89

0

你應該嘗試使用此。

myString = String.subString("<div id=\"value1\">", YOUR_ORIGINAL_STRING.len); 
    String required = myString.endsWith("</div>"); 
+0

myString是布爾類型? – phucloi89

+0

請檢查已編輯的代碼。 – Anuj

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