問題描述
我使用Volley進行HTTP請求,但是發生了一些非常奇怪的事情。Android Volley HTTP獲取請求有奇怪的響應?
如果我自己檢查GET請求獲取的網頁,它是正確的JSON格式。例如
{ data: [...] }
。
然而,當排球取它,該響應是這樣的:
473858840819 { data: [...] }}
它具有位於前部的隨機數,並在最後一個額外的支架。我加倍檢查和調試,只有當Volley請求頁面時纔會發生。有任何想法嗎?
我的骯髒的修復是檢查是否有一個數字,如果是的話,刪除數字和括號...但我想更好地解決這個問題。
是因爲編碼還是什麼?
的Android凌空代碼
private void doFetch(String url)
{
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(context);
// 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) {
// The really dirty fix I want to eliminate....
if(!(response.charAt(0)+"").equals("{"))
{
// Remove number at front // Remove excess } at the end
response = response.substring(response.indexOf("{"), response.length()-1);
}
// Save to SharedPreferences cache
saveToCache(response);
// Convert to json and display to UI
convertJson(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
}
你能分享一些代碼嗎? – driftking9987
有兩種方法可以查看它。首先它可能是你的服務器返回不同的響應。其次,在你的android代碼應該是問題。粘貼一些日誌從android的請求響應,並會檢查。 – Panther
@ driftking9987我加了代碼! –