2016-12-27 2 views
0

HTTP 200 상태 코드가있는 JSON 객체를 반환하는 REST API에 액세스하려고합니다. 그것은 우편 배달부와 올바르게 작동하지만 발리에서 요청할 때마다 오류가 발생합니다.전자/발리를 해결하는 방법 : BasicNetwork.performRequest : 예기치 않은 응답 코드 400?

E/Volley: [3766] BasicNetwork.performRequest: Unexpected response code 400 for API 

이 내가 내 API 서비스를 보이지 않았다 로그 캣

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 

내 코드

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

입니다

이 문제를 해결하는 방법?

+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

이것이 작동하지 않습니다. 같은 오류가 발생했습니다. –

관련 문제