Phpactivity

package text.com.myapplication;

import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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 java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import static android.R.id.list;


public class Phpacttivity extends AppCompatActivity {
List<Stu> stuList=null;

ListView listView;

private Handler handler=new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case 200:
if (stuList!=null){
StuAdapter stuAdapter=new StuAdapter(stuList,Phpacttivity.this);
listView.setAdapter(stuAdapter);
}

break;
case -1:

break;
case -2:

break;

default:

}
String abc=(String) msg.obj;
Toast.makeText(Phpacttivity.this,"我获取的字符串为:"+abc,Toast.LENGTH_SHORT).show();
}


};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phpacttivity);
ListView listView=(ListView)findViewById(R.id.ls);


if(getDataFromCache())
{
StuAdapter stuAdapter=new StuAdapter(stuList,Phpacttivity.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 sharedPreferences=getSharedPreferences("person",MODE_PRIVATE);
String data=sharedPreferences.getString("data","");
if (data!=""){
Gson gson=new Gson();
List<Stu> stuList= (List<Stu>) gson.fromJson(data,new TypeToken<List<Stu>>(){}.getRawType());

return true;
} else {
stuList=null;
return false;
}
}catch (Exception ex){
ex.printStackTrace();
stuList=null;
return false;
}

}private boolean getDataFromService(){

final String 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);

int code=conn.getResponseCode();
if(code==200)
{


InputStream inputStream=conn.getInputStream();

String abc=inputstream2string(inputStream);
JSONObject jsonObject=new JSONObject(abc);
if (jsonObject.getString("code")=="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);


Message message=new Message();
message.obj=abc;
message.what=200;
handler.sendMessage(message);
Log.d("zh","发送消息正常");
}else {

}

}
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();

return false;
}

};

posted @ 2017-06-23 15:09  12306.1  阅读(314)  评论(0)    收藏  举报