Sunday, March 2, 2014

Why can not get weather like this to get the data.


 
public InputStream getInputStreamFromUrl (String urlStr)
throws MalformedURLException, IOException {
url = new URL (urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection ();
urlConn.setConnectTimeout (15000);
urlConn.setRequestMethod ("GET");
InputStream input = urlConn.getInputStream ();
return input;
}


 
public String download (String urlStr) {
StringBuffer sb = new StringBuffer ();
String line = null;
BufferedReader buffer = null;
InputStream input = null;

try {
input = this.getInputStreamFromUrl (urlStr);
buffer = new BufferedReader (new InputStreamReader (input));
while ((line = buffer.readLine ())! = null) {
sb.append (line);
}
} Catch (MalformedURLException e) {
e.printStackTrace ();
} Catch (IOException e) {
e.printStackTrace ();
} Finally {
try {
if (buffer! = null) {
buffer.close ();
buffer = null;
}
if (input! = null) {
input.close ();
input = null;
}
} Catch (IOException e) {
e.printStackTrace ();
}
}

return sb.toString ();
}


download ("http://m.weather.com.cn/data/101190101.html");

A few days ago, no problems, now is get less data<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection ();
This place urlConn.connect (); This is not normally connected to the network can add?

No comments:

Post a Comment