new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL("输入url地址");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String line = null;
while((line = br.readLine())!=null){
sb.append(line);
}
br.close();
String result = sb.toString();
return result;
} catch (Exception e) {
e.printStackTrace();
}
}
protected void onPostExecute(String result) {
//拿到result的数据,直接在此方法内进行UI操作
};
}.execute("url");
通过HttpURLConnection获得服务器的数据简单模型
最新推荐文章于 2019-05-14 11:02:11 发布