2016-07-29 40 views
0

我想發一個服務器POST請求,發送一個JSON負載,並接收一個字符串響應作爲回報。我已到處尋找,似乎沒有一種簡單的方法來使用Volley庫來實現此功能,而無需實施自定義請求(我在嘗試實現時完全失去了它)。什麼是正確的方式來執行這個POST請求?Volley是否有JSON參數,字符串響應本機支持

回答

1
final JSONObject body = new JSONObject(); 
    try { 
     // Todo: populate JSON body 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://...", 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      } 
    ) { 
     @Override 
     public byte[] getBody() throws AuthFailureError { 
      return body.toString().getBytes(); 
     } 

     @Override 
     public String getBodyContentType() { 
      return "application/json"; 
     } 
    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
+0

完美 - 感謝您的幫助。我知道那裏肯定有一個優雅的解決方案...... –

0

排球有JsonObjectRequest和JsonArrayRequest。

/** 
    * Making json object request 
    * */ 
    public void makeJsonObjReq(String url) { 

     JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
       url, 
       new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         Log.d(TAG, response.toString()); 
//      msgResponse.setText(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("Content-Type", "application/json"); 
       return headers; 
      } 

      @Override 
      protected Map<String, String> getParams() { 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("name", "[email protected]"); 
       return params; 
      } 

     }; 

     // Adding request to request queue 
     ApplicationController.getInstance().addToRequestQueue(jsonObjReq, 
       tag_json_obj); 

     // Cancelling request 
     // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj); 
    } 



/** 
    * Making json array request 
    * */ 
    public void makeJsonArryReq(String url, final int _type) { 

     JsonArrayRequest req = new JsonArrayRequest(url, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 
         apiTaskListener.onTaskComplete("error",_type); 


        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       apiTaskListener.onTaskComplete("error",_type); 

      } 
     }); 

     // Adding request to request queue 
     ApplicationController.getInstance().addToRequestQueue(req, 
       tag_json_arry); 

     // Cancelling request 
     // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_arry); 
    } 
+0

絕對,但我不希望「恢復」 JSON對象,我想「後」一個JSON對象,並在返回時,服務器就會給我一個迴應被解釋爲一個字符串。我是JSONObject的開始,服務器有一個字符串,我們將交換。 –

+0

您可以使用 StringRequest strReq = new StringRequest 因爲json只是一種字符串格式。 所以你可以發送你的json作爲字符串。 protected Map getParams()throws AuthFailureError Map params = new HashMap (); params.put(「data」,json.toString()); return params; } – ViramP