如何自动检查网站源代码中的字符串何时不存在?

时间:2014-06-25 05:01:07

标签: android html web

我想每天查看是否

<h3>Tags</h3><ul class="ref-list"><li><a href="/platform/build/+/android-4.4.4_r1">android-4.4.4_r1</a>

出现在https://android.googlesource.com/platform/build/中,如果不是,则会收到通知。

这样我就会知道什么时候发布了新版本的aosp。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

警报+1。您可能需要查看有关页面下载的 HTTPClient 文档:

  

http://developer.android.com/reference/org/apache/http/client/HttpClient.html

示例代码段:

HttpClient client = new DefaultHttpClient();
String uri = "https://android.googlesource.com/platform/build/"
HttpGet request = new HttpGet( uri );

try {
  HttpResponse response = client.execute(request);
  StatusLine status = response.getStatusLine();
  if (status.getStatusCode() != 200) {
      throw new IOException("Invalid response from server: " + status.toString());
  }

  HttpEntity entity = response.getEntity();
  InputStream inputStream = entity.getContent();
  ByteArrayOutputStream content = new ByteArrayOutputStream();
} catch( Exception ex ) {
  throw new Exception( "Something went wrong while accessing '" + uri + "':"  + ex.getLocalizedMessage() );
}

  int readBytes = 0;
  byte[] sBuffer = new byte[1024];
  while ((readBytes = inputStream.read(sBuffer)) != -1) {
      content.write(sBuffer, 0, readBytes);
  }

  String dataAsString = new String(content.toByteArray()); 

  // TODO: Parse 'dataAsString'...

剩下的就是解析结果而你已经完成了。

答案 1 :(得分:0)

获取页面源代码。把它放在服务中。 :

HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://yoururl.com"); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
    sb.append(line + "\n");

String resString = sb.toString(); // Result is here

is.close(); // Close

然后:

String aospString = "<h3>Tags</h3><ul class=\"ref-list\"><li><a href=\"/platform/build/+/android-4.4.4_r1\">android-4.4.4_r1</a>";
if(!resString.contains(aospString)) {
    Log.d(LOGTAG, "ASOP Unavailable");
}

然后,您可以使用AlarmManager以固定频率安排执行。 Here