HttpURLConnection
来源:互联网 发布:多台网络打印机服务器 编辑:程序博客网 时间:2024/06/06 05:25
public class Fragment01 extends Fragment{ private ListView lv; private List<Result.NewslistEntity> list=new ArrayList<>(); private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { Gson gson=new Gson(); Result result = gson.fromJson(msg.obj.toString(),Result.class); List<Result.NewslistEntity> newslist = result.getNewslist(); Log.i("ZZZ",newslist.toString()); list.addAll(newslist); MyAdapter adapter=new MyAdapter(getActivity(),list); Log.i("ZZZ","集合"+newslist.size()); lv.setAdapter(adapter); } }; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment01,container,false); lv = (ListView) view.findViewById(R.id.lv); new Thread(){ @Override public void run() { requestNewsData(); } }.start(); return view; } public void requestNewsData(){ try { URL url=new URL("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10"); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int code = connection.getResponseCode(); connection.setReadTimeout(10000); connection.setConnectTimeout(10000); if(code==200){ InputStream stream = connection.getInputStream(); String content=StreamToString.streamToStr(stream); Message message=new Message(); message.obj=content.toString(); handler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); }
阅读全文
0 0
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- httpurlconnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- HttpURLconnection
- HttpURLConnection
- HttpURLConnection
- HttpURLConnection
- httpurlconnection
- C语言总述
- arcgis已知坐标点转面(通过excel文件)
- FFmpeg的H264编码有内存泄漏吗??!!!
- 如何实现python3的文件读取
- 012 随机生成n个字节的字符串?
- HttpURLConnection
- Python爬取www.alexa.cn网站上的部分url和相应的等级
- AdvancedInstaller打包工具使用(三)
- 013 QString转QByteArray? sha1算法的使用?
- 皮肤晒黑了怎么快速变白,最好实用的?
- scala编译器的安装
- SDUT 3334 数据结构实验之栈与队列七:出栈序列判定
- 连续总结第二十三天
- 014 QJson解析、封装?