How should I handle HTTP response codes when using AsyncTask?

I am creating an Android application that takes user input, tries to connect to the API URL with the specified input and retrieve the data and display it. Now I was able to do everything above, but there is a potential chance that if the user enters something, and he does not exist, my application will fail due to NPE (Null Pointer exception).

The API that I use shows me a list of response errors that may occur, but I'm not sure how I should handle or implement a function that takes into account these response errors.

Currently, my AsyncTask extended class has the following parameters: String, Void, JSONObject , and the following code is what I have in my doInBackground method .

URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

int statusCode = urlConnection.getResponseCode();
switch (statusCode) {
    case 400:
    return "Error 400 - Bad request.";
    case 401:
    return "Error 401 - Unauthorized request.";
}

I cannot return a String because my AsyncTask parameter returns a JSONObject. I can change it so that it returns a string, but I believe that this is not the right logical way to handle response errors.

Now, if the API response code was 404 (no data found), I do not want the application to crash because it cannot return a JSONObject, instead I would like it to continue moving on to the next fragment and display the minimum information.

So, how do I handle error response handling when my method returns a JSONObject?

+4
2

( # 1 - )

1) JSON ( ). , JSON "HTTP- JSON" . ( , "" / .. JSON JSON .)

2) ( , , , onPostExecute() ( Via, # 1)

3) - asyncTask , ( , , . , , ( ), "" .)

4) , fooobar.com/questions/45396/.... 1 1 'ehh...

: LEAK MEMORY. Java , . , async , /( ) , - / /etc... , , . ( asyncTask ( )) , static WeakRefernce < > . ( Android, , Handlers ..). , , MOOC, PhD - https://www.coursera.org/course/posacommunication

"ehh" ( " " )... AsyncTask Exception, , - AsyncTask (Statics Generics Java , ++, ++ , Java 1 , "shared" .) (: " ", " " )

+1

null, statusCode 200:

            URL url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            int statusCode = urlConnection.getResponseCode();

            if(statusCode!= 200){

                return null;

            }

postExecute :

protected void onPostExecute(JSONObject result) {

    if (result == null) {

        // Inform the user something weird happened

    }
    else{

        // Do your normal condition with your JSON object
    }

: JSON ArrayList:

            ArrayList<Object>  result = new ArrayList<Object>();

            URL url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            int statusCode = urlConnection.getResponseCode();


            switch (statusCode) {

                case 200:
                   // Your code to build your JSON object
                   JSONObject yourJsonObject = ...
                   result.add("Ok");
                   result.add(yourJsonObject);
                   break;
                case 400:
                   result.add("Error 400 - Bad request.");
                   result.add(null);
                   break;
                case 401:
                   result.add("Error 401 - Unauthorized request.");
                   result.add(null);
                   break;
            }

postExecute , ArrayList:

protected void onPostExecute(ArrayList<Object> result) {

    String message = (String) result.get(0);
    JSONObject yourJsonObject = (JSONObject) result.get(1);


    if (result == yourJsonObject) {

        // Inform the user something weird happened
        // Example: Toast the message

    }
    else{

        // Do your normal condition with your JSON object

    }
0

Source: https://habr.com/ru/post/1620007/


All Articles