2017-07-16 41 views
0

我從服務器的URL中包含下列參數:改造的baseUrl與參數

http://example/?p1=a&p2=b 

這將是服務器的地址,我會發送請求。

當我使用下面的代碼來做一個新的改造。

retrofit.newBuilder().baseUrl(url).build(); 

攔截

Request oldRequest = chain.request(); 
Logger.e("url:" + oldRequest.url()); 

日誌:

url:http://example/ 

但我想使的baseUrl,如:

http://example/?p1=a&p2=b 

http://example/ 

那麼,有沒有一些方法來使的baseUrl與參數?

回答

0

請嘗試以下操作,這裏是您的generator類,它可能看起來像這樣。

public class ServiceGenerator { 
    public static String apiBaseUrl = "http://example/"; 
    private static Retrofit retrofit; 

    private static Retrofit.Builder builder = 
      new Retrofit.Builder() 
        .addConverterFactory(GsonConverterFactory.create()) 
        .baseUrl(apiBaseUrl); 

    private static OkHttpClient.Builder httpClient = 
      new OkHttpClient.Builder(); 

    // No need to instantiate this class. 
    private ServiceGenerator() { 
    } 

    public static void changeApiBaseUrl(String newApiBaseUrl) { 
     apiBaseUrl = newApiBaseUrl; 

     builder = new Retrofit.Builder() 
         .addConverterFactory(GsonConverterFactory.create()) 
         .baseUrl(apiBaseUrl); 
    } 

    public static <S> S createService(Class<S> serviceClass, AccessToken token) { 
     String authToken = token.getTokenType().concat(token.getAccessToken()); 
     return createService(serviceClass, authToken); 
    } 

    // more methods 
    // ... 
} 

之後,你可以在你的活動如下方式使用它,或者fragmen

public class DynamicBaseUrlActivity extends AppCompatActivity { 

    public static final String TAG = "CallInstances"; 
    private Callback<ResponseBody> downloadCallback; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_file_upload); 

     downloadCallback = new Callback<ResponseBody>() { 
      @Override 
      public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
       Log.d(TAG, "server contacted at: " + call.request().url()); 
      } 

      @Override 
      public void onFailure(Call<ResponseBody> call, Throwable t) { 
       Log.d(TAG, "call failed against the url: " + call.request().url()); 
      } 
     }; 

     // first request 
     FileDownloadService downloadService = ServiceGenerator.create(FileDownloadService.class); 
     Call<ResponseBody> originalCall = downloadService.downloadFileWithFixedUrl(); 
     originalCall.enqueue(downloadCallback); 

     // change base url 
     ServiceGenerator.changeApiBaseUrl("http://example/?p1=a&p2=b"); 

     // new request against new base url 
     FileDownloadService newDownloadService = ServiceGenerator.create(FileDownloadService.class); 
     Call<ResponseBody> newCall = newDownloadService.downloadFileWithFixedUrl(); 
     newCall.enqueue(downloadCallback); 
    } 
} 

欲瞭解更多信息檢查this

+0

它贏得」 t工作,方法** baseUrl()**不會包含參數,它可以改變baseUrl,但仍然在URL中丟失了參數y我們的方式。我想要的是當發送請求時,不要在URL中丟失參數。 – ChengTao

+0

我編輯了我的答案,失去了一個重要部分。 –

+0

我看到了鏈接,它不起作用。 – ChengTao

0

你需要使用HttpUrl

HttpUrl url = new HttpUrl.Builder() 
        .scheme("https") 
        .host("www.google.com") 
        .addPathSegment("search") 
        .addQueryParameter("q", "polar bears") 
        .build(); 

retrofit = new Retrofit.Builder() 
       .baseUrl(url) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(httpClient) 
       .build();