异步加载

public class Main2Activity extends AppCompatActivity {


    private ListView lv;




    //path路径
String path="http://japi.juhe.cn/joke/content/list.from?key=94fbc7ec2262160140d71e1418322f34%20&page=1&pagesize=10&sort=asc&time=1418745237";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main2);
        //找控件
        lv = (ListView) findViewById(R.id.lv);


        getData();
    }




//异步加载数据
    public void getData(){
        new AsyncTask<String, Void, MyBean>() {


            @Override
            protected MyBean doInBackground(String... params) {


                try {
                    //实例化url
                    URL url = new URL(params[0]);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    //获取请求码
                    int responseCode = httpURLConnection.getResponseCode();
                    //判断请求码
                    if (responseCode==200){
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String s = StreamUtils.InputToJson(inputStream);
                        Log.i("++++++++++++++++++","++++"+s);
                        Gson gson=new Gson();
                        MyBean myBean = gson.fromJson(s,MyBean.class);
                        return myBean;


                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


                return null;
            }


            @Override
            protected void onPostExecute(MyBean myBean) {
                super.onPostExecute(myBean);
                MyBaseAdapter adapter = new MyBaseAdapter(Main2Activity.this,myBean);
                lv.setAdapter(adapter);




            }
        }.execute(path);


    }


}

--------------------------------------------------------

适配器


public class MyBaseAdapter extends BaseAdapter {
    Context context;
   MyBean myBean;
  //构造方法
    public MyBaseAdapter(Context context,  MyBean myBean) {
        this.context = context;
        this.myBean = myBean;
    }


    @Override
    public int getCount() {
        return myBean.getResult().getData().size();
    }


    @Override
    public Object getItem(int position) {
        return null;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView=View.inflate(context, R.layout.item, null);
        //获取布局item
        TextView tv1 = (TextView) convertView.findViewById(R.id.tv1);
        TextView tv = (TextView) convertView.findViewById(R.id.tv);
        tv1.setText(myBean.getResult().getData().get(position).getUpdatetime());
        tv.setText(myBean.getResult().getData().get(position).getContent());


        return convertView;


    }
}


------------------------------------------

public class StreamUtils {
    public static String InputToJson(InputStream inputStream){
        try {
            int len=0;
            byte[] b=new byte[1024];
            ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
            while ((len=inputStream.read(b))!=-1){
                outputStream.write(b,0,len);
            }
            return outputStream.toString("utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值