码迷,mamicode.com
首页 > 编程语言 > 详细

StudentListactivity.java

时间:2017-06-23 00:39:38      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:return   student   als   support   break   trace   .com   xtend   ast   

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 = sp.getString("studata", ""); //所有信息
JSONObject jsonObject=new JSONObject(data);
String studata= 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= jsonObject.getString("data");
Gson gson=new Gson();

//studata=[{"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 onLoad() {
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

标签:return   student   als   support   break   trace   .com   xtend   ast   

原文地址:http://www.cnblogs.com/vcer/p/7067768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!