• Resolved darlio

    (@darlio)


    Hello again,
    In my way on using this GREAT plugin for integration on Mobile App (Android), I’m using Volley.

    I’m making some tests and understanding the way the plugin/API works with PostMan and coding in Android Studio (Java) using Volley library (best practice).

    At the moment I’m interested in getting the body answer of POST requests, with correct and incorrect credentials.

    ACTUAL FACTS (seen on POSTMAN) :
    1. The answer from server is 200 OK only if all credentials are valid. Body is JSON with all we need (details can be configured by plugin = AMAZING !)
    2. In all other cases the answer from server is 400 Bad Request - The request cannot be fulfilled due to bad syntax. . Body is JSON with error statement etc…

    INTEGRATIONS in VOLLEY :
    On most libraries concerning HTTP requests, such as Volley…
    1. Server answers are seen as SUCCESS / FAILURE based on the returned code of server : 2xx = SUCCESS, 4xx = FAILURE.
    2. On failure, the body are not even passed as an answer.

    PROBLEM :
    On wrong credentials, thus 4xx, thus FAILURE… the body is not even passed to the listener, and all we can get is the 4xx code from server… not the JSON body giving the “failure” from Plugin/API.

    SUGGESTION :
    Would it be possible to always have a SUCCESS (2xx) if the request was correctly syntaxed… which it is… and only get a 4xx for real 4xx reasons ?
    In this case, with wrong credentials, we could still parse the JSON to identify the correct failure of not getting a token.

    WORKAROUND :
    It is probably possible to get the body from a failed answer 4xx from the server through Volley since PostMan can do it ?? ! I’ll go in this direction for the moment…

    Best regards, and thank you again for this MASTERPIECE of plugin… and such GREAT SUPPORT !!!!!!!

    • This topic was modified 4 years, 7 months ago by darlio.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter darlio

    (@darlio)

    Hello again…
    For those who need it, here is the workaround to be implemented in the request of Volley.
    Found the answer here on StackOverflow, here :
    https://stackoverflow.com/questions/26796965/android-volley-basicnetwork-performrequest-unexpected-response-code-400

    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // As of f605da3 the following should work
            NetworkResponse response = error.networkResponse;
            if (error instanceof ServerError && response != null) {
                try {
                    String res = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers, "utf-8"));
                    // Now you can use any deserializer to make sense of data
                    JSONObject obj = new JSONObject(res);
                } catch (UnsupportedEncodingException e1) {
                    // Couldn't properly decode data to string
                    e1.printStackTrace();
                } catch (JSONException e2) {
                    // returned data is not JSONObject?
                    e2.printStackTrace();
                }
            }
            Log.e("HttpClient", "error: " + error.toString());
        }
    })
    Thread Starter darlio

    (@darlio)

    Hello, any thoughts about my suggestion ?

    Thread Starter darlio

    (@darlio)

    Hello Nicu,

    You are right !

    2xx is ONLY if all went well on the request.

    4xx is for ERRORs.

    In these links are some more precise information :
    Wiki
    REST API

    My suggestion is just WRONG practice, no need to even go in that direction.

    Your suggestion of building up a parsing method of the 4xx answer is exactly what I have already done. As I am developing?in Java, it is in that language, adapted to Android development with Android Studio.

    Thank you again for your great support and professional?advice !

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Mobile APP – Android Volley’ is closed to new replies.