1. public class MainActivity extends Activity {
  2.  
  3. protected static final int WHAT_REQUEST_SUCCESS = 1;
  4. protected static final int WHAT_REQUEST_ERROR = 2;
  5. private ListView lv_main;
  6. private LinearLayout ll_main_loading;
  7. private List<ShopInfo> data;
  8. private ShopInfoAdapter adapter;
  9. private Handler handler = new Handler(){
  10. public void handleMessage(android.os.Message msg) {
  11. switch (msg.what) {
  12. case WHAT_REQUEST_SUCCESS:
  13. ll_main_loading.setVisibility(View.GONE);
  14. //显示列表
  15. lv_main.setAdapter(adapter);
  16. break;
  17. case WHAT_REQUEST_ERROR:
  18. ll_main_loading.setVisibility(View.GONE);
  19. Toast.makeText(MainActivity.this, "加载数据失败", Toast.LENGTH_SHORT).show();
  20. break;
  21.  
  22. default:
  23. break;
  24. }
  25. }
  26. };
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_main);
  31.  
  32. lv_main = (ListView) findViewById(R.id.lv_main);
  33. ll_main_loading = (LinearLayout) findViewById(R.id.ll_main_loading);
  34. adapter = new ShopInfoAdapter();
  35.  
  36. //1. 主线程, 显示提示视图
  37. ll_main_loading.setVisibility(View.VISIBLE);
  38. //2. 分线程, 联网请求
  39. //启动分线程请求服务器动态加载数据并显示
  40. new Thread(){
  41. public void run() {
  42. //联网请求得到jsonString
  43. try {
  44. String jsonString = requestJson();
  45. //解析成List<ShopInfo>
  46. data = new Gson().fromJson(jsonString, new TypeToken<List<ShopInfo>>(){}.getType());
  47. //3. 主线程, 更新界面
  48. handler.sendEmptyMessage(WHAT_REQUEST_SUCCESS);//发请求成功的消息
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. handler.sendEmptyMessage(WHAT_REQUEST_ERROR);//发送请求失败的消息
  52. }
  53. }
  54. }.start();
  55.  
  56. }
  57.  
  58. /**
  59. * 联网请求得到jsonString
  60. * @return
  61. * @throws Exception
  62. */
  63. private String requestJson() throws Exception {
  64. String result = null;
  65. String path = "http://192.168.10.165:8080/L05_Web/ShopInfoListServlet";
  66. //1. 得到连接对象
  67. URL url = new URL(path);
  68. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  69. //2. 设置
  70. connection.setConnectTimeout(5000);
  71. connection.setReadTimeout(5000);
  72. //连接
  73. connection.connect();
  74. //发请求并读取服务器返回的数据
  75. int responseCode = connection.getResponseCode();
  76. if(responseCode==200) {
  77. InputStream is = connection.getInputStream();
  78. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  79. byte[] buffer = new byte[1024];
  80. int len = -1;
  81. while ((len = is.read(buffer)) != -1) {
  82. baos.write(buffer, 0, len);
  83. }
  84. baos.close();
  85. is.close();
  86. connection.disconnect();
  87.  
  88. result = baos.toString();
  89. } else {
  90. //也可以抛出运行时异常
  91. }
  92. return result;
  93. }
  94.  
  95. class ShopInfoAdapter extends BaseAdapter {
  96.  
  97. private ImageLoader imageLoader;
  98.  
  99. public ShopInfoAdapter() {
  100. imageLoader = new ImageLoader(MainActivity.this, R.drawable.loading, R.drawable.error);
  101. }
  102. @Override
  103. public int getCount() {
  104. return data.size();
  105. }
  106.  
  107. @Override
  108. public Object getItem(int position) {
  109. return data.get(position);
  110. }
  111.  
  112. @Override
  113. public long getItemId(int position) {
  114. return 0;
  115. }
  116.  
  117. @Override
  118. public View getView(int position, View convertView, ViewGroup parent) {
  119. if(convertView==null) {
  120. convertView = View.inflate(MainActivity.this, R.layout.item_main, null);
  121. }
  122. //得到当前行的数据对象
  123. ShopInfo shopInfo = data.get(position);
  124. //得到当前行的子View
  125. TextView nameTV = (TextView) convertView.findViewById(R.id.tv_item_name);
  126. TextView priceTV = (TextView) convertView.findViewById(R.id.tv_item_price);
  127. ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_item_icon);
  128. //设置数据
  129. nameTV.setText(shopInfo.getName());
  130. priceTV.setText(shopInfo.getPrice()+"元");
  131. String imagePath = shopInfo.getImagePath();
  132. //根据图片路径启动分线程动态请求服务加载图片并显示
  133. imageLoader.loadImage(imagePath, imageView);
  134. return convertView;
  135. }
  136.  
  137. }
  138. }
 
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent" >
  4.  
  5. <ListView
  6. android:id="@+id/lv_main"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent" >
  9. </ListView>
  10.  
  11. <LinearLayout
  12. android:id="@+id/ll_main_loading"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:orientation="vertical"
  16. android:gravity="center"
  17. android:visibility="gone">
  18.  
  19. <ProgressBar
  20. style="?android:attr/progressBarStyleLarge"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content" />
  23.  
  24. <TextView
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="正在加载中..." />
  28.  
  29. </LinearLayout>
  30.  
  31. </FrameLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="60dp"
  5. android:orientation="horizontal" >
  6.  
  7. <ImageView
  8. android:id="@+id/iv_item_icon"
  9. android:layout_width="60dp"
  10. android:layout_height="60dp"
  11. android:src="@drawable/ic_launcher"/>
  12.  
  13. <LinearLayout
  14. android:layout_width="fill_parent"
  15. android:layout_height="match_parent"
  16. android:orientation="vertical"
  17. android:gravity="center_vertical"
  18. android:layout_marginLeft="10dp">
  19.  
  20. <TextView
  21. android:id="@+id/tv_item_name"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="名称"
  25. android:textSize="18sp"/>
  26.  
  27. <TextView
  28. android:id="@+id/tv_item_price"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="内容"
  32. android:textSize="18sp"/>
  33.  
  34. </LinearLayout>
  35. </LinearLayout>

