2017-08-04 198 views
0

我如何向我的凌空請求主體這一請求, 在此先感謝如何發送嵌套JSON對象的請求體內

{ 
    "amount": "000", 
    "card": { 
    "number": "00000", 
    "expiry_month": "00", 
    "expiry_year": "00", 
    "cvv": "000", 
    "pin": "000" 
    } 
} 

這是我的請求參數,我有嘗試這個API是告訴我無效指標的影響,請guzs幫我

@Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      Map<String, String> headers = new HashMap<String, String>(); 
      headers.put("Authorization", User_Token); 
      headers.put("Content-Type", "application/json"); 
      return headers; 
     } 

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



     @Override 
     public byte[] getBody() { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("amount", amount); 
      params.put("card", String.valueOf(come())); 

      return new JSONObject(params).toString().getBytes(); 
     } 
     private byte[] come() { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("number", number); 
      params.put("expiry_month", month); 
      params.put("expiry_year", year); 
      params.put("cvv", cvv); 
      params.put("pin", pin); 
      return new JSONObject(params).toString().getBytes(); 
     } 
+0

指定哪些API給你一個錯誤?這是服務器響應還是什麼?還指定一個錯誤本身 – j2ko

+0

服務器響應,錯誤是無效的參數 –

+0

響應只是意味着api不能理解我的請求,我想爲我的代碼更好的格式請參閱 –

回答

0

使用JsonObject代替:創建Map,將其轉換爲Byte[],然後獲取其String.valueOf(...)。 這將允許您發送完整的對象作爲請求的身體,而不是正在被現在發送的不完整的身體:{"amount":"000","card":"[[email protected]"}

與正在爲"card"發送的值的問題:"[[email protected]"是,它是不是您的對象與其屬性的表示。相反,它只是您創建的字節數組的內存地址。

在你的代碼,使用JsonObject爲對象,只有做轉換到Byte[]getBody()方法的結束,因爲這是你終於回到你的完整對象的地方:

@Override 
public Map<String, String> getHeaders() throws AuthFailureError { 
    Map<String, String> headers = new HashMap<String, String>(); 
    headers.put("Authorization", User_Token); 
    // headers.put("Content-Type", "application/json"); // This is probably redundant, as you are already setting it on getBodyContentType() 
    return headers; 
} 

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

@Override 
public byte[] getBody() { 
    JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject.put("amount", amount); 
     jsonObject.put("card", come()); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return jsonObject.toString().getBytes(); 
} 

private JSONObject come() throws JSONException { 
    JSONObject params = new JSONObject(); 
    params.put("number", number); 
    params.put("expiry_month", month); 
    params.put("expiry_year", year); 
    params.put("cvv", cvv); 
    params.put("pin", pin); 
    return params; 
} 
+0

它返回相同的錯誤,請問 –

+0

您是否嘗試過請求到您的服務使用郵遞員,捲曲或其他工具? – spuente

+0

我使用的是postmn,它在郵遞員上完美工作,另一個guz寫web完成。這是鏈接https //:api.myflex。 ng /錢包/基金 –

0

如果您需要確切的格式,您需要確保numbermonth等的值作爲字符串傳遞,但不是整數。 此外,爲了使代碼看起來更短,更具破壞性,您可以連接put調用。這是一個有點重構版本:

@Override 
public byte[] getBody() { 
    try { 
     final JSONObject card = new JSONObject() 
       .put("number", String.valueOf(number)) 
       .put("expiry_month", String.valueOf(month)) 
       .put("expiry_year", String.valueOf(year)) 
       .put("cvv", String.valueOf(cvv)) 
       .put("pin", String.valueOf(pin)); 

     return new JSONObject() 
       .put("amount", String.valueOf(amount)) 
       .put("card", card) 
       .toString() 
       .getBytes();  
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    //Empty json return will not help us as in this case 
    //we will send useless empty body to the server. 
    return null; 
} 

我不知道的getBody()主叫側發生什麼事,你是否可以修改代碼。但你需要以某種方式跟蹤null返回,或者引入你自己的異常類,應該在getBody()調用方檢查。但如果你有超過你需要恢復一些非null,空返getBody()發送方無法控制:

return new byte[0]; 
+0

它給出了同樣的錯誤「無效參數」請幫我出 –

+0

我提供的代碼應該生成與您請求的結構相同的JSON對象。如果服務器以「無效參數」響應,我們如何幫助您,而不知道服務器期望什麼? – j2ko

+0

這是api鏈接https //:api.myflex.ng/wallet/fund –