![](/img/trans.png)
[英]How give json parameters in Volley String Request (Android Studio)
[英]Volley String Request does'nt give Response in android
我對Post方法使用了Volley String請求。但是它沒有給出正確的響應。當我使用JsonObject請求時,它將給我正確的響應。我很困惑,我不明白字符串請求有什么問題。請任何人都可以幫助我請求字符串...這是我的代碼。
StringRequest requestQueue =Volley.newRequestQueue(MainActivity.this);
String URL ="http://demoangularaims.azurewebsites.net/api/v1/API_NI_ACD_FILL_COMMON_DDL_SP";
Log.d(TAG, " url=" + URL);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, " response=" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, " error=" + error);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
LinkedHashMap<String, String> linkmap = new LinkedHashMap<>();
linkmap.put("p_ENTITY_ID", "2");
linkmap.put("p_ORGCD", "p01");
linkmap.put("p_COMPCD", "A0002");
linkmap.put("p_DIVCD", "");
linkmap.put("p_USERID", "");
linkmap.put("p_ACDYR", "");
linkmap.put("p_TYPE", "ACDYR_DDL");
linkmap.put("p_FILTER1", "");
linkmap.put("p_FILTER2", "");
linkmap.put("p_DEFUNCT", "");
Log.d(TAG, " MAP=" + linkmap);
return linkmap;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
LinkedHashMap<String, String> headers = new LinkedHashMap<>();
return headers;
}
};
requestQueue.add(stringRequest);
}
PostMan OutPut-
使用getBody代替getParams它將起作用。 由於您使用的是POST方法,因此應添加Request正文。
@Override
public byte[] getBody() throws AuthFailureError {
try {
return stringRequestBody.getBytes("utf-8"); //String Request Body with Encoded
} catch (UnsupportedEncodingException uee) {
return null;
}
}
嘗試這個
JsonObjectRequest
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
String URL = "http://demoangularaims.azurewebsites.net/api/v1/API_NI_ACD_FILL_COMMON_DDL_SP";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", "JSONObj response=" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("TAG", "JSONObj Error: " + error.getMessage());
//Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
}
});
requestQueue.add(jsonObjReq);
Jsonobject請求輸出
D/TAG: JSONObj response={"recordsets":[],"output":{},"rowsAffected":[],"returnValue":0}
字符串請求
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
String URL = "http://demoangularaims.azurewebsites.net/api/v1/API_NI_ACD_FILL_COMMON_DDL_SP";
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("TAG", "String response=" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("TAG", "String error=" + error);
}
});
requestQueue.add(stringRequest);
字符串請求輸出
D/TAG: String response={"recordsets":[],"output":{},"rowsAffected":[],"returnValue":0}
郵遞員輸出
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.