2016-12-27 49 views
0

我正在尝试访问使用HTTP 200状态码返回JSON对象的REST API。它适用于Postman,但是当我从Volley请求时,每次都会给我以下错误。如何解决E/Volley:BasicNetwork.performRequest:意外的响应代码400?

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"); 
    } 

我的App类代码

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

我是我的系统浏览器。 @SathishKumarJ –

+0

但你在移动中使用Volley。可能是您的网络速度与系统相比速度较慢 –

回答

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

+0

时,你缺少POST参数,这不起作用。同样的错误 –

相关问题