package www.wulian15.com.websitetest;
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.List;
public class Phpactivity extends AppCompatActivity {
List<Stu> stulist=null;
ListView listView;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case 200:
if(stulist!=null)
{
StuAdapter stuAdapter=new StuAdapter(stulist,Phpactivity.this);
listView.setAdapter(stuAdapter);
}
break;
case -1:
Toast.makeText(Phpactivity.this,"获取失败",Toast.LENGTH_SHORT).show();
break;
case -2:
Toast.makeText(Phpactivity.this,"获取发生异常",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(Phpactivity.this,"获取发生异常",Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phpctivity);
listView=(ListView)findViewById(R.id.lv_stus);
if(getDataFromCache())
{
StuAdapter stuAdapter=new StuAdapter(stulist,Phpactivity.this);
listView.setAdapter(stuAdapter);
}
else
{
getDataFromService();
}
}
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();
return baos.toString();
} catch (Exception e) {
e.printStackTrace();
return "获取失败";
}
}
private boolean getDataFromCache()
{
try {
SharedPreferences sp = Phpactivity.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<Stu>>() {
}.getType());
return true;
} else {
stulist=null;
return false;
}
}catch (Exception ex)
{
ex.printStackTrace();
stulist=null;
return false;
}
}
private void getDataFromService()
{
final String path="http://192.168.1.108/index.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("zh","try -1 ");
int code=conn.getResponseCode();
if(code==200)
{
Log.d("zh","code==200 ");
InputStream inputStream=conn.getInputStream();
String abc=inputstream2string(inputStream);
JSONObject jsonObject=new JSONObject(abc);
if(jsonObject.getString("code").equals("200"))
{
String studata= jsonObject.getString("data");
Gson gson=new Gson();
List<Stu> stulist = gson.fromJson(studata, new TypeToken<List<Stu>>(){}.getType());
SharedPreferences sp=getSharedPreferences("person",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("studata",abc);
Log.d("zh","stulist 获取数据正常");
Message message=new Message();
message.obj=abc;
message.what=200;
handler.sendMessage(message);
Log.d("zh","发送消息正常");
}
else
{
Message message=new Message();
message.obj=abc;
message.what=202;
handler.sendMessage(message);
Log.d("zh","发送消息正常");
}
}
else
{
Message message=new Message();
message.what=-1;
handler.sendMessage(message);
Log.d("zh","请求失败");
}
}
catch (Exception ex)
{
Message message=new Message();
message.what=-2;
handler.sendMessage(message);
Log.d("zh","Exception");
}
}
}.start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Phpactivity">
<ListView
android:id="@+id/lv_stus"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
package www.wulian15.com.websitetest;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2017/5/15.
*/
public class StuAdapter extends BaseAdapter {
public List<Stu> list;
public Activity activity;
public StuAdapter(List<Stu> list,Activity activity)
{
this.list=list;
this.activity=activity;
}
public void setList(List<Stu> list)
{
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=View.inflate(activity,R.layout.item_listview,null);
TextView textView_name=(TextView) view.findViewById(R.id.tv_name);
TextView textView_age=(TextView) view.findViewById(R.id.tv_age);
textView_age.setText(list.get(position).getAge()+"");
textView_name.setText(list.get(position).getName());
return view;
}
}
package www.wulian15.com.websitetest;
/**
* Created by Administrator on 2017/5/15.
*/
public class Stu {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>