首页 > 代码库 > StudentListactivity.java
StudentListactivity.java
package www.wulian1.com.webview;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import www.wulian1.com.webview.MyViews.AutoListView;
public class StudentListactivity extends AppCompatActivity implements AutoListView.OnLoadListener,AutoListView.OnRefreshListener {
List<StuManage> stuList=null;
int pageindex=1;
AutoListView autoListView;
MyAdapter myAdapter;
//定义一个在主线程和子线程直接进行通信的一个**东西 handler
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case 200:
//完成数据绑定
if(stuList!=null)
{
//绑定数据
if(myAdapter==null)
{
myAdapter=new MyAdapter(stuList,StudentListactivity.this);
autoListView.setAdapter(myAdapter);
Log.d("zh","autoListView.setAdapter 第一绑定 ");
}
else
{
myAdapter.setList(stuList);
myAdapter.notifyDataSetChanged();
Log.d("zh","加载更多后, notifyDataSetChanged ");
}
}
else
{
Toast.makeText(StudentListactivity.this,"没有数据",Toast.LENGTH_SHORT).show();
}
autoListView.onRefreshComplete();
autoListView.onLoadComplete();
break;
case -1:
//获取失败
Toast.makeText(StudentListactivity.this,"获取失败",Toast.LENGTH_SHORT).show();
break;
case -2:
//获取发生异常
Toast.makeText(StudentListactivity.this,"获取发生异常",Toast.LENGTH_SHORT).show();
break;
default:
//其他情况
Toast.makeText(StudentListactivity.this,"获取发生异常",Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_studentlist);
autoListView=(AutoListView) findViewById(R.id.lv_stulist);
autoListView.setOnRefreshListener(this);
autoListView.setOnLoadListener(this);
//从缓存获取数据
// if(getDataFromCache())
// {
// //绑定数据
// StuAdapter stuAdapter=new StuAdapter(stulist,StudentListactivity.this);
// autoListView.setAdapter(stuAdapter);
// }
// else
// {
//从服务器获取数据
getDataFromService(1);
//}
}
private String inputstream2string(InputStream inputStream)
{
try {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int length=0;
byte[] buffer=new byte[1024];
while((length=inputStream.read(buffer))!=-1){
baos.write(buffer, 0, length);
}
inputStream.close();
baos.close();
//或者用这种方法
//byte[] result=baos.toByteArray();
//return new String(result);
return baos.toString();
} catch (Exception e) {
e.printStackTrace();
return "获取失败";
}
}
///返回true 说明成功从cache拿到数据 返回false 说明没有拿到
private boolean getDataFromCache()
{
try {
SharedPreferences sp = StudentListactivity.this.getSharedPreferences("person", MODE_PRIVATE);
String data = http://www.mamicode.com/sp.getString("studata", ""); //所有信息
JSONObject jsonObject=new JSONObject(data);
String studata= http://www.mamicode.com/jsonObject.getString("data");
//是否过期
if (data != "") {
Gson gson = new Gson();
stuList = gson.fromJson(data, new TypeToken<List<StuManage>>() {
}.getType());
//暂时没有时间过期的判断
return true;
} else {
stuList=null;
return false;
}
}catch (Exception ex)
{
ex.printStackTrace();
stuList=null;
return false;
}
}
private void getDataFromService(int pindex )
{
final String path="http://10.10.192.58:83/Json/Stu.php?pageindex="+pindex;
Log.d("zh","path= "+path);
// 开线程
new Thread(){
@Override
public void run() {
super.run();
//获取数据
try {
URL url = new URL(path);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET"); //必须大写
conn.setConnectTimeout(5000);
Log.d("zh","try -1 ");
int code=conn.getResponseCode();
if(code==200)
{
Log.d("zh","code==200 ");
//请求成功
InputStream inputStream=conn.getInputStream();
//然后把输入流转为字符串 。上网找一个把流转为字符串的函数
String abc=inputstream2string(inputStream);
//解析json字符串
//先拿到code 。 判断code是否等于200
// {"code":200,"pageindex":10,"isover":1,"data":[{"name":"\u5f20\u5b8f90","age":91},{"name":"\u5f20\u5b8f91","age":92},{"name":"\u5f20\u5b8f92","age":93},{"name":"\u5f20\u5b8f93","age":94},{"name":"\u5f20\u5b8f94","age":95},{"name":"\u5f20\u5b8f95","age":96},{"name":"\u5f20\u5b8f96","age":97},{"name":"\u5f20\u5b8f97","age":98},{"name":"\u5f20\u5b8f98","age":99},{"name":"\u5f20\u5b8f99","age":100}]}
JSONObject jsonObject=new JSONObject(abc);
if(jsonObject.getString("code").equals("200"))
{
//如果等于200。 服务器数据有效的
String studata= http://www.mamicode.com/jsonObject.getString("data");
Gson gson=new Gson();
//studata=http://www.mamicode.com/[{"name":"\u5f20\u5b8f90","age":91},。。。。]
List<StuManage> stulist_temp = gson.fromJson(studata, new TypeToken<List<StuManage>>(){}.getType());
// 程序如果走到这个位置 。说明数据符合json格式。这时候我要把数据使用sharedpreference保存起来
//android 内存缓存
SharedPreferences sp=getSharedPreferences("person",MODE_PRIVATE); //person是文件名.
SharedPreferences.Editor editor=sp.edit();
editor.putString("studata",abc);//按照关键字 studata。 把字符串保存起来
//修改当前yema
pageindex= Integer.parseInt(jsonObject.getString("pageindex"));
Log.d("zh","获取的为第"+pageindex+"页的数据");
if(pageindex>1)
{
stuList.addAll(stulist_temp);
Log.d("zh","合并数据");
}
else
{
stuList= new ArrayList<StuManage>();
stuList=stulist_temp;
Log.d("zh","新数据");
}
//把数据存储到
Log.d("zh","stulist 获取数据正常");
Message message=new Message();
message.obj=abc; //定义带有数据的消息
message.what=200; //200代码获取数据正常
handler.sendMessage(message);
Log.d("zh","发送消息正常");
}
else
{
//返回码不是200的情况
Message message=new Message();
message.obj=abc; //定义带有数据的消息
message.what=202; //200代码获取数据正常
handler.sendMessage(message);
Log.d("zh","发送消息正常");
}
}
else
{
//请求失败
Message message=new Message();
message.what=-1; //-1 代码获取数据失败
handler.sendMessage(message);
Log.d("zh","请求失败");
}
}
catch (Exception ex)
{
Message message=new Message();
message.what=-2; //-1 代码获取数据时发生异常情况
//发送消息
handler.sendMessage(message);
Log.d("zh","Exception");
}
}
}.start();
}
@Override
public void onl oad() {
int pageindex_temp= pageindex+1;
getDataFromService(pageindex_temp);
Log.d("zh","onLoad----加载更多");
}
@Override
public void onRefresh() {
pageindex=1;
getDataFromService(pageindex);
Log.d("zh","onRefresh----下拉刷新");
}
}
StudentListactivity.java
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。