首次使用Retrofit发起PATCH请求遇响应异常求技术指导
Hey there! Let's break down what's going wrong with your PATCH request and get it working correctly.
The Core Problem
You're treating the PATCH request like a GET request by passing an empty, meaningless @Body parameter, which is confusing both Retrofit and your server. That's why you're getting a generic 200 response instead of the expected JSON payload.
Step 1: Define a Proper Response Model
First, create a POJO class to map the expected API response. This makes parsing the JSON way easier than dealing with raw ResponseBody:
public class TicketResponse { private String result; private String flag; // Getters (and setters if needed) public String getResult() { return result; } public String getFlag() { return flag; } }
Step 2: Correct Your Retrofit Interface
Your current interface has an unnecessary @Body parameter. Since you're only passing the ticket ID via the URL path, remove that parameter and update the method to return your new response model:
@PATCH("ticket/{ticketId}/") Call<TicketResponse> verifyTicket(@Path("ticketId") String ticketId);
I renamed the method to verifyTicket to make its purpose clearer (it's a modification operation, not a "get" request).
Step 3: Update Your Request Call
Now adjust your calling code to match the corrected interface. You no longer need to pass the empty string, and you can directly access the parsed response fields:
cdsService.verifyTicket(ticketNo).enqueue(new Callback<TicketResponse>() { @Override public void onResponse(Call<TicketResponse> call, Response<TicketResponse> response) { Log.v("TAGGGEw", response.toString()); if (response.isSuccessful()) { Log.d("TAGGGE", "Ticket verification response received"); TicketResponse ticketResponse = response.body(); if (ticketResponse != null) { // Access the expected fields here String result = ticketResponse.getResult(); String flag = ticketResponse.getFlag(); Log.d("TAGGGE", "Result: " + result + ", Flag: " + flag); view.ticketVerified(); } } else { int statusCode = response.code(); view.HandleError(0); Log.v("SplashScreenPresenter", "Handling Server error - Status Code: " + statusCode); } } @Override public void onFailure(Call<TicketResponse> call, Throwable t) { if (t instanceof IOException) { t.printStackTrace(); Log.v("TAGGGE", "Network IO Exception occurred"); view.HandleError(1); Log.v("SplashScreenPresenter", "Handling Network Error"); } } });
Why This Works
- Removing the unused
@Bodyparameter ensures Retrofit sends a clean PATCH request without extra, unneeded data that the server might not expect. - Using the
TicketResponsePOJO lets Retrofit automatically parse the JSON response into a usable object, so you can easily accessresultandflaginstead of manually parsing raw response body.
Quick Check
If your server does require a request body for the PATCH endpoint (even an empty one), you can adjust the interface to pass an empty object instead of a string. For example:
@PATCH("ticket/{ticketId}/") Call<TicketResponse> verifyTicket(@Path("ticketId") String ticketId, @Body Object emptyBody);
Then call it with new Object() as the body parameter. But based on your expected response, this shouldn't be necessary.
内容的提问来源于stack exchange,提问作者Dilip Poudel

