實現ListView圖文混排 —— 獲取Json數據


先新建一個佈局文件和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" />

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章