欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Json数据解析模拟美团界面显示

程序员文章站 2023-12-18 15:29:40
<?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>

运行效果图:

Json数据解析模拟美团界面显示

需要用到四个lib包 :解析json gson包,从网络地址解析json数据成string字符串的异步网络解析工具asynchttpclient,等

下载地址:

Json数据解析模拟美团界面显示

代码如下:

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文件:

<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数据解析格式

// 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\"}]}";
// }

上一篇:

下一篇: