0
I’m trying to consume an API on Android, but I’m not getting it. Debugging looks like it cannot exit Asynctask and does not return the value to where it is calling the method. It can read the data without any problem and assign true or false to the variable, but does not return. Follow 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;
}
}
I created a separate method for Asynctask and it worked, but I need different connections to be made, as parameter step for it according to my need?
– Bruno Inácio