2017-07-16 43 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();