先新建一个布局文件和NewsBean类
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_image"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="10dp" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="Title"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:text="Content"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
public class NewsBean {
public String newsIconUrl;
public String newsTitle;
public String newsContent;
}
在activity_main.xml中,引用前面新建的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
在MainActivity中写相应的代码。
public class MainActivity extends Activity {
private ListView listView;
public static String URL = "http://www.imooc.com/api/teacher?type=4&num=30";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv_main);
new NewsAsyncTask().execute(URL);
}
private List<NewsBean> getJsonData(String url) {
List<NewsBean> newsBeansList = new ArrayList<NewsBean>();
try {
String jsonString = readStream(new URL(url).openStream());
// 此句和url.openConnection().getInputStream()相同,可根据URL直接联网获取网络数据
// 返回值的类型为InputStream
Log.d("abc", jsonString);
} catch (IOException e) {
e.printStackTrace();
}
return newsBeansList;
}
private String readStream(InputStream is) {
String result = "";
InputStreamReader isr;
try {
String line = "";
isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
result += line;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>> {
@Override
protected List<NewsBean> doInBackground(String... params) {
// TODO Auto-generated method stub
return getJsonData(params[0]);
}
}
}
这是如何通过InputStream读取网络信息的呢?
传进一个InputStream字节流,通过InputStreamReader将字节流转化为字符流,再通过BufferedReader将字节流以Buffer形式读取出来,最终拼接到result中。
最后,要记得在清单文件中添加权限。
<uses-permission android:name="android.permission.INTERNET" />