Skip to content

Commit ae99ec8

Browse files
committed
JP | Global httpResponse is removed. Async httpRequest method with return response is done. Code refactoring is done.
1 parent 39dcbe0 commit ae99ec8

File tree

3 files changed

+56
-50
lines changed

3 files changed

+56
-50
lines changed

app/src/main/java/net/simplifiedcoding/sqlitedbcode/ApiCall.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import okhttp3.Response;
1010

1111
public class ApiCall {
12-
//GET network request
12+
1313
public static String GET(OkHttpClient client, HttpUrl url) throws IOException {
1414
Request request = new Request.Builder()
1515
.url(url)
@@ -18,7 +18,6 @@ public static String GET(OkHttpClient client, HttpUrl url) throws IOException {
1818
return response.body().string();
1919
}
2020

21-
//POST network request
2221
public static String POST(OkHttpClient client, HttpUrl url, RequestBody body) throws IOException {
2322
Request request = new Request.Builder()
2423
.url(url)

app/src/main/java/net/simplifiedcoding/sqlitedbcode/ServerCustomAdapter.java

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.util.ArrayList;
5+
import java.util.concurrent.TimeUnit;
56

67
import android.app.Activity;
78
import 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
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.1.3'
8+
classpath 'com.android.tools.build:gradle:2.2.0'
99
// NOTE: Do not place your application dependencies here; they belong
1010
// in the individual module build.gradle files
1111
}

0 commit comments

Comments
 (0)