Error consuming API on Android

0

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;
}

}

    
asked by anonymous 04.08.2018 / 03:45

1 answer

0

Create a separate class, and pass the parameters through the constructor if it is more than one. For example:

Call

AsyncTaskEntrada entradaTask = new AsyncTaskEntrada("http://192.168.1.11/ws/entradaSetor.php?numberOF=", numberOF);
entradaTask.execute();

AsyncTask class

public class AsyncTaskEntry extends android.os.AsyncTask {

private URL url;
private String numberOf;
private Boolean resultEntradaSetor;

public AsyncTaskEntrada(URL url, String numberOf) {
    this.url = url;
    this.numberOf = numberOf;
}

@Override
protected Boolean doInBackground(Void... voids) {
    HttpURLConnection myConnection = null;
    try {
        myConnection = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        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 = null;
            try {
                jsonArray = new JSONArray(responseStrBuilder.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            if (jsonArray.length() == 0)
                resultEntradaSetor = false;
            else
                resultEntradaSetor = true;

            myConnection.disconnect();
            return null;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return resultEntradaSetor;
}

}

    
10.08.2018 / 20:02