我正在尝试访问返回带有HTTP 200状态代码的JSON对象的REST API。它与Postman一起正常工作,但是当我从Volley请求时,它每次都会给我低于误差。
E/Volley: [3766] BasicNetwork.performRequest: Unexpected response code 400 for API
这是我的代码
private void getListData() {
showDialog();
String url = "http://www.**********.com";
HashMap<String, String> requestParams = new HashMap<String, String>();
requestParams.put("SecurityKey",
Preferences.getSecurityKey(AppointmentsListActivity.this));
requestParams.put("ToDate", endDate);
requestParams.put("FromDate", startDate);
Log.i(TAG, "JSON : " + new JSONObject(requestParams));
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, new JSONObject(requestParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
closeDialog();
Log.i(TAG, "onResponse : " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
closeDialog();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
// headers.put("Content-Type", "application/json; charset=utf-8"); also use this but didn't work
headers.put("Content-Type", "application/json");
return headers;
}
};
App.getInstance().addToRequestQueue(jsonObjReq, "get_data");
}
我的应用类代码
public class App extends Application {
public static final String TAG = App.class
.getSimpleName();
private RequestQueue mRequestQueue;
private static App mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized App getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
logcat的
E/Volley: [3946] BasicNetwork.performRequest: Unexpected response code 400 for http://www.********.com
E/com.android.myappointments.ui.activity.AppointmentsListActivity: Error: null
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb8653b40
我没有显示我的Api服务
如何解决此问题?
答案 0 :(得分:0)
删除标题
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
// headers.put("Content-Type", "application/json; charset=utf-8"); also use this but didn't work
headers.put("Content-Type", "application/json");
return headers;
}
};
在应用程序类中添加重试策略:
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
req.setRetryPolicy(new DefaultRetryPolicy(30000, 1, 1));
getRequestQueue().add(req);
}
请在此处查看示例代码 - VolleyExample