读取网络数据,是输入流,用InputStream
实例:
public static void getNewsJSON(final String url,final Handler handler){
new Thread(new Runnable() {
@Override
public void run() {
HttpsURLConnection connection;
InputStream isInputStream;
try {
**connection = (HttpsURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
isInputStream = connection.getInputStream();//获取网络连接的输入流,用于读取
BufferedReader reader = new BufferedReader(new InputStreamReader(isInputStream));**
String lineString = "";
StringBuilder resultBuilder = new StringBuilder();//不能为null
while((lineString = reader.readLine()) != null){
resultBuilder.append(lineString);
}
Message message = new Message();
message.obj = resultBuilder;
handler.sendMessage(message);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}