1、创建一个URL对象,URL对象中包含url链接地址;
2、调用URL的openConnection()方法创建HttpURLConnection对象;
3、调用HttpURLConnection对象的getInputStream()方法得到一个InputStream对象;
4、网络请求得到的结果已经在得到的这个InputStream流中,解析这个inputStream即可;
5、InputStream转成InputStreamReader对象,再将该Reader对象转成BufferReader对象;
6、BufferReader对象的readLine()方法可以按行读取流中的数据,循环搞定!
7、注意:网络请求只能在子线程中调用!!!
8、关键代码示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| package com.yusian.httpurlconnction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String BASE_URL = "http://www.yusian.com/blog";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 网络请求只能在子线程中实现
new Thread(new Runnable() {
@Override
public void run() {
getDataFromInternet();
}
}).start();
}
public void getDataFromInternet(){
try {
URL url = new URL(BASE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
Log.i("SALog", "Code = "+code);
// Stream-->StreamReader-->Reader
InputStream input = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(input);
BufferedReader buffer = new BufferedReader(reader);
while (true){
String string = buffer.readLine();
if (string == null) break;
Log.i("SALog", string);
}
// 关闭相关类
reader.close();
input.close();;
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
package com.yusian.httpurlconnction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String BASE_URL = "http://www.yusian.com/blog";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 网络请求只能在子线程中实现
new Thread(new Runnable() {
@Override
public void run() {
getDataFromInternet();
}
}).start();
}
public void getDataFromInternet(){
try {
URL url = new URL(BASE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int code = connection.getResponseCode();
Log.i("SALog", "Code = "+code);
// Stream-->StreamReader-->Reader
InputStream input = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(input);
BufferedReader buffer = new BufferedReader(reader);
while (true){
String string = buffer.readLine();
if (string == null) break;
Log.i("SALog", string);
}
// 关闭相关类
reader.close();
input.close();;
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
| 运行结果:
I/SALog: Code = 302
I/SALog: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
I/SALog: <html><head>
I/SALog: <title>302 Found</title>
I/SALog: </head><body>
I/SALog: <h1>Found</h1>
I/SALog: <p>The document has moved <a href="http://www.yusian.com/blog/">here</a>.</p>
I/SALog: </body></html> |
运行结果:
I/SALog: Code = 302
I/SALog: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
I/SALog: <html><head>
I/SALog: <title>302 Found</title>
I/SALog: </head><body>
I/SALog: <h1>Found</h1>
I/SALog: <p>The document has moved <a href="http://www.yusian.com/blog/">here</a>.</p>
I/SALog: </body></html>
Pingback: Android基础控件之网络解析JSONObject与JSONArray的基本使用 | 小龙虾博客 (Crayfish)