1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package com.cszhi.mymonitor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
//jsonstring 字符串,用于存放通过url获取的json数据
String jsonstring=null ;
//接口地址
String url= "http://60.191.231.17/get.php" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//用于等待图标
requestWindowFeature(Window. FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout. activity_main);
Button bn=(Button) findViewById(R.id. bn);
//处理线程传递来的消息
final Handler myhandler =new Handler(){
public void handleMessage(Message msg){
ListView lv=(ListView) findViewById(R.id. lv);
String[] from={"id","hostname" ,"ip" ,"rootpartion" ,"uptime" ,"time" };
int[] to={R.id.id,R.id.hostname ,R.id.ip,R.id.rootpartion,R.id. uptime,R.id.time };
List<map <string,?>> listb= new ArrayList</map><map <string,?>>();
Map<string ,String> b= new HashMap</string><string , String>();
b.put( "id", "id" );
b.put( "hostname", "hostname" );
b.put( "ip", "ip" );
b.put( "rootpartion","rootpartion" );
b.put( "uptime","uptime" );
b.put( "time", "time" );
listb.add(b);
ListView lvtitle=(ListView) findViewById(R.id.lvTitle );
SimpleAdapter adapterb= new SimpleAdapter(MainActivity.this ,listb,R.layout.server_list,from,to);
lvtitle.setAdapter(adapterb);
List<map <string,?>> list= new ArrayList</map><map <string,?>>();
try{
JSONArray jsonArray= new JSONArray(jsonstring );
//解析json对象,并放入hashmap中
for(int i=0;i<jsonarray .length();i++){
JSONObject item=jsonArray.getJSONObject(i);
Map<string,String> m=new HashMap<string , String>();
m.put( "id", item.getString("id" ));
m.put( "hostname", item.getString("hostname" ));
m.put( "ip", item.getString("ip" ));
m.put( "rootpartion",item.getString("rootpartion" ));
m.put( "uptime", item.getString("uptime" ));
m.put( "time", item.getString("time" ));
list.add(m);
}
} catch(Exception e){
e.printStackTrace();
}
//这里的this是Handler,因为这个方法是在放在Handler里的,所以这里不应该使用this,而应该使用MainActivity.this(MainActivity是类名)
SimpleAdapter adapter= new SimpleAdapter(MainActivity.this ,list,R.layout.server_list,from,to);
lv.setAdapter(adapter);
//关闭等待图标(右上角的转圈)
setProgressBarIndeterminateVisibility( false);
}
};
//Runnable接口
final Runnable st=new Runnable() {
@Override
public void run() {
data( url);
myhandler.sendEmptyMessage(0);
}
};
//检查是否有网络连接
if(!checkNet( this)){
Toast. makeText(this, "网络连接失败,请检查网络" ,Toast.LENGTH_LONG).show();
} else{
new Thread(st).start();
setProgressBarIndeterminateVisibility( true);
}
//单击按钮
bn.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//如果有网路连接,创建新线程,传入 st接口,并启动该线程
if(!checkNet(MainActivity. this)){
Toast. makeText(MainActivity.this, "网络连接失败,请检查网络" ,Toast.LENGTH_LONG).show();
return;
} else{
new Thread(st).start();
//显示等待图标(右上角的转圈)
setProgressBarIndeterminateVisibility( true);
}
}
});
}
//获取url数据,并赋值给jsonstring
private void data(String url){
MyHttp myHttp= new MyHttp();
jsonstring= myHttp.httpGet(url);
}
//检查是否有网络的函数
public static boolean checkNet(Context context) {
try {
NetworkInfo networkInfo = (( ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE )).getActiveNetworkInfo();
if (networkInfo != null) {
Log. d("httpjson", "true");
return true ;
} else {
Log. d("httpjson", "false");
return false ;
}
} catch (Exception e) {
e.printStackTrace();
return false ;
}
}
}
</string></jsonarray></map></string></map>
|