Android - Check website content periodically for required result
I need to check the internet connection's download and upload speed. So,
my co-worker designed a php script which does that. All I need to do is
open the website and wait for the results.
The following code gives me this:
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://mysite.com/speedtest"); //
Set the action you want to do
HttpResponse response = null; // Execute it
try {
response = httpclient.execute(httpget);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
InputStream is; // Create an InputStream with the response
try {
is = entity.getContent();
BufferedReader reader = new BufferedReader(new
InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) // Read line by line
sb.append(line + "\n");
String resString = sb.toString(); // Result is here
Log.d(TAG, resString);
is.close(); // Close the stream
} catch (IOException e) {
e.printStackTrace();
}
Result:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /speedtest
on this server.</p>
<p>Additionally, a 500 Internal Server Error
error was encountered while trying to use an ErrorDocument to handle
the request.</p>
</body></html>
A '403 Forbidden' error, I'm assuming 'get' requests can't be made until
the final JSON result is posted. How do I open the webpage without a get
request and periodically check if the JSON result has been posted? Once
that's done, do I retrieve the JSON result using the method above?
My knowledge on servers and server sided scripting is pretty scarce, any
extra information is welcome.
No comments:
Post a Comment