联网获取内容,utf8格式显示

1.调用:
sendGetRequest(uriAPI, "utf-8");
2.实现
public static String sendGetRequest(String uriAPI, String encoding) {
//
// HttpGet httpRequest = new HttpGet(uriAPI);
// try {
// HttpResponse httpResponse = new DefaultHttpClient()
// .execute(httpRequest);
// if (httpResponse.getStatusLine().getStatusCode() == 200) {
// String strResult = EntityUtils.toString(
// httpResponse.getEntity(), "UTF-8");
// result = strResult;
// } else {
// httpResponse.getStatusLine().toString();
// }
// } catch (ClientProtocolException e) {
// e.getMessage().toString();
// e.printStackTrace();
// } catch (IOException e) {
// e.getMessage().toString();
// e.printStackTrace();
// } catch (Exception e) {
// e.getMessage().toString();
// e.printStackTrace();
// }
// return result;
URL url;
try {
url = new URL(uriAPI);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(6 * 1000);
if (conn.getResponseCode() == 200) {
InputStream inStream = conn.getInputStream();
byte[] data = readStream(inStream);
return new String(data, encoding);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;

}

你可能感兴趣的:(utf8)