BasicNetwork.performRequest-Android 6.0.1中出现意外响应代码400,但模拟器上未出现

时间:2019-10-12 11:14:55

标签: android android-emulator android-volley

我尝试在我的android三星下面的代码,它给出了错误。 (我想获取频道属性)

String URL = "https://www.googleapis.com/youtube/v3/search?hl=tr_TR&part=snippet&q=" + channel + "&type=channel&safeSearch=moderate&maxResults=1&key=xxxxxx";
StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, URL, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("items");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject item = jsonArray.getJSONObject(i);
                JSONObject snippet = item.getJSONObject("snippet");
                String channelId = snippet.getString("channelId");
                //getVideo(channelId);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
    //  Log.e("Error", error.getMessage());
    //  Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

);
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(stringRequest);

出现此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.cocuklaricin.junioryoutube, PID: 13820
    java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Native Method)
        at android.util.Log.e(Log.java:334)
        at com.cocuklaricin.junioryoutube.MainActivity$1$2.onErrorResponse(MainActivity.java:127)
        at com.android.volley.Request.deliverError(Request.java:617)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:104)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7225)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
I/qtaguid: Untagging socket 45
E/Volley: [4518] BasicNetwork.performRequest: Unexpected response code 400 for https://www.googleapis.com/youtube/v3/search?hl=tr_TR&part=snippet&q=Cartoon Network Türkiye&type=channel&safeSearch=moderate&maxResults=1&key=xxxx

但是当我在模拟器上尝试使用它时,效果很好。

有什么区别?我该如何解决这个错误?

预先感谢

1 个答案:

答案 0 :(得分:1)

尝试使用null之前先检查error.getMessage(),如下所示

   new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                         if(error.getMessage==null){
                             Toast.makeText(MainActivity.this, "Failed to retrieve data", Toast.LENGTH_LONG).show();
                         }
                         else{

                             Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                         }

                });

也不要忘记像error.getMessage().toString();

那样将其转换为String

更新

确保您的url不应包含任何空格,因此  IMO,您可以将channel中的输入%20中的任何空格替换为Cartoon%20Network%20Türkiye 那么此url将有效。