インターネットを介してlivescoresの結果を取得する必要があるプロジェクトがあります。調査の結果、 http: //iphone.livescore.com/iphone.dll?gmt=0.0からlivescoreを取得できることがわかりました。その後、上記のURLから応答する簡単なアプリケーションを作成します。しかし、問題は、結果が不安定になるようにコードを書くことです。ブラウザの表示として正しくないURLの本文が返されることがありますが、私のアプリケーションでは、人間が読み取れない文字を含む不明な文字列が表示されます。
ここに私のコード、誰かが私にアドバイスを与えることができますか?
private String getURLContent(String url) {
StringBuffer stringBuffer = new StringBuffer();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
InputStream inputStream = response.getEntity().getContent();
InputStreamReader reader = new InputStreamReader(inputStream,
"UTF-8");
int ch;
while ((ch = reader.read()) > -1) {
stringBuffer.append((char) ch);
}
reader.close();
}
} catch (ClientProtocolException e) {
log.fatal(e);
} catch (IOException e) {
log.fatal(e);
}
return stringBuffer.toString();
}