2
2
3
3
import java .io .IOException ;
4
4
import java .util .ArrayList ;
5
+ import java .util .concurrent .TimeUnit ;
5
6
6
7
import android .app .Activity ;
7
8
import android .content .Context ;
@@ -24,8 +25,10 @@ public class ServerCustomAdapter extends ArrayAdapter<Server> {
24
25
int layoutResourceId ;
25
26
26
27
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 ();
29
32
30
33
ArrayList <Server > data = new ArrayList <Server >();
31
34
@@ -68,31 +71,10 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
68
71
openDatabase ();
69
72
String ip = server .getIp ();
70
73
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" );
96
78
db .close ();
97
79
}
98
80
});
@@ -111,35 +93,60 @@ static class ServerHolder {
111
93
Switch powerSwitch ;
112
94
}
113
95
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
+
114
110
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 = "" ;
118
113
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 );
123
115
}
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 );
125
124
}
126
125
127
126
private void showMessage (String message ){
128
127
Toast .makeText (context , message , Toast .LENGTH_SHORT ).show ();
129
128
}
130
129
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 ;
140
145
}
141
- return null ;
142
- }
143
- }.execute ();
146
+ }.execute ().get ();
147
+ } catch (Exception e ) {
148
+ e .printStackTrace ();
149
+ }
150
+ return response ;
144
151
}
145
152
}
0 commit comments