MIike Eps
MIike Eps

Reputation: 441

com.android.volley.ClientError error when using Volley in a fragment

I am having difficulty consuming an API in my android project using volley

This API returns and works perfectly when called using postman postman1

postman2

Please advise me on how to consume an API from an android fragment

See the below error. I get

com.android.volley.ClientError

see my code below

private void volleyApiCall(final String theEmail, final String theToken) {

// creating a new variable for our request queue
RequestQueue queue = Volley.newRequestQueue(getActivity());

// on below line we are calling a string
// request method to post the data to our API
// in this we are calling a post method.
StringRequest request = new StringRequest(Request.Method.POST, URL, new com.android.volley.Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // inside on response method we are
        // hiding our progress bar
        // and setting data to edit text as empty


        // on below line we are displaying a success toast message.
        Log.d(TAG, "onResponse: INSIDE RESPONSE");
        Toast.makeText(getActivity(), "Data added to API", Toast.LENGTH_SHORT).show();
        try {
            // on below line we are passing our response
            // to json object to extract data from it.
            JSONObject jsonObject = new JSONObject(response);

            Log.d(TAG, "onResponse: >>  SUCCESS SUCCESS");
            Log.d(TAG, "onResponse: jsonObject >> " + jsonObject);
            String status = (jsonObject.getString("status"));
            Log.d(TAG, "onResponse: status >> " + status);

            String output = (jsonObject.getString("output"));
            Log.d(TAG, "onResponse: output >> " + output);

            String message = (jsonObject.getString("message"));
            Log.d(TAG, "onResponse: message >> " + message);


            if (status.equals("1")) {
                String balance = fetchBalance(email, token);
                tvBalance.setText(balance);
            } else {
                Toast.makeText(getActivity(),
                        "Unable to check wallet. Please Try again", Toast.LENGTH_LONG).show();

            }

            // on below line we are setting this string s to our text view.
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}, new com.android.volley.Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        // method to handle errors.
        Toast.makeText(getActivity(), "Failed to get response = " + error, Toast.LENGTH_SHORT).show();
        Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getMessage());
        Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getStackTrace());
        Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error.getLocalizedMessage());
        Log.d(TAG, "onErrorResponse: ERROR IS : >> "+ error);
    }
}) {
    @Override
    protected Map<String, String> getParams() {
        // below line we are creating a map for
        // storing our values in key and value pair.
        Map<String, String> params = new HashMap<String, String>();
        params.put("email", theEmail);
        params.put("token", theToken);

        // at last we are
        // returning our params.
        return params;
    }
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("Content-Type", "application/json");
        params.put("Accept", "application/json");
        return params;
    }
};
request.setRetryPolicy(new RetryPolicy() {
    @Override
    public int getCurrentTimeout() {
        return 30000;
    }

    @Override
    public int getCurrentRetryCount() {
        return 30000;
    }

    @Override
    public void retry(VolleyError error) throws VolleyError {

    }
});
// below line is to make
// a json object request.
queue.add(request);
}

-------EDITS ----- I added screenshot of the postman

Upvotes: 0

Views: 1726

Answers (1)

MIike Eps
MIike Eps

Reputation: 441

Thanks, guys, the issue was the response code passed by my backend engineersOnce it was changed to 201 volley started picking up responses

Upvotes: 1

Related Questions