2016-07-05 52 views
4

如何使這個查詢,我會在下面提及?方法@GET。查詢應該是這樣的:使用json進行GET查詢 - Retrofit 2.0

/top40?data={"ranking":"world"} /top40?data={"ranking":"country"}

@GET("/api/top40") 
    Call<FamousTop40Model> getStatus(
     // what should be there? 
    ); 

    class Factory { 
     private static FamousTop40Api service; 

     public static FamousTop40Api getInstance() { 
      Retrofit retrofit = new Retrofit.Builder() 
        .baseUrl(ApiConstants.BASE_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 

      service = retrofit.create(FamousTop40Api.class); 

      return service; 
     } 
    } 

你們能幫助我嗎?

編輯:+我應該有accessKey在標題中。

+0

你想要獲取或放置請求? – Drv

+0

獲取請求,但在接收到的API中,據說我應該發送json來接收數據。甚至可以在Get中發送json參數? – y07k2

+0

你可以按照下面的回答。@ y07k2 – Lampard

回答

0

你可以打電話給你的GET方法是這樣的:

public interface EndPointInterface{ 
@GET(ProximityConstants.URL_FETCH_STORE_INFO) 
    Call<Store> getStoreInfoByBeaconUUID(@Query(ProximityConstants.BEACON_UUID) String beaconUUID); 

} 

來調用GET方法Web服務這樣進行:

Call<StoreSettings> call = apiService.getStoreSettings(mStore.getStoreID(), mCustomer.getCustId(), 
       mStore.getBeaconID(), ProximityConstants.SETTINGS_CUST_TYPE, ProximityConstants.ACTION_STORE_VISIT); 
     call.enqueue(new Callback<StoreSettings>() { 
      @Override 
      public void onResponse(Call<StoreSettings> call, Response<StoreSettings> response) { 
       ProximityUtil.writeLog(TAG, "Received store settings"); 
       mStoreSettings = response.body(); 
       SharedPreferences.Editor editor = mAppPreferences.edit(); 
       editor.putString(ProximityConstants.SETTINGS_OBJ_STORE_SETTINGS, new Gson().toJson(mStoreSettings)); 
       editor.commit(); 
       Intent intent = new Intent(BeaconScanService.this, ProximityWelcomeActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       BeaconScanService.this.startActivity(intent); 
      } 

      @Override 
      public void onFailure(Call<StoreSettings> call, Throwable t) { 
       Toast.makeText(BeaconScanService.this, "Error: " + t.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     }); 
+0

'@Query(「ranking」)字符串排名將使用數據創建此請求= {...}'?我在哪裏可以添加標題? – y07k2

+0

沒有得到你,在你得到服務你需要傳遞參數或頭? – Lampard

+0

我沒有明白。頭文件中的'accessKey'和'data'包含'ranking'參數中的字符串值的json。 – y07k2

0

嘗試如下:創建類Task.java:

public class Task { 
private String ranking; 

public Task(String ranking) { 
    this.ranking = ranking; 
} 
} 

並做如下:

public interface TaskService { 

@POST("/top40?data=") 
Call<Top40Model> getStatus(@Body Task task); 

} 

並如下使用:

Task task = new Task("world"); 
Task task = new Task("country"); 

Call<Top40Model> call=new Factory.getInstance().getStatus(task); 
call.enqueue(new Callback<Top40Model>() {}); 
+0

'「data = {」ranking「:」{ranking}「}」'我怎樣才能用String值替換'ranking'?導致例如此網址在Postman http://5.135.147.222:8400/api/top40?data= {「ranking」:「world」}'中有效。注意,'{..}'包含所有參數 – y07k2

+1

出現錯誤:'IllegalArgumentException:URL查詢字符串「data = {」ranking「:」{ranking}「}」不能有替換塊。對於動態查詢參數,在使用@Query時使用@ Query.'。我認爲必須在那裏的括號... – y07k2

+0

他說GET,而不是POST。 – nasch

2

,可以幫助我:

public interface FamousTop40Api { 
    @GET("/api/top40") 
    Call<FamousTop40Model> getStatus(
      @Query("data") String ranking 
    ); 

    class Factory { 
     private static FamousTop40Api service; 

     public static FamousTop40Api getInstance() { 
      OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
      httpClient.addInterceptor(new Interceptor() { 
       @Override 
       public Response intercept(Interceptor.Chain chain) throws IOException { 
        Request original = chain.request(); 

        Request request = original.newBuilder() 
          .header("accessKey", MainActivity.ACCESS_KEY) 
          .method(original.method(), original.body()) 
          .build(); 

        return chain.proceed(request); 
       } 
      }); 

      OkHttpClient client = httpClient.build(); 

      Retrofit retrofit = new Retrofit.Builder() 
        .baseUrl(ApiConstants.BASE_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .client(client) 
        .build(); 

      service = retrofit.create(FamousTop40Api.class); 

      return service; 
     } 
    } 
} 

所以你需要添加@QueryaccessKey在頭部與OkHttpClient

FamousTop40Api.Factory.getInstance().getStatus("{\"ranking\":\"country\"}").enqueue();