AsyncTask异步加载和HttpURLConnection网络请求数据
//获得网络数据
private void huodeshuju() {
//这里是使用线程,已注释掉
/*new Thread(){
public void run() {
try {
URL url=new URL(urlPath);
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int code=urlConnection.getResponseCode();
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer buffer=new StringBuffer();
while ((line=reader.readLine())!=null) {
buffer.append(line);
}
String str=buffer.toString();
Message message=new Message();
message.what=0;
message.obj=str;
handler.sendMessage(message);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();*/
//----------下面是使用的异步加载-----------------------------
new AsyncTask<String, Void, String>(){
@Override
protected String doInBackground(String... params) {
try {
URL url=new URL(params[0]);
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int code=urlConnection.getResponseCode();
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String readerline;
StringBuffer buffer=new StringBuffer();
while ((readerline=reader.readLine())!=null) {
buffer.append(readerline);
}
String str=buffer.toString();
return str;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
try {
JSONObject obj=new JSONObject(result);
JSONArray data=obj.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject json=data.getJSONObject(i);
String id=json.getString("id");
String goods_name=json.getString("goods_name");
String shop_price=json.getString("shop_price");
String market_price=json.getString("market_price");
String is_coupon_allowed=json.getString("is_coupon_allowed");
String is_allow_credit=json.getString("is_allow_credit");
String goods_img=json.getString("goods_img");
String sales_volume=json.getString("sales_volume");
String efficacy=json.getString("efficacy");
Goods goodss=new Goods(id, goods_name, shop_price, market_price, is_coupon_allowed, is_allow_credit, goods_img, sales_volume, efficacy);
goods.add(goodss);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.execute(urlPath);//urlpath为网址
}
AsyncTask异步加载和HttpURLConnection网络请求数据的更多相关文章
- Android AsyncTask异步加载WebAPI
之前做的程序一直存在很多问题,因为需要加载的Activity需要从网络加载数据.并没有完全正确的使用异步的方法去加载! 之前用的虽然是AsyncTask,但是在加载完成的时候还是并没有使用AsyncT ...
- AsyncTask异步加载和Gson
public class MainActivity extends Activity { private List<Goods> goods=new ArrayList<Goo ...
- Android 应用开发 之通过AsyncTask与ThreadPool(线程池)两种方式异步加载大量数据的分析与对比--转载
在加载大量数据的时候,经常会用到异步加载,所谓异步加载,就是把耗时的工作放到子线程里执行,当数据加载完毕的时候再到主线程进行UI刷新.在数据量非常大的情况下,我们通常会使用两种技术来进行异步加载,一 ...
- Android开发--异步加载
因为移动端软件开发思维模式或者说是开发的架构其实是不分平台和编程语言的,就拿安卓和IOS来说,他们都是移动前端app开发展示数据和用户交互数据的数据终端,移动架构的几个大模块:UI界面展示.本地数据可 ...
- android 之图片异步加载
一.概述 本文来自"慕课网" 的学习,只是对代码做一下分析 图片异步加载有2种方式: (多线程/线程池) 或者 用其实AsyncTask , 其实AsyncTask底层也是用的多 ...
- Android 之异步加载LoaderManager
LoaderManager: Loader出现的背景: Activity是我们的前端页面展现,数据库是我们的数据持久化地址,那么正常的逻辑就是在展示页面的渲染页面的阶段进行数据库查询.拿到数据以后才展 ...
- Jquery树控件ZTree异步加载
异步加载的意思就是: 当点击展开树节点时,才去请求后台action返回点击节点的子节点数据并加载. 这里面主要设计ztree的setting变量的async属性设置: var setting = { ...
- Jquery Ztree异步加载树
1. 下载jquery的JS文件/ztree的CSS文件和JS文件 https://jquery.com/download/ https://gitee.com/zTree/zTree_v3/tree ...
- ztree异步加载---------补发周日内容
上周六老师要求和大三的进行JAVA知识交流,总体来说就是给大三学长做的东西打分,然后大三学长再教我们如果构建ztree.毕竟第一次接触ztree,所以有很多不了解,但通过周六日努力,还是做出来了.现在 ...
随机推荐
- 有关app的一些小知识
META相关 1. 添加到主屏后的标题(IOS)<meta name="apple-mobile-web-app-title" content="标题"& ...
- OC之消息调用过程
Bird * aBird = [[Bird alloc] init]; [aBird fly]; 中对 fly 的调用,编译器通过插入一些代码,将之转换为对方法具体实现 IMP 的调用,这个 IMP ...
- Openjudge-计算概论(A)-整数的个数
描述: 给定k(1<k<100)个正整数,其中每个数都是大于等于1,小于等于10的数.写程序计算给定的k个正整数中,1,5和10出现的次数.输入输入有两行:第一行包含一个正整数k,第二行包 ...
- html 图片上传预览
Html5 upload img 2012年12月27日 20:36 <!DOCTYPE HTML> <html> <head> <meta http-equ ...
- 复用TCP连接提升流媒体服务器之间流量转发效率
由于媒体推流客户端所在地域不同.所接入网络运营商不同.就近接入原则等因素,导致不同的视频推流客户端会推流至不同的流媒体服务器(本文主要针对目前WEB或手机的基于TCP的流媒体服务器),在某流媒体服务器 ...
- CSS传统布局之display属性+float属性+position属性
这三个属性是传统网页布局中经常用到的属性. 读这篇文章之前,希望你对css布局模型已经有了一定的了解.因为本文的三个属性是和css三个布局模型紧密联系在一起的.因此,如若你并不了解,我推荐你先看一下c ...
- Load$$ execution region symbols
6.3.3 Load$$ execution region symbols The linker generates Load$$ symbols for every execution region ...
- python常用正则表达式
匹配特定数字:^[1-9]\d*//匹配正整数−[1−9]\d∗ //匹配负整数^-?[1-9]\d*//匹配整数[1−9]\d∗|0 //匹配非负整数(正整数 + 0)^-[1-9]\d*|0// ...
- 推荐两个界面原型设计工具--GUIDesignStudio 和 Mockups For Desktop
前段时间,有幸参加一次高级软件架构师的培训,授课老师介绍了两个很好玩的界面原型设计工具:GUIDesignStudio 和 Mockups For Desktop,现分享一下,截图说明,洗洗眼球,权当 ...
- hdu_5680_zxa and set(想法题)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5680 题意: 问题描述 zxa有一个集合A=\{a_1,a_2,\cdots,a_n\}A={a1 ...