你的位置:首页 > 操作系统

[操作系统]解析网络json数据,模拟美团界面显示。


 1 <??> 2 <RelativeLayout ="http://schemas.android.com/apk/res/android" 3   android:layout_width="match_parent" 4   android:layout_height="80dp" 5   android:background="@android:color/white" > 6  7   <ImageView 8     android:id="@+id/list_icon_img" 9     android:layout_width="80dp"10     android:layout_height="80dp"11     android:src="@drawable/ic_launcher" />12 13   <TextView14     android:id="@+id/list_name_txt"15     android:layout_width="wrap_content"16     android:layout_height="wrap_content"17     android:layout_toRightOf="@id/list_icon_img"18     android:singleLine="true"19     android:text="瑞庭竹岛酒店"20     android:textColor="@android:color/background_dark"21     android:textSize="16sp" />22 23   <TextView24     android:id="@+id/list_coupe_txt"25     android:layout_width="wrap_content"26     android:layout_height="wrap_content"27     android:layout_below="@id/list_name_txt"28     android:layout_toRightOf="@id/list_icon_img"29     android:singleLine="true"30     android:text="网上预定入住可享返现优惠"31     android:textColor="@android:color/holo_red_dark"32     android:textSize="14sp" />33 34   <TextView35     android:id="@+id/list_distance_txt"36     android:layout_width="wrap_content"37     android:layout_height="wrap_content"38     android:layout_alignParentBottom="true"39     android:layout_alignParentRight="true"40     android:layout_marginRight="10dp"41     android:drawableLeft="@drawable/info_map"42     android:text="2.0k"43     android:textColor="@android:color/tab_indicator_text"44     android:textSize="14sp" />45 46   <TextView47     android:id="@+id/list_location_txt"48     android:layout_width="wrap_content"49     android:layout_height="wrap_content"50     android:layout_alignParentBottom="true"51     android:layout_toLeftOf="@id/list_distance_txt"52     android:layout_toRightOf="@id/list_icon_img"53     android:singleLine="true"54     android:text="四川省成都市高新区老成仁路8号成都市高新区老成都市高新区老成都市高新区老成都市高新区老"55     android:textColor="@android:color/tab_indicator_text"56     android:textSize="14sp" />57 -58 59 60 61   <RelativeLayout62     android:layout_width="wrap_content"63     android:layout_height="wrap_content"64     android:layout_alignParentRight="true"65     android:layout_alignParentTop="true" >66 67     <ImageView68       android:id="@+id/list_card_img"69       android:layout_width="wrap_content"70       android:layout_height="wrap_content"71       android:src="@drawable/near_card" />72 73     <ImageView74       android:id="@+id/list_group_img"75       android:layout_width="wrap_content"76       android:layout_height="wrap_content"77       android:layout_toRightOf="@id/list_card_img"78       android:src="@drawable/near_group" />79 80     <ImageView81       android:id="@+id/list_ticket_img"82       android:layout_width="wrap_content"83       android:layout_height="wrap_content"84       android:layout_toRightOf="@id/list_group_img"85       android:src="@drawable/near_ticket" />86   </RelativeLayout>87 88 </RelativeLayout>

运行效果图:

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

下载地址:点击下载

 

