我熟悉Volley並創建一個Singleton類,並將請求添加到隊列中。但是,我希望增加排列的模塊性,並簡單地通過方法調用將所有請求調用到另一個類。我已經安裝了本動作作爲基礎用於公共GET請求:無法賦值給最終的Volley變量響應
public Object getRequest(String params) {
final JSONObject getRequestReturn = new JSONObject();
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET,
VolleySingleton.prefixURL, ((String) null),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Parse the JSON:
try {
getRequestReturn = response;
Log.v("GET Request value", response.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("GET Request Error", error.toString());
}
});
mRequestQueue.add(getRequest);
return getRequestReturn;
}
然而,我在有響應的分配一個錯綜複雜的鎖釦22錯誤:
getRequestReturn = response;
錯誤注意到getRequestReturn
必須聲明爲final才能在內部類中使用,但在分配final
時,會出現另一個錯誤,指出您無法將值分配給最終變量。
該方法如何處理?
聲明getRequest返回到課程級別 –
不要使用像那樣的回報,你不會得到任何東西,請看看我的答案在下面的問題http://stackoverflow.com/questions/32627089/post-request -json-file-passing-string-and-wait-for-the-response-volley/32627293#32627293 – BNK
另一個http://stackoverflow.com/questions/32375295/android-how-to-return-async-jsonobject -from-method-using-volley :) – BNK