package aaaaa.yangxinhong20170911;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import aaaaa.yangxinhong2017091101.DataDataBean;
import aaaaa.yangxinhong2017091102.Madapter;
import view.XListView;
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private int NUM=10;
private XListView xListView;
private DataDataBean dataDataBean;//ben类
private List<DataDataBean.DataBean> list = new ArrayList<>();//数组
private Madapter madapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xListView);//做出xlistview
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
xListView.setXListViewListener(this);
gerData();
}
private void gerData() {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... voids) {//得到借口
String path="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1";
try {
URL url = new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");//yogaget
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode==200){
InputStream inputStream = connection.getInputStream();
String json=streamToString(inputStream,"utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String json) {//解析
super.onPostExecute(json);
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
list.addAll(dataDataBean.getData());
xListView.stopLoadMore();
setAdapter();
}
};
asyncTask.execute();
}
private void setAdapter() {//适配器
if(madapter==null){
madapter = new Madapter(MainActivity.this, list);
xListView.setAdapter(madapter);
xListView.stopLoadMore();
}else{
xListView.deferNotifyDataSetChanged();
}
}
private String streamToString(InputStream inputStream, String s) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, s);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder builder = new StringBuilder();
String t=null;
while ((t=bufferedReader.readLine())!=null){
builder.append(t);
}
bufferedReader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void onRefresh() {//德道网络
NUM--;
if (NUM >0){
refreshData();
}else {
Toast.makeText(MainActivity.this,"没有最新数据了",Toast.LENGTH_SHORT).show();
xListView.stopRefresh();
}
}
private void refreshData() {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void,Void,String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream, "utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String json) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
list.addAll(0,dataDataBean.getData());
setAdapter();
xListView.stopRefresh();
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String time = simpleDateFormat.format(date);
xListView.setRefreshTime(time);
}
};
asyncTask.execute();
}
public void onLoadMore() {
NUM ++;
gerData();
}
}
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import aaaaa.yangxinhong2017091101.DataDataBean;
import aaaaa.yangxinhong2017091102.Madapter;
import view.XListView;
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private int NUM=10;
private XListView xListView;
private DataDataBean dataDataBean;//ben类
private List<DataDataBean.DataBean> list = new ArrayList<>();//数组
private Madapter madapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xListView = (XListView) findViewById(R.id.xListView);//做出xlistview
xListView.setPullLoadEnable(true);
xListView.setPullRefreshEnable(true);
xListView.setXListViewListener(this);
gerData();
}
private void gerData() {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... voids) {//得到借口
String path="http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1";
try {
URL url = new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");//yogaget
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode==200){
InputStream inputStream = connection.getInputStream();
String json=streamToString(inputStream,"utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String json) {//解析
super.onPostExecute(json);
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
list.addAll(dataDataBean.getData());
xListView.stopLoadMore();
setAdapter();
}
};
asyncTask.execute();
}
private void setAdapter() {//适配器
if(madapter==null){
madapter = new Madapter(MainActivity.this, list);
xListView.setAdapter(madapter);
xListView.stopLoadMore();
}else{
xListView.deferNotifyDataSetChanged();
}
}
private String streamToString(InputStream inputStream, String s) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(inputStream, s);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder builder = new StringBuilder();
String t=null;
while ((t=bufferedReader.readLine())!=null){
builder.append(t);
}
bufferedReader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void onRefresh() {//德道网络
NUM--;
if (NUM >0){
refreshData();
}else {
Toast.makeText(MainActivity.this,"没有最新数据了",Toast.LENGTH_SHORT).show();
xListView.stopRefresh();
}
}
private void refreshData() {
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void,Void,String>() {
@Override
protected String doInBackground(Void... voids) {
String path = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=1";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream, "utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String json) {
Gson gson = new Gson();
DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);
list.addAll(0,dataDataBean.getData());
setAdapter();
xListView.stopRefresh();
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
String time = simpleDateFormat.format(date);
xListView.setRefreshTime(time);
}
};
asyncTask.execute();
}
public void onLoadMore() {
NUM ++;
gerData();
}
}