亲宝软件园·资讯

展开

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

0代码狂人0 人气:3
想了解Json数据解析模拟美团界面显示的相关内容吗,0代码狂人0在本文为您仔细讲解美团Json数据解析 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:美团Json数据解析,json解析,下面大家一起来学习吧。
<?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,等

下载地址:点击下载

代码如下:

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

加载全部内容

相关教程
猜你喜欢
用户评论