Error while consuming API on Android

Asked

Viewed 25 times

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?

1 answer

0

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

Calling for

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

Asynctask class

public class Asynctaskentrada 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;
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.