代码如下:

 1 package com.lixu.testjsonall; 2  3 import java.util.ArrayList; 4 import java.util.List; 5 import com.google.gson.Gson; 6 import com.loopj.android.http.AsyncHttpClient; 7 import com.loopj.android.http.TextHttpResponseHandler; 8 import com.squareup.picasso.Picasso; 9 import android.app.Activity; 10 import android.content.Context; 11 import android.os.Bundle; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.view.ViewGroup; 15 import android.view.Window; 16 import android.widget.ArrayAdapter; 17 import android.widget.ImageView; 18 import android.widget.ListView; 19 import android.widget.TextView; 20 import android.widget.Toast; 21 import cz.msebera.android.httpclient.Header; 22  23 public class MainActivity extends Activity { 24   private String net_url = "http://192.168.1.139/json/around"; 25   private ListView lv; 26   private MyAdapter mMyAdapter; 27  28   @Override 29   protected void onCreate(Bundle savedInstanceState) { 30     super.onCreate(savedInstanceState); 31     requestWindowFeature(Window.FEATURE_NO_TITLE); 32     setContentView(R.layout.activity_main); 33  34     lv = (ListView) findViewById(R.id.lv); 35  36     mMyAdapter = new MyAdapter(this, -1); 37  38     lv.setAdapter(mMyAdapter); 39  40     AsyncHttpClient ahc = new AsyncHttpClient(); 41     ahc.get(net_url, new TextHttpResponseHandler() { 42       // 通过网络地址解析Json数据成String类型 43       @Override 44       public void onSuccess(int arg0, Header[] arg1, String arg2) { 45         // 从字符串中解析json文件 46         Gson gson = new Gson(); 47         Infoall infoall = gson.fromJson(arg2, Infoall.class); 48         Info info = infoall.getInfo(); 49         List<MerchantKey> mMerchantKey = info.getMerchantKey(); 50         mMyAdapter.setList(mMerchantKey); 51       } 52  53       @Override 54       public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) { 55  56         Toast.makeText(getApplicationContext(), "错误!", 0).show(); 57       } 58     }); 59  60   } 61  62   private class MyAdapter extends ArrayAdapter { 63     private LayoutInflater flater; 64     private List<MerchantKey> data = new ArrayList<MerchantKey>(); 65     private Context context; 66  67     public MyAdapter(Context context, int resource) { 68       super(context, resource); 69       this.context = context; 70       flater = LayoutInflater.from(context); 71     } 72  73     public void setList(List<MerchantKey> data) { 74       this.data = data; 75       mMyAdapter.notifyDataSetChanged(); 76     } 77  78     @Override 79     public View getView(int position, View convertView, ViewGroup parent) { 80       if (convertView == null) 81         convertView = flater.inflate(R.layout.list, null); 82       TextView biaoti = (TextView) convertView.findViewById(R.id.list_name_txt); 83       biaoti.setText(data.get(position).getName()); 84       TextView biaoti2 = (TextView) convertView.findViewById(R.id.list_coupe_txt); 85       biaoti2.setText(data.get(position).getCoupon()); 86  87       TextView dizhi = (TextView) convertView.findViewById(R.id.list_location_txt); 88       dizhi.setText(data.get(position).getLocation()); 89  90       TextView juli = (TextView) convertView.findViewById(R.id.list_distance_txt); 91       juli.setText(data.get(position).getDistance()); 92  93       ImageView jpg = (ImageView) convertView.findViewById(R.id.list_icon_img); 94       Picasso.with(context).load(data.get(position).getPicUrl()).into(jpg); 95  96       ImageView tuan = (ImageView) convertView.findViewById(R.id.list_group_img); 97       ImageView quan = (ImageView) convertView.findViewById(R.id.list_ticket_img); 98       ImageView ka = (ImageView) convertView.findViewById(R.id.list_card_img); 99 100       if (data.get(position).getGroupType().equals("YES")) {101         tuan.setVisibility(View.VISIBLE);102       } else {103         tuan.setVisibility(View.GONE);104       }105       if (data.get(position).getCardType().equals("YES")) {106         ka.setVisibility(View.VISIBLE);107       } else {108         ka.setVisibility(View.GONE);109       }110       if (data.get(position).getCouponType().equals("YES")) {111         quan.setVisibility(View.VISIBLE);112       } else {113         quan.setVisibility(View.GONE);114       }115 116       return convertView;117 118     }119 120     @Override121     public int getCount() {122 123       return data.size();124     }125 126   }127 128 }

 1 package com.lixu.testjsonall; 2  3 import java.util.List; 4  5 public class Info { 6   private List<MerchantKey> merchantKey; 7  8   public List<MerchantKey> getMerchantKey() { 9     return merchantKey;10   }11 12   public void setMerchantKey(List<MerchantKey> merchantKey) {13     this.merchantKey = merchantKey;14   }15 16 }

 1 package com.lixu.testjsonall; 2  3 public class Infoall { 4  5   private Info info; 6  7   public Info getInfo() { 8     return info; 9   }10 11   public void setInfo(Info info) {12     this.info = info;13   }14 15 }

 1 package com.lixu.testjsonall; 2  3 public class MerchantKey { 4   private String name; 5   private String coupon; 6   private String location; 7   private String distance; 8   private String picUrl; 9   private String couponType;10   private String cardType;11   private String groupType;12 13   public String getName() {14     return name;15   }16 17   public void setName(String name) {18     this.name = name;19   }20 21   public String getCoupon() {22     return coupon;23   }24 25   public void setCoupon(String coupon) {26     this.coupon = coupon;27   }28 29   public String getLocation() {30     return location;31   }32 33   public void setLocation(String location) {34     this.location = location;35   }36 37   public String getDistance() {38     return distance;39   }40 41   public void setDistance(String distance) {42     this.distance = distance;43   }44 45   public String getPicUrl() {46     return picUrl;47   }48 49   public void setPicUrl(String picUrl) {50     this.picUrl = picUrl;51   }52 53   public String getCouponType() {54     return couponType;55   }56 57   public void setCouponType(String couponType) {58     this.couponType = couponType;59   }60 61   public String getCardType() {62     return cardType;63   }64 65   public void setCardType(String cardType) {66     this.cardType = cardType;67   }68 69   public String getGroupType() {70     return groupType;71   }72 73   public void setGroupType(String groupType) {74     this.groupType = groupType;75   }76 77 }

 1 <LinearLayout ="http://schemas.android.com/apk/res/android" 2   ="http://schemas.android.com/tools" 3   android:layout_width="match_parent" 4   android:layout_height="match_parent" 5   android:orientation="vertical" 6   tools:context="com.lixu.testjsonall.MainActivity" > 7  8   <LinearLayout 9     android:layout_width="match_parent"10     android:layout_height="wrap_content"11     android:background="@drawable/title_log"12     android:orientation="horizontal" >13 14     <ImageView15       android:id="@+id/fanhui"16       android:layout_width="30dp"17       android:layout_height="30dp"18       android:layout_gravity="center"19       android:src="@drawable/btn_back" />20 21     <TextView22       android:layout_width="wrap_content"23       android:layout_height="wrap_content"24       android:layout_gravity="center"25       android:text="  我的关注"26       android:textSize="25sp" />27   </LinearLayout>28 29   <ListView30     android:id="@+id/lv"31     android:layout_width="match_parent"32     android:layout_height="match_parent" />33 34 </LinearLayout>