Skip to content Skip to sidebar Skip to footer

Android: Restfull Api Called From `retrofit 2` Says `method Not Allowed`

I have developed a web service using Java and Jersey. Now I am trying to connect into it, call its methods and get data, using android. Below is the related part of the web servic

Solution 1:

Change your ws endpoint to @GET, and then change your rest client to below code:

@GET("patient/getPatientById/{Id}")
Call<PatientBean>getPatientById(@Path("Id") String Id);

GET should be used to retrieve data from the server. POST should be used to send data to the server.

Solution 2:

If you are using GSON along with RetroFit, you should not need your own implementation within getPatientById(). And, yes you should be using a GET method.

publicinterfacePatientService {

    @GET("patient/getPatientById/{Id}")
    Call<PatientBean> getPatientById(@Path("Id") String id);

}

If your PatientBean is setup correctly, you should be able to call the following to get a fully formed instance of the class:

PatientService service = retrofit.create(PatientService.class);
Call<PatientBean> call = service.getPatientById("ERTA001");

call.enqueue(newCallback<PatientBean> {
    @OverridepublicvoidonResponse(Call<PatientBean> call, Response<PatientBean> response) {
        mPatientBean = response.body();
    }

    @OverridepublicvoidonFailure(Call<PatientBean> call, Throwable throwable) {
        throwable.printStackTrace();
    }
});

Post a Comment for "Android: Restfull Api Called From `retrofit 2` Says `method Not Allowed`"