封装AsyncTask成工具类
来源:互联网 发布:淘宝上哪里买书最便宜 编辑:程序博客网 时间:2024/06/06 11:43
先说一下怎么使用:
String s = new GetAsyncTaskData() {}.execute("http://169.254.168.158/mobile/index.php?act=goods&op=goods_list&page=100").get();
接着再去Gson解析就好了,但是下面的代码会等到网络获取到数据之后再进行。
public class GetNetData { public static String getData(String path){ String result = null; try { URL url=new URL(path); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); int code=conn.getResponseCode(); Log.i("-----------", "getData: "+code); if (code==200){ InputStream is=conn.getInputStream(); ByteArrayOutputStream baos=new ByteArrayOutputStream(); int len=-1; byte[] buff=new byte[1024]; while ((len=is.read())!=-1){ baos.write(buff,0,len); } result=new String(baos.toByteArray()); } } catch (Exception e) { e.printStackTrace(); } return result; }}
public class GetAsyncTaskData extends AsyncTask<String,String,String> { @Override protected String doInBackground(String... strings) { String result=GetNetData.getData(strings[0]); return result; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); }}
阅读全文
0 0
- 封装AsyncTask成工具类
- AsyncTask封装为工具类+Imagerloader
- AsyncTask解析(下)——实现自定义AsyncTask网络传输工具类封装
- android AsyncTask工具类
- AsyncTask工具类
- AsyncTask异步工具类
- AsyncTask 工具类
- AsyncTask工具类--pizifusheng
- AsyncTask的套路(线程间通信中处理耗时任务的工具封装类)
- SharePreference封装成工具类
- 异步处理工具类:AsyncTask
- AsyncTask 带进度条工具类
- 异步处理工具类:AsyncTask
- 怎么调用AsyncTask工具类
- Android Notification 通知封装成工具类
- java导出excel封装成工具类
- jdbc封装工具类
- Calendar封装工具类
- Ubuntu16.10安装Pytorch
- Google Protocol Buffer 的使用和原理
- mutex互斥体
- ubuntu下scrapyd部署爬虫项目
- 用Photon中的room做麻将房间
- 封装AsyncTask成工具类
- SwitchHosts管理
- 线程间同步机制之完成量
- 11.8第十一周java作业
- [leetcode]#326. Power of Three
- js文件访问不到
- bzoj 1121: [POI2008]激光发射器SZK
- 自旋锁
- 为什么建议使用类名调用静态成员?