如何在Retrofit 2中移除Base Url末尾的斜杠/
Hey, let's break down what's going wrong here and get your Bkash API call working correctly.
The Problem Breakdown
You're stuck with two frustrating issues:
- When you add a trailing slash to your Base URL (
https://www.bkashcluster.com:9081/dreamwave/merchant/trxcheck/sendmsg/), Retrofit appends your query parameters right after that slash. This creates a URL likehttps://www.bkashcluster.com:9081/dreamwave/merchant/trxcheck/sendmsg/?user=XX&pass=r@12...— the extra slash aftersendmsgis what's triggering the 500 Internal Server Error, since the API expectssendmsgto be followed directly by the?and parameters. - If you remove the trailing slash from the Base URL, your app crashes because the manual URL string you're building doesn't play nice with Retrofit's URL resolution logic.
The Solution
The cleanest way to fix this is to adjust your Base URL structure and let Retrofit handle parameter stitching instead of doing it manually. Here's how to implement it:
Step 1: Fix the Base URL
Trim the Base URL down to the parent directory of sendmsg, making sure it ends with a slash (this is key for Retrofit's proper URL resolution):
.baseUrl("https://www.bkashcluster.com:9081/dreamwave/merchant/trxcheck/")
Step 2: Update Your API Interface
Instead of using a raw @Url parameter, define the sendmsg endpoint in the @GET annotation and use @Query for each parameter. This lets Retrofit handle URL construction correctly, avoiding extra slashes or encoding mistakes:
@GET("sendmsg") Call<List<Transaction>> getBkashTrasactionCode( @Query("user") String user, @Query("pass") String pass, @Query("msisdn") String msisdn, @Query("trxid") String trxid );
Step 3: Adjust the Request Call
Remove the manual URL string concatenation and pass each parameter directly to the API method. We'll also add small tweaks to make the code more robust:
private void requestDataForBkashTransaction() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://www.bkashcluster.com:9081/dreamwave/merchant/trxcheck/") .addConverterFactory(GsonConverterFactory.create()) .build(); InstituteService api = retrofit.create(InstituteService.class); // Let Retrofit handle parameter stitching automatically Call<List<Transaction>> call = api.getBkashTrasactionCode( "Exampll", "12345", "0160000000", "6BM3KRWHLB" ); call.enqueue(new retrofit2.Callback<List<Transaction>>() { @Override public void onResponse(Call<List<Transaction>> call, retrofit2.Response<List<Transaction>> response) { if (!response.isSuccessful()) { // Show a clearer error message for debugging Toast.makeText(PaymentActivity.this, "Error Code: " + response.code(), Toast.LENGTH_LONG).show(); return; } List<Transaction> transactions = response.body(); // Add null check to avoid crashes if response body is empty if (transactions != null) { for (Transaction transaction : transactions) { String content = transaction.getTrxId(); textView.append(content); } } } @Override public void onFailure(Call<List<Transaction>> call, Throwable t) { // Give feedback when the request fails entirely Toast.makeText(PaymentActivity.this, "Request Failed: " + t.getMessage(), Toast.LENGTH_LONG).show(); } }); }
Why This Works
- By setting the Base URL to
trxcheck/, Retrofit correctly appendssendmsgwithout adding an extra slash, resulting in the proper endpoint path:trxcheck/sendmsg. - Using
@Queryannotations is safer and more maintainable than manual URL concatenation — Retrofit automatically handles parameter encoding and ensures the URL matches what the API expects. - The added null checks and error messages make your code more resilient and help with debugging if something goes wrong.
If you absolutely need to stick with the @Url approach (not recommended), you could adjust your URL string to include sendmsg before the parameters:
String urlString = "sendmsg?user=Exampll&pass=12345&msisdn=0160000000&trxid=6BM3KRWHLB";
But the @Query method is far cleaner and less error-prone.
内容的提问来源于stack exchange,提问作者Hafizur Rahman Shaik




