我正在尝试在我的Android应用中从我的服务器检索响应,但是获得" com.android.volley.ServerError " E / Volley:[224669] BasicNetwork.performRequest:http://52.74.115.250:8080/api/products的意外响应代码400
当我在Postman尝试时,我确实得到了回复。
这是我的GET请求代码:
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, Constants.productListUrlStr, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("jsonsucces","success");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("jsonerror",error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Constants.getToken(getActivity()));
//headers=globalProvider.addHeaderToken(getActivity());
return headers;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
if (contract!=null){
if(contract._supplier!=null){
Log.d("checksupplier",contract._supplier);
params.put("_supplier",contract._supplier);
}
}
if(!globalProvider.ShangpingHeaderLoadCategory .equals("")){
params.put("category",globalProvider.ShangpingHeaderLoadCategory);
Log.d("checkcategoryhe",globalProvider.ShangpingHeaderLoadCategory);
}
return params;
}
};
globalProvider.addRequest(jsonObjectRequest);
我做错了什么?
答案 0 :(得分:0)
关于getParams()
凌空请求JsonObjectRequest
不再有效的帖子很少。
或者您可以使用StringRequest
或在请求中进行一些调整,如下所示
JSONObject jOb = new JSONObject();
try {
jOb.put("_supplier", contract._supplier);
jOb.put("category", globalProvider.ShangpingHeaderLoadCategory);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Constants.productListUrlStr, jOb, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("jsonsucces","success");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("jsonerror",error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Constants.getToken(getActivity()));
//headers=globalProvider.addHeaderToken(getActivity());
return headers;
}
};
答案 1 :(得分:0)
我想通了,因为我传递了查询参数,我正在构建错误的网址。 String uri = String.format(Constants.productListUrlStr +&#34;?_ supplier =%1 $ s&#34;,contract._supplier);
StringRequest stringRequest=new StringRequest(Request.Method.GET, uri, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("checksuc",response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("checkstrerr",error.networkResponse.headers.toString());
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Constants.getToken(getActivity()));
//headers=globalProvider.addHeaderToken(getActivity());
return headers;
}
}
;
globalProvider.addRequest(stringRequest);