package com.example.user.day5_zhoukaomoni_3.fragment; import android.os.Bundle; import android.os.Handler; import android.os.Message; 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.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.example.user.day5_zhoukaomoni_3.R; import com.example.user.day5_zhoukaomoni_3.adapter.MyAdapter; import com.example.user.day5_zhoukaomoni_3.bean.NewsBean; import com.example.user.day5_zhoukaomoni_3.http.HttpConfig; import com.example.user.day5_zhoukaomoni_3.utils.ConmentUtil; 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.URL; import java.net.URLConnection; import java.util.LinkedList; import java.util.List; public class Fragment01 extends Fragment { private static final String TAG = "Fragment01"; private final static int SUCCESS = 0; private final static int ERROR = 1; private MyHandler myHandler =new MyHandler(); private ListView listView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment01_layout,container,false); listView = view.findViewById(R.id.listview); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); new Thread(){ @Override public void run() { try { URL url = new URL(HttpConfig.ONE_URL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); if(connection.getResponseCode()==200) { //输入流 InputStream inputStream = connection.getInputStream(); String json = ConmentUtil.inputStream2String(inputStream); Log.d(TAG, "run: 66666666666" + json); Message message = new Message(); message.what = SUCCESS; message.obj = json; myHandler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case SUCCESS: String json = (String) msg.obj; Log.d(TAG, "handleMessage: "+json); Gson gson = new Gson(); // // Type type = new TypeToken<LinkedList<NewsBean>>() { // }.getType(); // LinkedList<NewsBean> list = gson.fromJson(json, type); // List<NewsBean.ItemBean> item = list.get(0).getItem(); Type type = new TypeToken<LinkedList<NewsBean>>() { }.getType(); LinkedList<NewsBean> list = gson.fromJson(json, type); List<NewsBean.ItemBean> item = list.get(0).getItem(); MyAdapter myAdapter = new MyAdapter(getActivity(), item); listView.setAdapter(myAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(getActivity(),"fdfd",Toast.LENGTH_LONG).show(); } }); break; } } } }
HttpURLConnection的使用
最新推荐文章于 2019-02-13 19:26:24 发布