标签:photo cte flat aos ken rac etc ati cal
1.Stu
public class Stu {
private String name;
private 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;
}
}
2.StuAdapter
public class StuAdapter extends BaseAdapter {
List stuList;
Activity activity;
public StuAdapter(List stuList,Activity activity){
this.stuList=stuList;
this.activity=activity;
}
@Override
public int getCount() {
return stuList.size();
}
@Override
public Object getItem(int position) {
return stuList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=activity.getLayoutInflater().inflate(R.layout.listview_item,null);
TextView textView_name=(TextView)view.findViewById(R.id.name);
TextView textView_age=(TextView)view.findViewById(R.id.age);
textView_name.setText(stuList.get(position).getName());
textView_age.setText(stuList.get(position).getAge()+"");
return view; }
3.MainActivity
public class MainActivity extends AppCompatActivity {
List stuList=null;
ListView listView;
public Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 200:
if (stuList!=null){
StuAdapter stuAdapter=new StuAdapter(stuList,MainActivity.this);
listView.setAdapter(stuAdapter);
}
break;
case -1:
Toast.makeText(MainActivity.this,"获取失败",Toast.LENGTH_SHORT).show();
break;
case -2:
Toast.makeText(MainActivity.this,"获取异常",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(MainActivity.this,"我也不知道为啥",Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listview);
getDateFromSeriver();
}
private void getDateFromSeriver() {
final String path="";
new Thread(){
@Override
public void run() {
super.run();
try {
URL url=new URL(path);
HttpURLConnection coon=(HttpURLConnection)url.openConnection();
coon.setRequestMethod("GET");
coon.setConnectTimeout(5000);
int code=coon.getResponseCode();
if (code==200){
InputStream inputStream=coon.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();
stuList=gson.fromJson("studata",new TypeToken>(){}.getType());
Message msg=new Message();
msg.what=200;
msg.obj=abc;
handler.sendMessage(msg);
}else {}
}else {
Message msg=new Message();
msg.what=-1;
handler.sendMessage(msg);
}
}catch (Exception e){
Message msg=new Message();
msg.what=-2;
handler.sendMessage(msg);
e.printStackTrace();
}
}
}.start();
}
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 "获取失败";
}
}
}
4.布局文件
(1)mainactivity
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="com.example.believer.gougoude.MainActivity">
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
(2)listview_item.xml
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="姓名"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/age"
android:text="年龄"/>
.Stu
StuAdapterMianActivity
2.布局文件
stu
标签:photo cte flat aos ken rac etc ati cal
原文地址:http://www.cnblogs.com/zongze/p/7092592.html