Volley never returns VolleyError or explains why there is an exception

前提是你 提交于 2019-12-07 15:48:26

The answer is that any Volley function you extend or override must have

    @Override
protected void deliverResponse(T response) {
    // TODO Auto-generated method stub
    mListener.onResponse(response);
}

function implemented. The Listener must be initialized in the constructor and have the onResponse method implemented.

Otherwise your network call will never return in the onResponse section.

EDIT: and your extended Request class has to also implement deliverError, along with deliverResponse

private final Listener<T> mListener;
private ErrorListener mErrorListener;

@Override
 public void deliverError(VolleyError error) {
    mErrorListener.onErrorResponse(error);
}

with ErrorListener initialized in your constructor

I have had the same problem this morning. Probably you are not receiving a Json as response, your server is answering with a 200 OK but Volley is waiting for receiving a Json. Than, when it does not receive a Json it generates that error, which has not a HTTP code (because it is not), but is a volley internal error.

You may solve the problem by using the right object (from the volley library) for performing your requests.

If you use a JsonObjectRequest volley needs a JSONObject in the request's body and is aspecting for a JSONObject in the response object. If you use a JsonArrayRequest volley needs a JSONArray in the request and a JSONArray in the response. For each different case you need to extend the JsonRequest class for managing the response.

For JSON why don't you use own class for answer, for example:

import com.google.gson.annotations.SerializedName;

public class UserData {
@SerializedName("email")
public String userMail;
@SerializedName("number")
public String number;
@SerializedName("loginName")
public String loginName;
@SerializedName("password")
public String password;

}

and then

public Request<?> getInformation(String loginName, String password, Response.Listener<UserData> responseListener,
                                         Response.ErrorListener errorListener) {

    String url = apiURL;

    Map<String, String> authHeaders = getAuthHeaders(loginName, password);
    authHeaders.put(HEADER_PARAM_INTERFACE_KEY, DPAG_INTERFACE_KEY);
    int method = Request.Method.GET;

    GsonRequest<UserData> request = new GsonRequest<UserData>(
            method,
            url,
            UserData.class,
            authHeaders,
            responseListener,
            errorListener,
            gson);


    return mQueue.add(request);
}

plus here the onResponse and on ErrorResponse

    Application.get().getApi().getInformation(loginName, password,
            new Response.Listener<UserData>() {

                @Override
                public void onResponse(UserData data) {

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {


                }
            }
    );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!