Android-异步加载

创建一个item_layout.xml布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/iv_icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:src="@mipmap/ic_launcher"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="4dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="15sp"
                android:maxLines="1"
                android:text="Title"/>
        <TextView
                android:id="@+id/tv_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="10sp"
                android:maxLines="3"
                android:text="Content"/>
        LinearLayout>
    LinearLayout>
LinearLayout>

效果图:
这里写图片描述

然后在主界面activity_main.xml中创建一个ListView


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">ListView>
RelativeLayout>

布局完成了!然后就是要完成获取数据

创建一个NewsBean.java

public class NewsBean {

    public String newsIconUrl;
    public String newsTitle;
    public String newsContent;

}

在主函数MainActivity.java

package com.example.huang.myapplication;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

//创建mListView获取ListView布局
    private ListView mListView;
//网址
    private 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);

        mListView = (ListView) findViewById(R.id.lv_main);

        new NewsAsyncTask().execute(URL);
    }


    private List getJsonData(String url) {

        List newsBeanList = new ArrayList<>();
        //获取网页返回的字符串
        String jsonString = null;
        try {
            //调用readStream方法读取网页url的信息返回到jsonString
            jsonString = readStream(new URL(url).openStream());
            Log.d("hj",jsonString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newsBeanList;
    }

    //读取网页返回信息
    private String readStream(InputStream is) {
        InputStreamReader isr;
        String result = "";
        try {
            String line = "";
            //字节流转化为字符流
            isr = new InputStreamReader(is, "utf-8");
            //字符流利用BufferedReader读取出来
            BufferedReader br = new BufferedReader(isr);

            //如果不为内容空,读取到result中
            while ((line=br.readLine()) != null) {
                result += line;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

    }

    //AsyncTask<传入一个String网址,不需要记录中间过程Void,返回NewsBean的集合>
    class NewsAsyncTask extends AsyncTask> {
        @Override
        protected List doInBackground(String... strings) {
            //strings就一个,传进来的网址
            return getJsonData(strings[0]);
        }
    }
}

你可能感兴趣的:(安卓学习)