자바와 저지를 사용하여 웹 서비스를 개발했습니다. 이제 저는 그것으로 연결하고, 메소드를 호출하고, 데이터를 얻으려고합니다. 안드로이드를 사용하고 있습니다.Android :`retrofit 2 '에서 호출 된 RESTfull API가`Method Not Allowed`라고 말합니다.
다음은 웹 서비스의 관련 부분입니다. 내 안드로이드 응용 프로그램에서
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;
}
}
, 나는 위의 REST 메서드를 호출
Retrofit 2
을 사용하고 있습니다.
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();
}
}
});
}
다음은 내가 코드를 실행하면
public interface YourEndpoints {
@POST("patient/getPatientById/{Id}")
Call<PatientBean>getPatientById(@Body String Id);
}
는 그러나, 나는 기본적으로 HTTP Status 405 - Method Not Allowed
을 말한다 Apache Tomcat Server
에서 HTML 응답을 얻을 내 EndPoint
인터페이스입니다.
이 문제를 어떻게 해결할 수 있습니까?
을 –
좋은 질문입니다. 하지만 우체부 호출을 어떻게해야하는지 모릅니다. –
엔드 포인트가 일반적인 GET처럼 보입니다. POST를 사용하여 자원을 검색하는 이유는 무엇입니까? –