无法理解为什么是onErrorResponse,即“出了点问题”

时间:2019-06-12 13:37:55

标签: java android

我想对onResponse回答,但没有得到。我需要如何实现这一目标的指导。

这是我的代码:

public class Network {

    Context context;
    String pokemondata;
    CallbackInterface callbackInterface;

    public Network(Context context,CallbackInterface callbackInterface) {
        this.context = context;
        this.callbackInterface=callbackInterface;
    }

    public void test(){


        String URL = "http://pokeapi.co/api/v2/pokemon/";
        RequestQueue requestQueue = Volley.newRequestQueue(context);

        JsonObjectRequest jsonObjectRequest= new JsonObjectRequest(Request.Method.GET, URL, null,new Response.Listener<JSONObject>(){
            @Override
            public void onResponse(JSONObject response) {
                Log.d("onResponse", String.valueOf(response));

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context,"Something went wrong",Toast.LENGTH_SHORT).show();
                callbackInterface.data("failure");
                Log.d("fail","failure");

            }
        });
        requestQueue.add(jsonObjectRequest);
   }

}

失败-显示com.example.tablayout D / Data的OnErrorResponse:我的数据+失败

onResponse数据为“ http://pokeapi.co/api/v2/pokemon/

1 个答案:

答案 0 :(得分:0)

我使用了String url =“ https://pokeapi.co/api/v2/pokemon/”;而不是String URL =“ http://pokeapi.co/api/v2/pokemon/”; 现在可以了