2017-08-28 43 views
1

這下面的代碼是調用webservice的,我想實現與Retrofit在Android,但我得到這個錯誤:實現用於改造自定義標題和我沒有得到任何發現改造註解,錯誤

No Retrofit annotation found 

調用Web ServiceCURL

curl -H "X-Auth-Token: 9HqLlyZOugoStsXCUfD_0YdwnNnunAJF8V47U3QHXSq" \ 
    -H "X-User-Id: aobEdbYhXfu5hkeqG" \ 
    http://localhost:3000/api/v1/channels.list 

我寫這個接口像上面的代碼:

import retrofit2.Call; 
import retrofit2.http.GET; 
import retrofit2.http.Header; 
public interface RocketRestfulService { 
    @GET("/api/v1/channels.list") 
    Call<List<ChannelsList>> getChannelsList(
      @Header("X-Auth-Token") String AuthToken, 
      @Header("X-User-Id") String UserId, 
      ChannelsList channelsList); 
} 

,我這段代碼調用此休息Web Service

ChannelsList channelsList = new ChannelsList(); 
Call<List<ChannelsList>> call = rocketRestfulService.getChannelsList(
     "HNv1VtMiyUky2RkXWUydyj4f2bfciQ6DzVQgKULSwfe", 
     "Wz9ex2N2z9zzJWdzD", 
     channelsList); 
call.enqueue(new Callback<List<ChannelsList>>() { 
    @Override 
    public void onResponse(Call<List<ChannelsList>> call, final Response<List<ChannelsList>> response) { 
     Log.e("contentLength ", response.code() + ""); 
    } 
    @Override 
    public void onFailure(Call<List<ChannelsList>> call, Throwable t) { 
     t.printStackTrace(); 
    } 
}); 

什麼我的代碼,我不能打電話,我得到錯誤的問題?

回答

0

您忘記了@Body註釋。由於您必須發送正文,因此您必須爲您的API創建一個POST。如果不是POST,則必須找到如何發送ChannelsList以成爲GET請求,因爲這取決於您的服務器實現。

@POST("/api/v1/channels.list") 
Call<List<ChannelsList>> getChannelsList(
     @Header("X-Auth-Token") String AuthToken, 
     @Header("X-User-Id") String UserId, 
     @Body ChannelsList channelsList); 
+0

我得到這個錯誤'非身體HTTP方法不能包含@身體現在 –

+0

@ Mahdi.Pishguy我更新了我的答案。 –

+0

我試圖在Android上實現這個鏈接,爲這個Web服務改進'https:// rocket.chat/docs/developer-guides/rest-api/channels/list'和'HTTP Method'是'GET'我在Web瀏覽器上,有些其他客戶端沒有任何問題 –

0

問題通過此以下代碼解決:

接口:

public interface RocketRestfulService { 
    @GET("/api/v1/channels.list") 
    Call<ResponseBody> getChannelsList(
      @Header("X-Auth-Token") String AuthToken, 
      @Header("X-User-Id") String UserId); 
} 

呼叫請求,並獲得響應:

ChannelsList channelsList = new ChannelsList(); 
Call<ResponseBody> call = rocketRestfulService.getChannelsList(
     "HNv1VtMiyUky2RkXWUydyj4f2bfciQ6DzVQgKULSwfe", 
     "Wz9ex2N2z9zzJWdzD"); 
call.enqueue(new Callback<ResponseBody>() { 
    @Override 
    public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) { 
     if (response.isSuccessful()) { 
      try { 
       String  jsonString = response.body().string(); 
       JSONObject jsonObject = new JSONObject(jsonString); 
       JSONArray jsonarray = jsonObject.getJSONArray("channels"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    @Override 
    public void onFailure(Call<ResponseBody> call, Throwable t) { 
     Log.e("Err: ", t.getMessage()); 
    } 
}); 
相關問題