获取网络数据进行Gson解析用ListView展示

package com.example.weekoneex.fragment;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Text;

import com.example.weekoneex.R;
import com.example.weekoneex.bean.NewsData;
import com.google.gson.Gson;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentOne extends Fragment{
    ListView lv;
    NewsData data;
    MyAdapter adapter;
    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.frag_one, null);
        
        lv = (ListView) view.findViewById(R.id.lv);
        adapter = new MyAdapter();
        initData();
        
        return view;
    }
    private void initData() {
//        list = new ArrayList();
        new AsyncTask() {

            @Override
            protected String doInBackground(String... params) {
                return getData();
            }

            @Override
            protected void onPostExecute(String result) {
                Gson gson = new Gson();
                data = gson.fromJson(result, NewsData.class);
                lv.setAdapter(adapter);
                super.onPostExecute(result);
            }
            
        }.execute();
    }
    
    class MyAdapter extends BaseAdapter{

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

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                convertView = View.inflate(getActivity(), R.layout.list_item, null);
                holder = new ViewHolder();
                holder.title = (TextView) convertView.findViewById(R.id.item_title);
                holder.content = (TextView) convertView.findViewById(R.id.item_content);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            holder.title.setText(data.getNewsList().get(position).getTitle());
            holder.content.setText(data.getNewsList().get(position).getContent());
            return convertView;
        }
        
    }
    
    static class ViewHolder{
        TextView title;
        TextView content;
    }
    
    protected String getData() {
        String result = "";
        try {
            URL url = new URL("http://169.254.122.215:8080/news/news.json");
            HttpURLConnection connection = (HttpURLConnesction) url.openConnection();
            connection.setRequestMethod("GET");
            int code = connection.getResponseCode();
            if(code == 200){
                InputStream is = connection.getInputStream();
                //InputStreamReader isr = new InputStreamReader(is, "GBK");
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                
                int len = -1;
                byte[] buff = new byte[1024];
                while((len = is.read(buff)) != -1){
                    bos.write(buff,0,len);
                }
                result = new String(bos.toByteArray(), "GBK");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
}



你可能感兴趣的:(获取网络数据进行Gson解析用ListView展示)