http网络请求

package bwei.com.day0511_demo123.fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;
import java.util.List;

import bwei.com.day0511_demo123.R;
import bwei.com.day0511_demo123.adapter.MyBase;
import bwei.com.day0511_demo123.bean.WineBean;
import bwei.com.day0511_demo123.https.ConHttp;
import bwei.com.day0511_demo123.utils.ConUtils;

import static android.content.ContentValues.TAG;

public class Fragment01 extends Fragment{


    private ListView listview;

    private final static int SUCCESS=0;

    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SUCCESS:

                    String json= (String) msg.obj;
                    Log.d(TAG, "run: "+json);
                    Gson gson = new Gson();

                    Type type = new TypeToken<LinkedList<WineBean>>() {
                    }.getType();

                    LinkedList<WineBean> list=gson.fromJson(json,type);
//创建适配器
                    List<WineBean.ItemBean> item = list.get(0).getItem();

                    MyBase myBase = new MyBase(getActivity(),item);

                    listview.setAdapter(myBase);
                    break;
            }

        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment01, container, false);
        //获取id
        listview = view.findViewById(R.id.fragment1_listview);


        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//开启线程
        new Thread(){
            @Override
            public void run() {
                try {
                    URL u = new URL(ConHttp.ONE_URL);
                   HttpURLConnection con = (HttpURLConnection) u.openConnection();
                    con.setConnectTimeout(5000);

                    if (con.getResponseCode()==200){

                        InputStream inputStream = con.getInputStream();
                        //加载工具类
                        String json= ConUtils.inpuStream(inputStream);
                        Message me = new Message();
                        me.what=SUCCESS;
                        me.obj=json;
                        //发送handler请求
                       handler.sendMessage(me);


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


            }
        }.start();



    }
}

猜你喜欢

转载自blog.csdn.net/chenyibai/article/details/80289212
今日推荐