81,122
社区成员




public static String getDCBallHistory() {
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
boolean flag = false;
int count = 0;
// System.setProperty("sun.net.client.defaultConnectTimeout", "2500");
// System.setProperty("sun.net.client.defaultReadTimeout", "3500");
while (count < 3) {
try {
URL apiUrl = new URL("http://f.apiplus.net/ssq-50.json");
HttpURLConnection httpURLConnection = (HttpURLConnection) apiUrl.openConnection();
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception(
"HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
} catch (Exception e) {
// e.printStackTrace();
flag = true;
System.out.println("direct get " + count + " times");
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (flag) {
count++;
} else {
break;
}
}
return resultBuffer.toString();
}