22
33import java .io .IOException ;
44import java .util .ArrayList ;
5+ import java .util .concurrent .TimeUnit ;
56
67import android .app .Activity ;
78import android .content .Context ;
@@ -24,8 +25,10 @@ public class ServerCustomAdapter extends ArrayAdapter<Server> {
2425 int layoutResourceId ;
2526
2627 private SQLiteDatabase db ;
27- private String httpResponse = null ;
28- private OkHttpClient client = new OkHttpClient ();
28+ private OkHttpClient client = new OkHttpClient ()
29+ .newBuilder ()
30+ .connectTimeout (500 , TimeUnit .MILLISECONDS )
31+ .build ();
2932
3033 ArrayList <Server > data = new ArrayList <Server >();
3134
@@ -68,31 +71,10 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
6871 openDatabase ();
6972 String ip = server .getIp ();
7073 int port = Integer .parseInt (server .getPort ());
71- if (isChecked ) {
72- loadContent ("http" , ip , port , "on" );
73- if (httpResponse != null && !httpResponse .isEmpty ()) {
74- String sql = "UPDATE servers SET status=" + 1 + " WHERE ip='" +
75- server .getIp () + "' AND port='" + server .getPort () + "';" ;
76- db .execSQL (sql );
77- showMessage ("Power is ON at " + ip +":" +port );
78- }
79- else {
80- showMessage ("Server is not responding or you are offline." );
81- }
82- }
83- else {
84- loadContent ("http" , ip , port , "off" );
85- if (httpResponse != null && !httpResponse .isEmpty ()) {
86- String sql = "UPDATE servers SET status=" + 0 + " WHERE ip='" +
87- server .getIp () + "' AND port='" + server .getPort () + "';" ;
88- db .execSQL (sql );
89- showMessage ("Power is OFF at " + ip +":" +port );
90- }
91- else {
92- showMessage ("Server is not responding or you are offline." );
93- }
94- }
95- httpResponse = null ;
74+ if (isChecked )
75+ updateSwitchStatus (ip , port , "on" );
76+ else
77+ updateSwitchStatus (ip , port , "off" );
9678 db .close ();
9779 }
9880 });
@@ -111,35 +93,60 @@ static class ServerHolder {
11193 Switch powerSwitch ;
11294 }
11395
96+ private void updateSwitchStatus (String ip , int port ,String status ){
97+ String httpResponse = loadContent ("http" , ip , port , status );
98+ if (httpResponse != null && !httpResponse .isEmpty ()) {
99+ int newStatus = status .equals ("on" ) ? 1 : 0 ;
100+ String sql = "UPDATE servers SET status=" + newStatus + " WHERE ip='" +
101+ ip + "' AND port='" + port + "';" ;
102+ db .execSQL (sql );
103+ showMessage ("Power is ON at " + ip +":" +port );
104+ }
105+ else {
106+ showMessage ("Server is not responding or you are offline." );
107+ }
108+ }
109+
114110 private String getSwitchStatus (String ip , int port ){
115- loadContent ("http" , ip , port , "status" );
116- try { Thread .sleep (3000 ); }
117- catch (InterruptedException e ) { e .printStackTrace (); }
111+ String httpResponse = loadContent ("http" , ip , port , "status" );
112+ String status = "" ;
118113 if (httpResponse != null && !httpResponse .isEmpty ()) {
119- int startPosition = httpResponse .indexOf ("<html>" ) + "<html>" .length ();
120- int endPosition = httpResponse .indexOf ("</html>" , startPosition );
121- String status = httpResponse .substring (startPosition , endPosition );
122- return status .substring (status .length () -1 );
114+ status = getStatusFromResponse (httpResponse );
123115 }
124- return "" ;
116+ return status ;
117+ }
118+
119+ private String getStatusFromResponse (String httpResponse ){
120+ int startPosition = httpResponse .indexOf ("<html>" ) + "<html>" .length ();
121+ int endPosition = httpResponse .indexOf ("</html>" , startPosition );
122+ String status = httpResponse .substring (startPosition , endPosition );
123+ return status .substring (status .length () -1 );
125124 }
126125
127126 private void showMessage (String message ){
128127 Toast .makeText (context , message , Toast .LENGTH_SHORT ).show ();
129128 }
130129
131- private void loadContent (final String requestType , final String ip , final int port , final String path ) {
132- new AsyncTask <Void , Void , Void >() {
133- @ Override
134- protected Void doInBackground (Void ... params ) {
135- try {
136- HttpUrl httpUrl = RequestBuilder .buildURL (requestType , ip , port , path );
137- httpResponse = ApiCall .GET (client , httpUrl );
138- } catch (IOException e ) {
139- e .printStackTrace ();
130+ private String loadContent (final String requestType , final String ip , final int port , final String path ) {
131+ String response = "" ;
132+ try {
133+ response = new AsyncTask <String , Integer , String >() {
134+
135+ @ Override
136+ protected String doInBackground (String ... params ) {
137+ String httpResponse = "" ;
138+ try {
139+ HttpUrl httpUrl = RequestBuilder .buildURL (requestType , ip , port , path );
140+ httpResponse = ApiCall .GET (client , httpUrl );
141+ } catch (IOException e ) {
142+ e .printStackTrace ();
143+ }
144+ return httpResponse ;
140145 }
141- return null ;
142- }
143- }.execute ();
146+ }.execute ().get ();
147+ } catch (Exception e ) {
148+ e .printStackTrace ();
149+ }
150+ return response ;
144151 }
145152}
0 commit comments