package com.bwie.xlistview;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.bwie.bean.Bean;
import com.bwie.bean.Data;
import com.bwie.xlistview.view.XListView;
import com.bwie.xlistview.view.XListView.IXListViewListener;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class MainActivity extends Activity implements IXListViewListener {
private XListView xListView;
int i = 1;
List<Data> list = new ArrayList<Data>();
Handler handler = new Handler() {
private MyAdapter myAdapter;
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String s = (String) msg.obj;
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
int tag = msg.arg1;
switch (tag) {
// 下拉刷新
case 1:
// 下拉刷新之前要清除数据
list.clear();
// 添加新的数据
list.addAll(bean.data);
// 刷新适配器
myAdapter.notifyDataSetChanged();
// 停止下拉刷新
xListView.stopRefresh();
SimpleDateFormat dateFormat=new SimpleDateFormat("hh:mm:ss");
String format = dateFormat.format(new Date());
xListView.setRefreshTime(format);
break;
case 2:
// 添加新的数据
list.addAll(bean.data);
// 刷新适配器
myAdapter.notifyDataSetChanged();
// 停止上拉加载
xListView.stopLoadMore();
break;
case 3:
// 第一次进入要有数据
list.addAll(bean.data);
myAdapter = new MyAdapter(list, MainActivity.this);
xListView.setAdapter(myAdapter);
break;
default:
break;
}
}
};
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xlistview);
// 开启加载更多
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
new Thread(){
public void run() {
getdata(3);
};
}.start();
// 设置监听
xListView.setXListViewListener(this);
}
// 上拉刷新
@Override
public void onRefresh() {
new Thread() {
public void run() {
getdata(1);
};
}.start();
}
// 下拉加载更多
@Override
public void onLoadMore() {
new Thread(){
public void run() {
getdata(2);
};
}.start();
}
// 请求网络数据
private void getdata(final int tag) {
if (tag == 1) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/1";
} else if (tag == 2) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/"+(i + 1);
} else if (tag == 3) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/1";
}
try {
URL url = new URL(path);
// 得到HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// 读取超时
connection.setReadTimeout(5000);
// 请求超时
connection.setConnectTimeout(5000);
// 请求方式
connection.setRequestMethod("GET");
// 连接
connection.connect();
if (200 == connection.getResponseCode()) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String s;
while ((s = reader.readLine()) != null) {
// 记录每一行数据
builder.append(s);
}
String string = builder.toString();
Message msg = Message.obtain();
msg.obj = string;
msg.arg1 = tag;
handler.sendMessage(msg);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.bwie.bean.Bean;
import com.bwie.bean.Data;
import com.bwie.xlistview.view.XListView;
import com.bwie.xlistview.view.XListView.IXListViewListener;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
public class MainActivity extends Activity implements IXListViewListener {
private XListView xListView;
int i = 1;
List<Data> list = new ArrayList<Data>();
Handler handler = new Handler() {
private MyAdapter myAdapter;
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String s = (String) msg.obj;
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
int tag = msg.arg1;
switch (tag) {
// 下拉刷新
case 1:
// 下拉刷新之前要清除数据
list.clear();
// 添加新的数据
list.addAll(bean.data);
// 刷新适配器
myAdapter.notifyDataSetChanged();
// 停止下拉刷新
xListView.stopRefresh();
SimpleDateFormat dateFormat=new SimpleDateFormat("hh:mm:ss");
String format = dateFormat.format(new Date());
xListView.setRefreshTime(format);
break;
case 2:
// 添加新的数据
list.addAll(bean.data);
// 刷新适配器
myAdapter.notifyDataSetChanged();
// 停止上拉加载
xListView.stopLoadMore();
break;
case 3:
// 第一次进入要有数据
list.addAll(bean.data);
myAdapter = new MyAdapter(list, MainActivity.this);
xListView.setAdapter(myAdapter);
break;
default:
break;
}
}
};
private String path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xlistview);
// 开启加载更多
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
new Thread(){
public void run() {
getdata(3);
};
}.start();
// 设置监听
xListView.setXListViewListener(this);
}
// 上拉刷新
@Override
public void onRefresh() {
new Thread() {
public void run() {
getdata(1);
};
}.start();
}
// 下拉加载更多
@Override
public void onLoadMore() {
new Thread(){
public void run() {
getdata(2);
};
}.start();
}
// 请求网络数据
private void getdata(final int tag) {
if (tag == 1) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/1";
} else if (tag == 2) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/"+(i + 1);
} else if (tag == 3) {
path = "http://api.expoon.com/AppNews/getNewsList/type/3/p/1";
}
try {
URL url = new URL(path);
// 得到HttpURLConnection对象
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// 读取超时
connection.setReadTimeout(5000);
// 请求超时
connection.setConnectTimeout(5000);
// 请求方式
connection.setRequestMethod("GET");
// 连接
connection.connect();
if (200 == connection.getResponseCode()) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String s;
while ((s = reader.readLine()) != null) {
// 记录每一行数据
builder.append(s);
}
String string = builder.toString();
Message msg = Message.obtain();
msg.obj = string;
msg.arg1 = tag;
handler.sendMessage(msg);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}