异步请求网络json

package com.example.lxss;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
private  Button button;
 private  Button button1;
 private Bean bean;
    private  ListView lv;
    private Mydapter ma;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button1 = (Button) findViewById(R.id.button2);

        lv = (ListView) findViewById(R.id.lv);

        ma=new Mydapter();


button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


        new AsyncTask<String,String,String>()
        {
            @Override
            protected String doInBackground(String... strings) {
                return getData();
            }

            @Override
            protected void onPostExecute(String str) {
                Gson gson = new Gson();
               bean = gson.fromJson(str, Bean.class);
                lv.setAdapter(ma);
                super.onPostExecute(str);

            };
        }.execute();
    }
});
    }
    class Mydapter extends BaseAdapter{


        @Override
        public int getCount() {
            return bean.getNewslist().size();
        }

        @Override
        public Object getItem(int i) {
            return bean.getNewslist().get(i );
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            ViewHoder vi;
            if (view==null){
                vi = new ViewHoder();
                view = View.inflate(MainActivity.this, R.layout.list, null);

                vi.textView3=(TextView) view.findViewById(R.id.textView3);
                vi.textView4=(TextView) view.findViewById(R.id.textView4);
                vi.textView5=(TextView) view.findViewById(R.id.textView5);
                vi.textView6=(TextView) view.findViewById(R.id.textView6);
                   vi.tvs=(TextView) view.findViewById(R.id.tvs);
         view.setTag(vi);
            }else {

              vi = (ViewHoder) view.getTag();

            }
            vi.textView3.setText(bean.getNewslist().get(i).getCtime());
            vi.textView4.setText(bean.getNewslist().get(i).getDescription());
            vi.textView5.setText(bean.getNewslist().get(i).getPicUrl());
            vi.textView6.setText(bean.getNewslist().get(i).getTitle());
            vi.tvs.setText(bean.getNewslist().get(i).getUrl());


            return view;

        }
        class ViewHoder{

            public TextView tvs;
            public TextView textView3;

            public TextView textView4;
            public TextView textView5;
            public TextView textView6;
        }
    }
    
private String getData(){

String str="";
    try {
        URL url = new URL("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        int code = conn.getResponseCode();
        if (code==200)
        {
            InputStream stream = conn.getInputStream();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len=-1;
            byte[] b = new byte[1024];
            while ((len=stream.read(b))!=-1){

                bos.write(b,0,len);
            }
            str = new String (bos.toByteArray(),"GBK");


        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return str;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值