1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<?xml version= "." encoding= "utf-" ?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "dp" android:background= "@android:color/white" > <imageview android:id= "@+id/list_icon_img" android:layout_width= "dp" android:layout_height= "dp" android:src= "@drawable/ic_launcher" /> <textview android:id= "@+id/list_name_txt" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_torightof= "@id/list_icon_img" android:singleline= "true" android:text= "瑞庭竹岛酒店" android:textcolor= "@android:color/background_dark" android:textsize= "sp" /> <textview android:id= "@+id/list_coupe_txt" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/list_name_txt" android:layout_torightof= "@id/list_icon_img" android:singleline= "true" android:text= "网上预定入住可享返现优惠" android:textcolor= "@android:color/holo_red_dark" android:textsize= "sp" /> <textview android:id= "@+id/list_distance_txt" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignparentbottom= "true" android:layout_alignparentright= "true" android:layout_marginright= "dp" android:drawableleft= "@drawable/info_map" android:text= ".k" android:textcolor= "@android:color/tab_indicator_text" android:textsize= "sp" /> <textview android:id= "@+id/list_location_txt" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignparentbottom= "true" android:layout_toleftof= "@id/list_distance_txt" android:layout_torightof= "@id/list_icon_img" android:singleline= "true" android:text= "四川省成都市高新区老成仁路号成都市高新区老成都市高新区老成都市高新区老成都市高新区老" android:textcolor= "@android:color/tab_indicator_text" android:textsize= "sp" /> - <relativelayout android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_alignparentright= "true" android:layout_alignparenttop= "true" > <imageview android:id= "@+id/list_card_img" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/near_card" /> <imageview android:id= "@+id/list_group_img" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_torightof= "@id/list_card_img" android:src= "@drawable/near_group" /> <imageview android:id= "@+id/list_ticket_img" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_torightof= "@id/list_group_img" android:src= "@drawable/near_ticket" /> </relativelayout> </relativelayout> |
运行效果图:
需要用到四个lib包 :解析json gson包,从网络地址解析json数据成string字符串的异步网络解析工具asynchttpclient,等
下载地址:
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
package com.lixu.testjsonall; import java.util.arraylist; import java.util.list; import com.google.gson.gson; import com.loopj.android.http.asynchttpclient; import com.loopj.android.http.texthttpresponsehandler; import com.squareup.picasso.picasso; import android.app.activity; import android.content.context; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.view.window; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; import cz.msebera.android.httpclient.header; public class mainactivity extends activity { private string net_url = "http://.../json/around" ; private listview lv; private myadapter mmyadapter; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); lv = (listview) findviewbyid(r.id.lv); mmyadapter = new myadapter( this , -); lv.setadapter(mmyadapter); asynchttpclient ahc = new asynchttpclient(); ahc.get(net_url, new texthttpresponsehandler() { // 通过网络地址解析json数据成string类型 @override public void onsuccess( int arg, header[] arg, string arg) { // 从字符串中解析json文件 gson gson = new gson(); infoall infoall = gson.fromjson(arg, infoall. class ); info info = infoall.getinfo(); list<merchantkey> mmerchantkey = info.getmerchantkey(); mmyadapter.setlist(mmerchantkey); } @override public void onfailure( int arg, header[] arg, string arg, throwable arg) { toast.maketext(getapplicationcontext(), "错误!" , ).show(); } }); } private class myadapter extends arrayadapter { private layoutinflater flater; private list<merchantkey> data = new arraylist<merchantkey>(); private context context; public myadapter(context context, int resource) { super (context, resource); this .context = context; flater = layoutinflater.from(context); } public void setlist(list<merchantkey> data) { this .data = data; mmyadapter.notifydatasetchanged(); } @override public view getview( int position, view convertview, viewgroup parent) { if (convertview == null ) convertview = flater.inflate(r.layout.list, null ); textview biaoti = (textview) convertview.findviewbyid(r.id.list_name_txt); biaoti.settext(data.get(position).getname()); textview biaoti = (textview) convertview.findviewbyid(r.id.list_coupe_txt); biaoti.settext(data.get(position).getcoupon()); textview dizhi = (textview) convertview.findviewbyid(r.id.list_location_txt); dizhi.settext(data.get(position).getlocation()); textview juli = (textview) convertview.findviewbyid(r.id.list_distance_txt); juli.settext(data.get(position).getdistance()); imageview jpg = (imageview) convertview.findviewbyid(r.id.list_icon_img); picasso.with(context).load(data.get(position).getpicurl()).into(jpg); imageview tuan = (imageview) convertview.findviewbyid(r.id.list_group_img); imageview quan = (imageview) convertview.findviewbyid(r.id.list_ticket_img); imageview ka = (imageview) convertview.findviewbyid(r.id.list_card_img); if (data.get(position).getgrouptype().equals( "yes" )) { tuan.setvisibility(view.visible); } else { tuan.setvisibility(view.gone); } if (data.get(position).getcardtype().equals( "yes" )) { ka.setvisibility(view.visible); } else { ka.setvisibility(view.gone); } if (data.get(position).getcoupontype().equals( "yes" )) { quan.setvisibility(view.visible); } else { quan.setvisibility(view.gone); } return convertview; } @override public int getcount() { return data.size(); } } } package com.lixu.testjsonall; import java.util.list; public class info { private list<merchantkey> merchantkey; public list<merchantkey> getmerchantkey() { return merchantkey; } public void setmerchantkey(list<merchantkey> merchantkey) { this .merchantkey = merchantkey; } } package com.lixu.testjsonall; public class infoall { private info info; public info getinfo() { return info; } public void setinfo(info info) { this .info = info; } } package com.lixu.testjsonall; public class merchantkey { private string name; private string coupon; private string location; private string distance; private string picurl; private string coupontype; private string cardtype; private string grouptype; public string getname() { return name; } public void setname(string name) { this .name = name; } public string getcoupon() { return coupon; } public void setcoupon(string coupon) { this .coupon = coupon; } public string getlocation() { return location; } public void setlocation(string location) { this .location = location; } public string getdistance() { return distance; } public void setdistance(string distance) { this .distance = distance; } public string getpicurl() { return picurl; } public void setpicurl(string picurl) { this .picurl = picurl; } public string getcoupontype() { return coupontype; } public void setcoupontype(string coupontype) { this .coupontype = coupontype; } public string getcardtype() { return cardtype; } public void setcardtype(string cardtype) { this .cardtype = cardtype; } public string getgrouptype() { return grouptype; } public void setgrouptype(string grouptype) { this .grouptype = grouptype; } } |
xml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "com.lixu.testjsonall.mainactivity" > <linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:background= "@drawable/title_log" android:orientation= "horizontal" > <imageview android:id= "@+id/fanhui" android:layout_width= "dp" android:layout_height= "dp" android:layout_gravity= "center" android:src= "@drawable/btn_back" /> <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center" android:text= " 我的关注" android:textsize= "sp" /> </linearlayout> <listview android:id= "@+id/lv" android:layout_width= "match_parent" android:layout_height= "match_parent" /> </linearlayout> |
解析网络json数据,模拟美团界面显示相关内容就给大家介绍到这里,希望对大家有所帮助。
接着给大家介绍模拟json数据解析格式
1
2
3
4
5
6
7
|
// string response = // "{\"code\":\"1\",\"desc\":\"success\",\"data\":{\"showcount\":\"10\",\"totalcount\":\"1\",\"totalpage\":\"1\",\"currpage\":\"1\",\"offset\":\"0\"},\"apps\":[" // + // "{\"id\":1,\"status\":\"1\",\"url\":\"www.qq.com\",\"rank\":\"1\",\"title\":\"qq\",\"createtime\":\"1325088000000\",\"updatetime\":\"1325088000000\",\"remarks\":\"??\",\"intro\":\"酷我音乐2012是酷我音乐专为android平台量身定做的专业在线音乐播放\",\"isusable\":\"1\",\"isjoint\":\"1\",\"micon\":\"http://www.51anzhuo.com.cn/upload/widget/splidapps/m/2011-10/vapk_1.jpg\",\"appcount\":\"20\",\"storename\":\"anzhuo\"}," // + // "{\"id\":2,\"status\":\"1\",\"url\":\"www.wabang.com\",\"rank\":\"1\",\"title\":\"web\",\"createtime\":\"1325606400000\",\"updatetime\":\"1325606400000\",\"remarks\":\"??\",\"intro\":\"正点闹钟,基本上其他闹钟满足不了的功能都涵盖了\",\"isusable\":\"1\",\"isjoint\":\"1\",\"micon\":\"http://www.51anzhuo.com.cn/upload/widget/splidapps/m/2011-10/vapk_1.jpg\",\"appcount\":\"20\",\"storename\":\"anzhuo\"}]}"; // } |