PeakGen
PeakGen

Reputation: 23035

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 service.

import bean.PatientBean;
import bean.UserBean;
import db.PatientImpl;
import db.PatientInterface;
import db.UserImpl;
import db.UserInterface;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/patient")
public class PatientJSONService 
{

    @POST
    @Path("/getPatientById/{Id}")
    @Produces(MediaType.APPLICATION_JSON)       
    public PatientBean getPatientById(@PathParam("Id")String Id)
    {
        PatientInterface patinetInterface=new PatientImpl();
        PatientBean patientById = patinetInterface.getPatientById(Id);
        return patientById;
    }

}

In my android application, I am using Retrofit 2 to call the above REST method.

private void restCall()
    {
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        YourEndpoints request = retrofit.create(YourEndpoints.class);


        Call<PatientBean> yourResult = request.getPatientById("ERTA001");
        yourResult.enqueue(new Callback<PatientBean>() {
            @Override
            public void onResponse(Call<PatientBean> call, Response<PatientBean> response) {

                try {
//                    Log.d("MainActivity", "RESPONSE_A: " + response.body().toString());
                    Log.d("MainActivity", "RESPONSE: " + response.errorBody().string());
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }



            }

            @Override
            public void onFailure(Call<PatientBean> call, Throwable t) {
                try {
                    t.printStackTrace();
                    Log.d("MainActivity", "RESPONSE: "+"FAILED");
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

        });
    }

Below is my EndPoint interface

public interface YourEndpoints {

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

However When I run the code, I get a HTML response from Apache Tomcat Server, which basically says HTTP Status 405 - Method Not Allowed.

How can I solve this issue?

Upvotes: 0

Views: 3367

Answers (2)

Lucas Ferraz
Lucas Ferraz

Reputation: 4152

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.

Upvotes: 3

Bryan
Bryan

Reputation: 15155

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.

public interface PatientService {

    @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(new Callback<PatientBean> {
    @Override
    public void onResponse(Call<PatientBean> call, Response<PatientBean> response) {
        mPatientBean = response.body();
    }

    @Override
    public void onFailure(Call<PatientBean> call, Throwable throwable) {
        throwable.printStackTrace();
    }
});

Upvotes: 1

Related Questions