2017-09-20 70 views
-1

,我已經使用例如谷歌「https://developer.android.com/training/volley/simple.html#simple排球誤差新請求隊列

   RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 
      String url ="http://www.google.com"; 

      // Request a string response from the provided URL. 
      StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
        new Response.Listener<String>() { 
         @Override 
         public void onResponse(String response) { 
          // Display the first 500 characters of the response string. 
          message.setText("Response is: "+ response.substring(0,500)); 
         } 
        }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        message.setText("That didn't work!"); 
       } 
      }); 
       // Add the request to the RequestQueue. 
        queue.add(stringRequest); 

我得到的下面的錯誤。

E /排球:[77336] NetworkDispatcher.run:未處理的異常顯示java.lang.NullPointerException:嘗試從場到讀取空對象引用 的java.lang '詮釋java.lang.String.offset'。 NullPointerException:嘗試從java.lang.String.com處的java.lang.String.compareToIgnoreCase(String.java:560) 上的null對象引用 的字段'int java.lang.String.offset'中讀取java.lang.String $ CaseInsensitiveComparator.compare( String.java:77) at java.lang.String $ CaseInsensitiveComparator.compare(String.java:66) at java.util.TreeMap.find(TreeMap.java:277) 在java.util.TreeMap.putInternal(TreeMap.java:240) at java.util.TreeMap.put(TreeMap.java:186) at com.android.volley.NetworkResponse.toHeaderMap(NetworkResponse.java:138) at com.android.volley.NetworkResponse。(NetworkResponse.java:0) at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:169) at com.android.volley.NetworkDispatcher.run(NetworkDispatcher。 Java的:113)

+0

你能告訴我哪條線有錯誤(Logcat指向線)嗎? –

+0

其實我不知道你指的是什麼意思,這就是我應用這個請求時出現的錯誤。 –

回答

1

了很多工作,我後,我找到了解決辦法,我用的是從GitHub而是採用了凌空庫中的應用程序文件的依賴關係「編譯‘com.android.volley:1.0.0:凌空’」 。希望有人能解釋一下爲什麼我不能使用Github的庫。感謝所有的幫助。

1

代碼說你遇到了NullPointer exception

這意味着您在此代碼中使用的變量在 運行時爲空。

檢查您的傳遞值不爲null。

this

+0

我沒有使用包含數據的變量,我試圖測試這個簡單的請求! –

0

請從響應爲刪除子: -

message.setText("Response is: "+ response.substring(0,500)); 

變更後:

if(response.lemght>500){ 
     message.setText("Response is: "+ response.substring(0,500)); 
    }else{ 

     message.setText("Response is: "+ response); 
    } 

試試這個 在創建請求隊列,請通過上下文不是getApplicationContext(): -

RequestQueue queue = Volley.newRequestQueue(this);//if you are in activity, if you are in fragment then **getActivity()** 
+0

應用更改後仍然有相同的錯誤。 –

+0

有時getApplicationContext可能返回null,因此嘗試將其更改爲this或getActivity() –

0

在進行網絡調用時,您可能會成功,但有時候不會有數據。所以請確保您在訪問數據之前進行空檢查。

if(response!=null){ 
    message.setText("Response is: "+ response.substring(0,500)); 
} 
+0

仍然一樣,它看起來不是來自「響應」的錯誤。 –

+0

雅這就是我說的。我解決了你的空指針問題..但你沒有得到響應數據,你正試圖設置它。這就是爲什麼應用程序崩潰 – Anonymous

+0

你正在打「http://www.google.com」和期望字符串響應?如何bro..try命中不同的API,它給你字符串響應或JSON響應和try.Or如果可能的話檢查翻新library.Its相同排球,但是新的 – Anonymous