做Android开发不可避免的需要进行Json数据的获取与解析
首先,我们要根据url地址获取网络或服务器的Json数据,其中涉及Java的I/O流对数据的读取
下面通过代码实际演示具体实现:
public class JSONTest extends Activity {
private String URL = "http://www.imooc.com/api/teacher?type=4&num=30";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getJsonData(URL);
}
//获取Json数据并放入Bean对象中
private List<NewsBean> getJsonData(String url) {
List<NewsBean> beanList = new ArrayList<>();
try {
//new URL(url).openStream()功能相当于url.openconnection.getInputStream(),返回值类型为InputStream
String jsonString = readJson(new URL(url).openStream());
Log.i("xyz", "getJsonData: " + jsonString);
} catch (IOException e) {
e.printStackTrace();
}
return beanList;
}
//通过输