远程请求json数据,list中显示的更多相关文章

  1. AJAX跨域请求json数据的实现方法

    这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式, ...

  2. bootstrap通过ajax请求JSON数据后填充到模态框

    1.   JSP页面中准备模态框 <!-- 详细信息模态框(Modal) --> <div> <div class="modal fade" id=& ...

  3. 关于使用Ajax请求json数据,@RequestMapping返回中文乱码的几种解决办法

    一.问题描述: 使用ajax请求json数据的时候,无论如何返回的响应编码都是ISO-8859-1类型,因为统一都是utf-8编码,导致出现返回结果中文乱码情况. $.ajax({ type:&quo ...

  4. EHlib在数据单元中显示字段值为图形。

    -[定制网格数据单元]  在数据单元中显示字段值为图形.  TDBGridEh allows to show bitmaps from TImageList component depending o ...

  5. react之fetch请求json数据

    Fetch下载 npm install whatwg-fetch -S Fetch请求json数据 json文件要放在public内部才能被检索到

  6. ajax 请求json数据中json对象的构造获取问题

    前端的界面中,我想通过ajax来调用写好的json数据,并调用add(data)方法进行解析,请求如下: json数据如下: { “type”:"qqq", "lat&q ...

  7. ajax请求json数据跨域问题(转)

    一.后台代理技术 由服务器端向跨域下的网站发出请求,再将请求结果返回给前端,成功避免同源策略的限制. 具体操作如下: 1.在localhost:81/a.html中,向同源下的某个代理程序发出请求 $ ...

  8. Struts2 Action接收POST请求JSON数据及其实现解析

    一.认识JSON JSON是一种轻量级.基于文本.与语言无关的数据交换格式,可以用文本格式的形式来存储或表示结构化的数据. 二.POST请求与Content-Type: application/jso ...

  9. Android开发——获得Json数据,并显示图片

    流程介绍 使用okhttp网络框架进行get请求,获得json数据 //一个封装好的工具类的静态方法 public static void sendOkHttpRequest(final String ...

随机推荐

  1. AC日记——线段树练习5 codevs 4927

    4927 线段树练习5  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有n个数和5种操作 add a b ...

  2. vue 权限控制按钮3种样式、内容、以及跳转事件

    最近碰到一个因为要根据权限来给一个按钮变成不同功能, 简单写出3个按钮然后用v-if也能实现这个功能,但是在加载页面时,如果延迟过高则会把按钮按照DOM顺序加载出来,这是个很不好的效果 思索了下,把三 ...

  3. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  4. 平衡树与可持久化treap

    平衡树(二叉树) 线段树不支持插入or删除一个数于是平衡树产生了 常见平衡树:treap(比sbt慢,好写吧),SBT(快,比较好写,有些功能不支持),splay(特别慢,复杂度当做根号n来用,功能强 ...

  5. springboot快速集成swagger

    今天技术总监说:小明,我们本次3.0改造,使用swagger2.0作为前后端分离的接口规范,它可以一键生成前后端的API,一劳永逸--小明:??? Spring Boot 框架是目前非常流行的微服务框 ...

  6. BT中的磁力链接(转)

    注意:磁力链接不是迅雷的,而是BT网络中的一种协议. 磁力链接与种子文件 磁力链接并不是一个新概念,早在2002年,相关的标准草稿就已经制定了.但直到2012年海盗湾为规避版权问题删除了站点上的所有T ...

  7. screen状态变Attached连接会话失败

    使用xshell远程登录主机,使用screen命令启动程序运行至后台,意外发现screen session的状态为Attached,使用命令screen -r <session-id>,提 ...

  8. iOS -- 十进制、十六进制字符串,byte,data等之间的转换

    十进制->十六进制 Byte bytes[]={0xA6,0x27,0x0A}; NSString *strIdL = [NSStringstringWithFormat:]]]; 十六进制-& ...

  9. DICOM医学图像处理:Deconstructed PACS之Orthanc,Modification & Anonymization

    背景: 上篇博文为引子,介绍了一款神奇的开源PACS系统——Orthanc.本篇开始解读官方Cookbook中的相关内容,对于简单的浏览.访问和上传请阅读前篇博文.在常规的PACS系统中还未出现对于D ...

  10. [反汇编练习] 160个CrackMe之033

    [反汇编练习] 160个CrackMe之033. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...