1. 程式人生 > >多條目 2018/11/4

多條目 2018/11/4

////GsonUitn


    package com.example.day04_duotiaomu;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class GsonUtil {
    //定義方法
    public static <E> E getRequest(String UrlStr,Class cla){
        String request = getRequest(UrlStr);
        E e = (E) new Gson().fromJson(request,cla);
        return e;
    }


    public static String getRequest(String UrlStr){
        String result="";
        try {
            URL url = new URL(UrlStr);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setReadTimeout(5000);
            urlConnection.setConnectTimeout(5000);
            int responseCode = urlConnection.getResponseCode();
            if(responseCode==200){
                InputStream inputStream = urlConnection.getInputStream();
                result = stream2String(inputStream);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    private static String stream2String(InputStream is) throws IOException {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        for (String tmp = br.readLine();tmp!=null;tmp=br.readLine()){
            sb.append(tmp);
        }
        return sb.toString();
    }
}


//Main

package com.example.day04_duotiaomu;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {

    private MygetdataAdapter adapter;

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

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

        adapter = new MygetdataAdapter(MainActivity.this);
        listView.setAdapter(adapter);


    }

    private String UrlStr1 = "http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=tt";
    private String UrlStr2 ="http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=gn";

    @SuppressLint("StaticFieldLeak")
    AsyncTask<String,Void,List<MygetdataBean.Result.Data>> asyncTask = new AsyncTask<String, Void, List<MygetdataBean.Result.Data>>() {
        @Override
        protected List<MygetdataBean.Result.Data> doInBackground(String... strings) {
            List<MygetdataBean.Result.Data> result = new ArrayList<>();

            for (String url:strings)
            {
                MygetdataBean bean = GsonUtil.getRequest(url, MygetdataBean.class);
                result.addAll(bean.getResult().getData());
            }
            return result;
        }

        @Override
        protected void onPostExecute(List<MygetdataBean.Result.Data> data) {
            adapter.setDataList(data);
        }

    }.execute(UrlStr1,UrlStr2);
}

//Bean

package com.example.day04_duotiaomu;

import java.util.List;

public class MygetdataBean {
    public Result result;

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

    public class Result{
        public List<Data> getData() {
            return data;
        }

        public void setData(List<Data> data) {
            this.data = data;
        }

        public List<Data> data;

        public class Data{
            private String title;
            private String date;
            private String category;
            private final String tt = "頭條";
            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }

            public String getDate() {
                return date;
            }

            public void setDate(String date) {
                this.date = date;
            }

            public String getCategory() {
                return category;
            }

            public void setCategory(String category) {
                this.category = category;
            }

            public boolean isTT(){
                return category.equals(tt);
            }
        }

    }
}

/***Adapter

package com.example.day04_duotiaomu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

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

public class MygetdataAdapter extends BaseAdapter {

    private static final int TT_ITEM = 0;
    private static final int GN_ITEM = 1;
    private List<MygetdataBean.Result.Data> dataList;
    private Context context;
    private int ITEM_CONUT = 2;

    public MygetdataAdapter(Context context) {
        this.context = context;
        dataList = new ArrayList<>();
    }
    public void setDataList(List<MygetdataBean.Result.Data> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    //重寫兩個方法
    @Override
    public int getItemViewType(int position) {
        return getItem(position).isTT()?TT_ITEM : GN_ITEM;
    }
    @Override
    public int getViewTypeCount() {
        return ITEM_CONUT;
    }

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

    //返回值是List要用的class型別
    @Override
    public MygetdataBean.Result.Data getItem(int position) {
        return dataList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(
                    getItemViewType(position)==GN_ITEM ? R.layout.item_gn:R.layout.item_tt
                    ,parent,false
            );
            holder = new ViewHolder();

            holder.t1=convertView.findViewById(R.id.title);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.t1.setText(getItem(position).getCategory());

        return convertView;
    }

    class ViewHolder{

        TextView t1;

    }

}