我必須使用排球庫向網址發出POST請求。我已經建立了下面的自定義類凌空: - CustomJSONObjectRequestAndroid排除請求無法正常工作
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* Created by user on 4/14/2016.
*/
public class CustomJSONObjectRequest extends JsonObjectRequest {
Map<String, String> mParams;
public CustomJSONObjectRequest(int method, String url, JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return mParams;
}
@Override
public RetryPolicy getRetryPolicy() {
// here you can write a custom retry policy
return super.getRetryPolicy();
}
}
CustomVolleyRequestQueue
import android.content.Context;
import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
/**
* Created by Devastrix on 4/14/2016.
*/
public class CustomVolleyRequestQueue {
private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private CustomVolleyRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
}
public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleyRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
// Don't forget to start the volley request queue
mRequestQueue.start();
}
return mRequestQueue;
}
}
我使用下面的函數使POST請求: -
RequestQueue mQueue = CustomVolleyRequestQueue.getInstance(context)
.getRequestQueue();
String url = "someURL";
Log.d("URL= ", url);
final CustomJSONObjectRequest jsonRequest = new CustomJSONObjectRequest(Request.Method.POST, url,
new JSONObject(), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("post: ", response+"");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String,String> params = new HashMap<String, String>();
params.put("A", "[[\"123\",\"456\",\"789\"]]"); // it is a json string actually
params.put("B", "23-11-2016");
params.put("C", "ABC");
return params;
//return query;
}
};
jsonRequest.setTag(TAG);
int socketTimeout = TIMEOUT;//30 seconds -
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsonRequest.setRetryPolicy(policy);
mQueue.add(jsonRequest);
但問題是請求正在返回空數據,而當我使用某些第三方應用程序進行POST請求時,它會返回正確的結果。什麼可能是可能的錯誤?
編輯 - 實際上,響應不爲空。真實的迴應是這樣的。
{
"data" : ""
}
但數據的關鍵不應該有空洞value.SO我認爲這是與PARAMS或PARAMS沒有得到正確發送任何問題。但參數是完全正確的,我檢查過。
您希望服務器響應成功的(2xx)POST而返回哪些數據?另外,該數據應該以何種格式存在? JSON? – clownba0t
數據爲JSON格式。關鍵'數據'應該包含一些JSONObject,但是響應顯示'數據'鍵具有空鍵。 – devastrix
我明白了。我能想到的唯一的事情是服務器真的沒有爲'data'屬性返回任何東西。嘗試在服務器端添加日誌記錄,以便您可以在構建響應時準確查看服務器打包到「數據」中的內容。或者,在應用程序方面,截取原始響應,然後將其中的數據轉換爲「JSONObject」,並檢查其內容。你應該可以通過覆蓋'protected Response parseNetworkResponse(NetworkResponse response){'並使用調試器或日誌記錄來做到這一點。 –
clownba0t