I'm trying to consume an API on Android, but I'm not getting it. Debugging looks like it can not get out of AsyncTask and does not return the value where it is calling the method. It can read the data without any problem and assign true or false to the variable, but it does not return. Here is the code:
private Boolean resultEntradaSetor;
public Boolean entradaSetor(final String numberOF){
AsyncTask.execute(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://192.168.1.11/ws/entradaSetor.php?numberOF="+numberOF);
HttpURLConnection myConnection =
(HttpURLConnection) url.openConnection();
if (myConnection.getResponseCode() == 200) {
InputStream responseBody = myConnection.getInputStream();
InputStreamReader responseBodyReader =
new InputStreamReader(responseBody, "UTF-8");
BufferedReader streamReader = new BufferedReader(new InputStreamReader(responseBody, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
JSONArray jsonArray = new JSONArray(responseStrBuilder.toString());
if(jsonArray.length() == 0)
resultEntradaSetor = false;
else
resultEntradaSetor = true;
myConnection.disconnect();
} else {
// Error handling code goes here
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
return resultEntradaSetor;
}
}