I'm new in Android, Java and the whole object-oriented programming, I'm trying to develop an app which needs to listen on many web services (PHP scripts written by me returning a standardized JSON string) and I thought I could design a class which takes this commitment: ServiceListener
.
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public abstract class ServiceListener implements runnable {
public static final String SERVICE_AIRPORTS = "http://example.com/app/a/services/json/airports.php";
// ...As many constants as web services
private static final int STATUS_CODE_FORMAT_ERROR = 5;
private static final int STATUS_CODE_EXECUTION_FAILED = 3;
private static final int INTERNAL_ERROR = -1;
private static final int GENERIC_ERROR = 0;
private JSONObject jsonResult, dataToSend;
private String serviceUrl;
protected ServiceListener(String serviceUrl, JSONObject dataToSend) {
this.serviceUrl = serviceUrl;
this.dataToSend = dataToSend;
new Thread(this).start();
}
protected JSONObject listenToService() throws JSONException {
String jsonString;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(serviceUrl + "?data=" + dataToSend.toString(0)).build();
try (Response response = client.newCall(request).execute()) {
jsonString = response.body().string();
jsonResult = new JSONObject(jsonString);
if(jsonResult.has("Status")) {
if(jsonResult.getString("Status").equals("Success")) {
return jsonResult;
}
else if(jsonResult.getString("Status").equals("Failed")) {
jsonResult.put("StatusCode", STATUS_CODE_EXECUTION_FAILED);
jsonResult.put("StatusMessage", "Service failed execution");
return jsonResult;
}
}
else {
jsonResult.put("StatusCode", STATUS_CODE_FORMAT_ERROR);
jsonResult.put("StatusMessage", "Service invalid format");
}
}
catch (IOException e) {
e.printStackTrace();
jsonResult.put("StatusCode", INTERNAL_ERROR);
return jsonResult;
}
jsonResult.put("Error", GENERIC_ERROR);
return jsonResult;
}
}
Then you can get the response wherever you want using this:
try {
JSONObject dataToSend = new JSONObject(/*data to send with GET e.g: UserToken, Query and so on...will be interpreted on the web service*/);
new ServiceListener(ServiceListener.SERVICE_DATE_SELECTOR, dataToSend) {
@Override public void run() {
try {
JSONObject jo = new JSONObject().put("Content", listenToService());
// Do whatever you want with the JSONObject (jo)
Log.d("joContent", jo.getJSONObject("Content").toString(0));
} catch(JSONException e) {
e.printStackTrace();
}
}
};
} catch(JSONException e) {
e.printStackTrace();
}
Is it a good and fast way to request for (my own) web services? Eager to receive criticism
dataToSend
to your service? It is not clear in your code where that data comes from. \$\endgroup\$jsonSource
becomesdataToSend
). Anyway there could be some service which doesn't need some data to interpret so I'm going to overload the constructor. \$\endgroup\$