2016-04-03 107 views
1

你好,我想實現利用改造2.0改造2.0 POST請求

我的問題後要求就是展示我應該在onResponse寫,從而通過採取從用戶或手動輸入獲取數據。

謝謝

+0

打電話? –

+0

您可能希望將'postCards''移動到Loader對象中,因爲您的響應可能會到達較舊的活動實例,並在正在處理時導致內存泄漏。 – Machinarius

回答

0

onResponse被調用後,請求完成。你不會以這種相反的順序要求用戶的輸入(除非你正在做多個請求或鏈接它們)。所以你應該已經有用戶輸入PRIOR來完成改造請求。

所以你onResponse回調是你處理HTTP響應:

@Override 
    public void onResponse(Call<List<Card>> call, Response<List<Card>> response) { 
     processResponse(response.body()); 
    } 

但是你發送的請求之前(和接收響應),就可以將表單數據添加到您的POST要求,你可以這樣做:

@POST("/api/Cards") 
Call<List<Card>> createCards(@Body List<Card> cards, 
     // Sort the cards using a query string param 
     @Query("sort") String contractAccount), 
     // Set a group id parameter as the replacement block 
     @Path("id") int groupId); 
+0

你能解釋一下嗎?@Igor Ganapolsky – Ghost

1

爲POST方法你必須在接口來使用@Body標籤

@POST("/api/Cards") 
Call<List<Card>> createCards(@Body List<Card> cards); 

和你在哪裏調用`createCards()`從活動

Card card=new Card(); 
card.setId(20); 
card.setTitle("New Cards"); 
card.setMessage("New Launched cards"); 

List<Card> cards=new List<Card>(); 
cards.add(card); 
Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    RequestApi requestApi = retrofit.create(RequestApi.class); 
    mCardsRequest = requestApi.createCards(cards); 
    mCardsRequest.enqueue(new Callback<List<Card>>() { 
     @Override 
     public void onResponse(Call<List<Card>> call, Response<List<Card>> response) { 
      ** what should I add here to post data ** 
     } 

     @Override 
     public void onFailure(Call<List<Card>> call, Throwable t) { 
      // 
     } 
    }); 
+0

不行@sushil庫馬爾 – Ghost

+0

把Log.d(「tags」,t.toString);在onFailure方法中,並告訴我錯誤 – sushildlh