asyncTask 异步网络请求数据 判断适配器为空

package com.example.guoqiang15090976;


import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import android.widget.ListView;

import com.example.guoqiang15090976.Bean.DataDataBean;
import com.google.gson.Gson;

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private List list=new ArrayList<>();
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.list_view);


        getJsonFormat();


    }

       public void setAdapter(){

          if (myAdapter==null)
          {
              myAdapter = new MyAdapter(MainActivity.this, list);


              listView.setAdapter(myAdapter);
          }



      }


    private void getJsonFormat() {
           //来时那些异步
        AsyncTask asyncTask = new AsyncTask() {
            @Override
            protected String doInBackground(Void... voids) {

                String path="https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1";

                try {
                    URL url = new URL(path);

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(5000);
                    connection.setConnectTimeout(5000);

                    //请求码
                    int responseCode = connection.getResponseCode();
                    if (responseCode==200){
                        InputStream inputStream = connection.getInputStream();
                          //将字节装换文字符流
                        String json= StreamToString(inputStream,"utf-8");

                        Log.i("打印json的内容",json.toString());  //写到这里下面的不用写也能打印json



                        return  json;   //要注释点掉这个返回的json

                        //返回json  传递值
                    }


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


                return "";
            }



            @Override
            protected void onPostExecute(String json) {
                super.onPostExecute(json);
                  //
               Gson gson = new Gson();


                DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);


                list.addAll(dataDataBean.getNewslist());
                setAdapter();



                // Log.i("这是最里面的对象所打印的",json.toString());

            }
        }.execute();



    }


  //解析字符串


     private String StreamToString(InputStream inputStream, String charset) {


        try {
            InputStreamReader inputStreamReader = new            InputStreamReader(inputStream,charset);  

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String s = null;
            StringBuilder builder = new StringBuilder();

            while ((s = bufferedReader.readLine()) != null){
                builder.append(s);
            }

            bufferedReader.close();
            return builder.toString();

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

        return  null;
    }

}

你可能感兴趣的:(asyncTask 异步网络请求数据 判断适配器为空)