2017-04-16 79 views
0

使用凌空POST JSON數組中JSON對象,我想用POST方法如何在Android的

{"device_type": "SUP","commands": [ 

{ 

    "command_id": 165, 

    "arguments": [ 

    { 

     "name": "Host", 

     "value": "google.com" 

    } 

    ] 

}]} 

我嘗試了許多解決方案可通過網站,但他們大多告訴格式此JSON主體發送到服務器該字符串併發送到服務器。是否有任何正確的方法發送此主體到服務器端使用齊射。請幫幫我。謝謝。

回答

3

讓我們從底部開始。首先,

JSONObject json1= new JSONObject(); 
json1.put("name","Host"); 
json1.put("value","google.com"); 

此之後,我們把上面的對象數組

JSONArray jsonArray = new JSONArray(); 
jsonArray.put(json1); 

現在我們上面的陣列和命令ID添加到一個新的對象裏面

JSONObject json2= new JSONObject(); 
json2.put("command_id","165"); 
json2.put("arguments",jsonArray); 

再次,這是一個命令陣列對象

JSONArray jsonArray2 = new JSONArray(); 
jsonArray2.put(json2); 

現在我們把上面的陣列和設備類型INTA最後一個目的

JSONObject json3= new JSONObject(); 
json3.put("device_type","SUP"); 
json3.put("commands",jsonArray2); 

現在你可以json3對象轉換爲字符串,併發送至服務器。

json3.toString; 
+1

真棒解釋@Nirup Iyer感謝您的時間:) –

0

創建一個JSONObject的JSON =新的JSONObject(DEVICE_TYPE(你的情況))的JSON數據,你想,並張貼凌空

+0

感謝您的回覆。但如何發送數組即ie **參數**。 –

0

你可以使用這個類:

public class GsonRequest<T> extends Request<T> { 

    public static final String HEADER_CONTENT_TYPE = "Content-Type"; 
    public static final String CONTENT_TYPE_TEXT_HTML = "text/html"; 
    public static final int HTTP_STATUS_OK = 200; 

    protected final Type mTypeOfT; 
    private final Response.Listener<T> mListener; 
    protected final Gson mGson; 
    protected String mUrl = ""; 

    protected boolean mIsCached = false; 

    /** 
    * Create a new Gson Json-parser request. 
    * 
    * @param method the Http method see {@link com.android.volley.Request.Method} 
    * @param typeOfT type of generic. 
    * @param url request url. 
    * @param listener response listener. 
    * @param errorListener error listener. 
    */ 
    public GsonRequest (final int method, final Type typeOfT, final String url, 
    final Response . Listener <T> listener, 
    final Response . ErrorListener errorListener) { 
     this(method, typeOfT, url, listener, errorListener, new Gson()); 
    } 

    /** 
    * Create a new Gson Json-parser request with a custom gson instance (useful for specifying 
    * custom date formats, etc.) 
    * 
    * @param method the Http method see {@link com.android.volley.Request.Method} 
    * @param typeOfT type of generic. 
    * @param url request url. 
    * @param listener response listener. 
    * @param errorListener error listener. 
    * @param gson custom Gson instance. 
    */ 
    public GsonRequest (final int method, final Type typeOfT, final String url, 
    final Response . Listener <T> listener, 
    final Response . ErrorListener errorListener, final Gson gson) { 
     super(method, url, errorListener); 

     mListener = listener; 
     mTypeOfT = typeOfT; 
     mGson = gson; 
     mUrl = url; 
    } 



    @Override 
    protected Response <T> parseNetworkResponse (final NetworkResponse response) { 
     try { 

      String charset = HttpHeaderParser . parseCharset (response.headers); 
      Log.d("response", "1"); 
      final String responseData = new String (response.data, charset); 
      Log.d("response", "2"); 
      logResponse("Request finished with response from the server:", response, responseData); 

      if (isHtmlFacade(response)) { 

       return Response.error(new VolleyError()); 
      } 

      T responseObject; 
      if (responseData.startsWith("{\"list\":")) { 
       Log.d("response", "if"); 
       String json = responseData . substring (responseData.indexOf(":") + 1, responseData.indexOf("]")+1); 


       responseObject = mGson.fromJson(json, mTypeOfT); 
      } else { 
       Log.d("response-single", responseData); 
       responseObject = mGson.fromJson(responseData, mTypeOfT); 
      } 


      return Response.success(responseObject, HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (Exception e) { 

      return Response.error(new ParseError (e)); 
     } 
    } 


    /** 
    * @param response the response to check 
    * @return true if the response contains html according to its Content-Type and the status is 
    * 200 OK. 
    */ 
    private boolean isHtmlFacade(NetworkResponse response) { 
     Map<String, String> headers = response . headers; 
     String contentType = headers . get (HEADER_CONTENT_TYPE); 

     return response.statusCode == HTTP_STATUS_OK && contentType != null 
       && contentType.contains(CONTENT_TYPE_TEXT_HTML); 
    } 

    @Override 
    protected void deliverResponse(final T response) { 
     mListener.onResponse(response); 
    } 
} 

然後電話:

GsonRequest<Model> request = new GsonRequest<Model>(Request.Method.POST,Model.class,"http://requestUrl", onsuccessListener, onerrorListener)