//两个权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
android:name=".Appi" Bean类加toString
import android.os.Handler; import android.os.Message; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.ImageLoader; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{ private XListView lv; private ImageView img2; private LinearLayout aaaa; private DrawerLayout dl; private StringBuilder builder; List<User> list=new ArrayList<User>(); private mydata mydata; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); mydata.notifyDataSetChanged(); close(); } }; private void close() { lv.stopLoadMore(); lv.stopRefresh(); lv.setRefreshTime("2017-09-08"); } private View view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (XListView) findViewById(R.id.xlv); dl = (DrawerLayout) findViewById(R.id.dl); img2 = (ImageView) findViewById(R.id.img2); aaaa = (LinearLayout) findViewById(R.id.aaaa); img2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { dl.openDrawer(aaaa); } }); //获取json new Thread(){ @Override public void run() { super.run(); try { URL url = new URL("http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=0&qtime=20160411091603"); try { URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); builder = new StringBuilder(); String tem=""; while((tem=reader.readLine())!=null){ builder.append(tem); } Log.e("=========读取到的数据========",builder.toString()); } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } //解析json runOnUiThread(new Runnable() { @Override public void run() { //解析数据 Gson gson = new Gson(); Bean bean = gson.fromJson(builder.toString(), Bean.class); Bean.ResultBean result = bean.getResult(); List<Bean.ResultBean.RowsBean> rows = result.getRows(); for(int i=0;i<rows.size();i++){ Bean.ResultBean.RowsBean.InfoBean info = rows.get(i).getInfo(); String address = info.getAddress(); String image = info.getDefault_image(); List<Bean.ResultBean.RowsBean.InfoBean.ActivityBean> activity = info.getActivity(); for(int j=0;j<activity.size();j++){ String title=activity.get(j).getTitle(); list.add(new User(title,address,image)); } } Log.e("========解析到的数据========",list.toString()); lv.setPullRefreshEnable(true); lv.setPullLoadEnable(true); lv.setXListViewListener(MainActivity.this); //创建适配器 mydata = new mydata(); //设置适配器 lv.setAdapter(mydata); } }); } }.start(); } //上啦刷新 @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { User user=new User("京东方","交电话费绝对是","http://pic1.ajkimg.com/display/xinfang/6ac7bfb385ce1f67555f6ac9ace994f7/275x206n.jpg"); list.add(0,user); handler.sendEmptyMessage(0); } },2000); } //下拉加载 @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { User user=new User("京东方","交电话费绝对是","http://pic1.ajkimg.com/display/xinfang/6ac7bfb385ce1f67555f6ac9ace994f7/275x206n.jpg"); list.add(user); handler.sendEmptyMessage(0); } },2000); } class mydata extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder vh; if(view==null){ vh=new ViewHolder(); view = View.inflate(MainActivity.this, R.layout.scenic_item_list, null); vh.tile= view.findViewById(R.id.title); vh.type= view.findViewById(R.id.content); vh.img= view.findViewById(R.id.img); view.setTag(vh); }else{ vh= (ViewHolder) view.getTag(); } vh.tile.setText(list.get(i).tile); vh.type.setText(list.get(i).type); //获取图片 ImageLoader.getInstance().displayImage(list.get(i).img,vh.img); return view; } class ViewHolder { TextView tile,type; ImageView img; } } } Bean类自动解析public class User { public String tile; public String type; public String img; public User(String tile, String type, String img) { this.tile = tile; this.type = type; this.img = img; } @Override public String toString() { return "User{" + "tile='" + tile + '\'' + ", type='" + type + '\'' + ", img='" + img + '\'' + '}'; } }import android.app.Application; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; /** * Created by HQ on 2017/9/8. */ public class Appi extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(build); } }<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout android:id="@+id/dl" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="bbb.bwie.com.dierzhou2.MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/img2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher_round" /> <bbb.bwie.com.dierzhou2.XListView android:id="@+id/xlv" android:layout_width="match_parent" android:layout_height="match_parent"> </bbb.bwie.com.dierzhou2.XListView> </LinearLayout> <LinearLayout android:id="@+id/aaaa" android:layout_width="250dp" android:layout_height="match_parent" android:orientation="horizontal" android:background="@mipmap/ic_launcher" android:layout_gravity="left" android:choiceMode="singleChoice" ></LinearLayout> </android.support.v4.widget.DrawerLayout>