2017-03-04 3 views
0

본문 매개 변수에 문자열 컬렉션과 함께 POST 요청을 보내려고합니다. 다음은 여기문자열의 컬렉션과 함께 발리를 사용하여 POST 요청을 보내는 방법

{ 
    "Emails": [ 
    "sample string 1", 
    "sample string 2" 
    ] 
} 

및 포맷팅 및 내 요청의 샘플은 내가 trying-

private void sendEmailRequest(final String email, String playId) { 
    String url = "https://someurl"; 
    Map<String, String> postParam = new HashMap<String, String>(); 
    postParam.put("Emails", "["+ email +"]"); 

    JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url,new JSONObject(postParam), 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.d(TAG, response.toString());    
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      VolleyLog.d(TAG, "Error: " + error.getMessage()); 
     } 
    }){ 
     /** 
     * Passing some request headers 
     * */ 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<String, String>(); 
      headers.put("Authorization", "Bearer " + myToken); 
      headers.put("Content-Type", "application/json; charset=utf-8"); 
      return headers; 
     } 
    }; 
    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(jsonRequest, tagStringReq); 
} 

오전 것입니다하지만 항상 오류 onErrorResponse 방법에 간다

E/Volley: [613] BasicNetwork.performRequest: Unexpected response code 500 for https://someurl 

을 로그 - 이 문제를 극복하기위한 제안을 해주십시오.

답변

0

당신의 JSON 구조의 등가 몸 요청 :

{ 
    "Emails": [ 
    "sample string 1", 
    "sample string 2" 
    ] 
} 

은 다음과 같이이다 :

try { 
    JSONObject body = new JSONObject(); 
    JSONArray array = new JSONArray(); 
    array.put("sample string 1"); 
    array.put("sample string 2"); 
    body.put("Emails", array.toString()); 

    new JsonObjectRequest(Request.Method.POST, url, body, listener, listener); 
    .... 
} catch (JSONException e) { 
    // ignores exception 
} 

참고 : 사용하는 경우 JsonObjectRequest 당신이 콘텐츠 형식을 추가 할 필요가 없습니다. 나는이 라인을 참조하고있다 :

headers.put("Content-Type", "application/json; charset=utf-8"); 
관련 문제