Issue
I have a function inside a aar module that needs to wait the onResponse()
or onError()
response by using AndroidNetworking
. The output is the return
executes first before waiting on whatever response it will received. I'm having a problem on waiting the response to return on my app. I also used synchronized
on the function. this is the code on my module
public static synchronized String getActivationData(final Context context, final String api, final String base64Header, final String endpoint, final JSONObject body) {
final String[] result = {null};
new Thread(new Runnable() {
@Override
public void run() {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String baseURL = null;
if (api.equalsIgnoreCase("LOCAL")) {
baseURL = Environments.getMarkLocalAPI();
} else if (api.equalsIgnoreCase("DEVELOPMENT")) {
baseURL = Environments.getDevelopmentAPI();
} else if (api.equalsIgnoreCase("STAGING")) {
baseURL = Environments.getStagingAPI();
} else if (api.equalsIgnoreCase("DEPLOYMENT")) {
baseURL = Environments.getDeploymentAPI();
}
final String finalBaseURL = baseURL;
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.build();
AndroidNetworking.initialize(context, okHttpClient);
AndroidNetworking.post(finalBaseURL + endpoint)
.setPriority(Priority.HIGH)
.addJSONObjectBody(body)
.addHeaders("Project", base64Header)
.addHeaders("Content-Type", "application/json")
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
result[0] = String.valueOf(response);
}
@Override
public void onError(ANError anError) {
if (anError.getErrorCode() != 0) {
result[0] = String.valueOf(anError.getErrorDetail());
} else {
result[0] = String.valueOf(anError.getErrorDetail());
}
}
});
Log.i("data", result[0]);
}
}).start();
return result[0];
}
and calling the function on my App via :
String data = ActivationResponseV2.getActivationData(getContext(), "LOCAL", header, "/sample/response", jsonObject);
may I know what I'm doing wrong?. Thanks!
Solution
I have done it by using ANRequest
. After reading this document. I tried to replicate it and I get my desired output.
This is the code :
public static synchronized String getActivationData(final Context context, final String api, final String base64Header, final String endpoint, final JSONObject body) {
final String[] result = {null};
String baseURL = null;
if (api.equalsIgnoreCase("LOCAL")) {
baseURL = Environments.getMarkLocalAPI();
} else if (api.equalsIgnoreCase("DEVELOPMENT")) {
baseURL = Environments.getDevelopmentAPI();
} else if (api.equalsIgnoreCase("STAGING")) {
baseURL = Environments.getStagingAPI();
} else if (api.equalsIgnoreCase("DEPLOYMENT")) {
baseURL = Environments.getDeploymentAPI();
}
final String finalBaseURL = baseURL;
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.build();
AndroidNetworking.initialize(context, okHttpClient);
ANRequest request = AndroidNetworking.post(finalBaseURL + endpoint)
.setPriority(Priority.HIGH)
.addJSONObjectBody(body)
.addHeaders("Project", base64Header)
.addHeaders("Content-Type", "application/json")
.build();
ANResponse response = request.executeForJSONObject();
if (response.isSuccess()) {
result[0] = String.valueOf(response.getResult());
Log.d(TAG, "response : " + result[0]);
} else {
ANError error = response.getError();
Log.d(TAG, "response : " + error);
}
return result[0];
}
}
Happy Coding!
Answered By - FroyoDevourer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.