2017-05-24 6 views
0

私はアンドロイドでレトロフィットを使用しています。私は良い方法で働いているget要求を持っていますが、URLのエンドポイントに共有設定の値を含める必要があります。私のエンドポイントURLが:Androidエンドポイントで共通の設定値を追加しました

public interface Data{ 
    @GET("/myphone/extra/pull/Sharedpreferencevalue") //add shared preference value here 
} 

私は改造でこれを行うことができますか、他の方法と関係がありますか?どのように改装することができますか?

あなたは、プログラムのエンドポイントに値を追加する @Pathアノテーションを使用して、あなたの改造のサービス・インターフェースでこのような何かを行うことができ
+0

チェッククエリパラメータ@ http://square.github.io/retrofit/ – Raghunandan

答えて

0

次のように、動的パラメータを追加することができます

@GET("/myphone/extra/pull/{Sharedpreferencevalue}") 
Call<YourResponseClass> getData(@Path("Sharedpreferencevalue") String value); 
0

@GET("/myphone/extra/pull/{sharedprefValue}") 
Call<EntityName> getPref(@Path("sharedprefValue") String pref); 
0

urlを動的に使用して、次のようにretrofit2を実行します。あなたのインターフェースで

@GET 
public Call<ResponseBody> fetchMileage(@Url String url); 

使用もこのよう

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
    Retrofit.Builder builder = new Retrofit.Builder() 
        .baseUrl(ROOT_URL) 
        .addConverterFactory(GsonConverterFactory.create()); 
    Retrofit retrofit = builder.client(httpClient.build()).build(); 
    MyInterface myInterface = retrofit.create(MyInterface.class); 
    Call<ResponseBody> result = myInterface.fetchMileage(endpointUrl); 
    result.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
      String output =""; 
      if (response.isSuccessful()) { 
       try { 

        output = response.body().string(); 


       }catch (IOException e) 
       { 
        e.printStackTrace(); 
       } 


      }else{ 
      } 

     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable throwable) { 
      //Toast.makeText(context,"Error "+throwable.getMessage(),Toast.LENGTH_LONG).show(); 
      throwable.printStackTrace(); 
     } 
    }); 
関連する問題