package www.wulian1.com.webview; import android.content.SharedPreferences;import android.icu.text.IDNA;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ListView;import android.widget.Toast; import com.google.gson.Gson;import com.google.gson.reflect.TypeToken; import org.json.JSONObject;import org.xml.sax.ErrorHandler; import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.URL;import java.util.List; public class PHPActivity extends AppCompatActivity { ListView listView; List<StuManage> stuList=null; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); String abc = (String) msg.obj; Toast.makeText(PHPActivity.this, "我输出的字符:" + abc, Toast.LENGTH_SHORT).show(); switch (msg.what) { case 200: //获取到数据 //List<StuManage> stuList=(List<StuManage>)msg.obj; if(stuList!=null &&stuList.size()>0) { MyAdapter myAdapter = new MyAdapter(stuList, PHPActivity.this); listView.setAdapter(myAdapter); } else { Toast.makeText(PHPActivity.this,"没有数据",Toast.LENGTH_SHORT).show(); } break; case 2: //获取失败 Toast.makeText(PHPActivity.this,"获取数据失败",Toast.LENGTH_SHORT).show(); break; case 0: //获取发生异常 Toast.makeText(PHPActivity.this,"获取数据异常",Toast.LENGTH_SHORT).show(); break; default: //其他情况 Toast.makeText(PHPActivity.this,"获取数据异常2",Toast.LENGTH_SHORT).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_php); listView=(ListView)findViewById(R.id.lv_1); SharedPreferences sp=this.getSharedPreferences("WebView",MODE_PRIVATE); String dataString=sp.getString("name",""); if (!"".equals(dataString)){ Gson gson=new Gson(); try{ JSONObject jsonObject=new JSONObject(dataString); String timestamp=jsonObject.getString("timestamp"); StuManage stuManage=gson.fromJson(dataString,StuManage.class); Long nowtimestamp=System.currentTimeMillis(); Long offday=(nowtimestamp - stuManage.getTimestamp())/(1000*60*60*24); Log.d("84","offday="+offday); if (offday<30){ //从缓存中获取数据 dataget(); Toast.makeText(PHPActivity.this,"未过期",Toast.LENGTH_SHORT).show(); Log.d("84","距离上次天数为"+offday); }else { getDataFromService(); Toast.makeText(PHPActivity.this,"已过期",Toast.LENGTH_SHORT).show(); Log.d("84","距离上次天数为"+offday); } }catch (Exception e){ e.printStackTrace(); Toast.makeText(PHPActivity.this,"获取时间异常",Toast.LENGTH_SHORT).show(); Log.d("84","获取时间异常"); } }else { //没拿到数据,重新获取数据 getDataFromCache(); } Button btn_getstring = (Button) findViewById(R.id.btn_1); Button btn_getjson = (Button) findViewById(R.id.btn_2); btn_getjson.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (getDataFromCache()) { //拿到数据,绑定数据 MyAdapter myAdapter=new MyAdapter(stuList,PHPActivity.this); listView.setAdapter(myAdapter); Log.d("84","cache"); } else { getDataFromService(); Log.d("84","Service"); } // Toast.makeText(PHPActivity.this,"准备完成",Toast.LENGTH_SHORT).show(); } }); } private void dataget() { if (getDataFromCache()){ MyAdapter myAdapter=new MyAdapter(stuList,PHPActivity.this); listView.setAdapter(myAdapter); }else { getDataFromService(); } } public String readInfoStream(InputStream input) throws Exception { if (input == null) { throw new Exception("输入流为null"); } //字节数组 byte[] bcache = new byte[2048]; int readSize = 0;//每次读取的字节长度 int totalSize = 0;//总字节长度 ByteArrayOutputStream infoStream = new ByteArrayOutputStream(); try { //一次性读取2048字节 while ((readSize = input.read(bcache)) > 0) { totalSize += readSize; //将bcache中读取的input数据写入infoStream infoStream.write(bcache, 0, readSize); } } catch (IOException e1) { throw new Exception("输入流读取异常"); } finally { try { //输入流关闭 input.close(); } catch (IOException e) { throw new Exception("输入流关闭异常"); } } try { return infoStream.toString("utf-8"); } catch (UnsupportedEncodingException e) { throw new Exception("输出异常"); } } private boolean getDataFromCache() { try { SharedPreferences sharedPreferences = PHPActivity.this.getSharedPreferences("WebView", MODE_PRIVATE); String data = sharedPreferences.getString("data", ""); //JSONObject jsonObject=new JSONObject(data); //String code=jsonObject.get("code").toString(); if (data != "") { // String datas=jsonObject.get("datas").toString(); Gson gson = new Gson(); stuList = gson.fromJson(data, new TypeToken<List<StuManage>>() { }.getType()); return true; } else { stuList = null; return false; } } catch (Exception e) { e.printStackTrace(); stuList = null; return false; } } private void getDataFromService() { final String path = "http://10.10.192.58:8081/Json/Json.php"; 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("84", "try -1"); int code = conn.getResponseCode(); if (code == 200) { Log.d("84", "code==200"); InputStream inputStream = conn.getInputStream(); String abc = readInfoStream(inputStream); Gson gson = new Gson(); stuList = gson.fromJson(abc, new TypeToken<List<StuManage>>() { }.getType()); SharedPreferences sharedPreferences = getSharedPreferences("WebView", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("studata", abc); //StuManage stuManage=new StuManage(); //stuManage.setTimestamp(System.currentTimeMillis()); //Gson gson=new Gson(); Log.d("84", "stulist 获取数据正常"); Message message = new Message(); message.obj = abc; message.what = 200; handler.sendMessage(message); } else { Message message = new Message(); message.what = 2; handler.sendMessage(message); Log.d("84", "请求失败"); } } catch (Exception e) { Message message = new Message(); message.what = 0; handler.sendMessage(message); Log.d("84", "Exception"); } } }.start(); }}
时间: 2024-10-13 14:40:02