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 { 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 ); 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(); } SimpleAdapter adapter= new SimpleAdapter(MainActivity.this ,list,R.layout.server_list,from,to); lv.setAdapter(adapter); setProgressBarIndeterminateVisibility( false); } }; 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) { if(!checkNet(MainActivity. this)){ Toast. makeText(MainActivity.this, "网络连接失败,请检查网络" ,Toast.LENGTH_LONG).show(); return; } else{ new Thread(st).start(); setProgressBarIndeterminateVisibility( true); } } }); } 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>
|