Android:从 `retrofit 2` 调用的 RESTfull API 说 `Method Not Allowed`
Android: RESTfull API called from `retrofit 2` says `Method Not Allowed`
我使用 Java 和 Jersey 开发了一个网络服务。现在我正在尝试连接到它,调用它的方法并获取数据,使用 android。
以下是网络服务的相关部分。
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;
}
}
在我的 android 应用程序中,我使用 Retrofit 2
调用上述 REST 方法。
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();
}
}
});
}
下面是我的EndPoint
界面
public interface YourEndpoints {
@POST("patient/getPatientById/{Id}")
Call<PatientBean>getPatientById(@Body String Id);
}
然而,当我 运行 代码时,我从 Apache Tomcat Server
收到 HTML 响应,基本上是 HTTP Status 405 - Method Not Allowed
.
我该如何解决这个问题?
将您的 ws 端点更改为@GET,然后将您的其余客户端更改为以下代码:
@GET("patient/getPatientById/{Id}")
Call<PatientBean>getPatientById(@Path("Id") String Id);
GET 应该用于从服务器检索数据。
POST应该用于向服务器发送数据。
如果您将 GSON 与 RetroFit 一起使用,则在 getPatientById()
中不需要您自己的实现。而且,是的,您应该使用 GET
方法。
public interface PatientService {
@GET("patient/getPatientById/{Id}")
Call<PatientBean> getPatientById(@Path("Id") String id);
}
如果您的 PatientBean
设置正确,您应该能够调用以下命令以获得 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();
}
});
我使用 Java 和 Jersey 开发了一个网络服务。现在我正在尝试连接到它,调用它的方法并获取数据,使用 android。
以下是网络服务的相关部分。
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;
}
}
在我的 android 应用程序中,我使用 Retrofit 2
调用上述 REST 方法。
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();
}
}
});
}
下面是我的EndPoint
界面
public interface YourEndpoints {
@POST("patient/getPatientById/{Id}")
Call<PatientBean>getPatientById(@Body String Id);
}
然而,当我 运行 代码时,我从 Apache Tomcat Server
收到 HTML 响应,基本上是 HTTP Status 405 - Method Not Allowed
.
我该如何解决这个问题?
将您的 ws 端点更改为@GET,然后将您的其余客户端更改为以下代码:
@GET("patient/getPatientById/{Id}")
Call<PatientBean>getPatientById(@Path("Id") String Id);
GET 应该用于从服务器检索数据。 POST应该用于向服务器发送数据。
如果您将 GSON 与 RetroFit 一起使用,则在 getPatientById()
中不需要您自己的实现。而且,是的,您应该使用 GET
方法。
public interface PatientService {
@GET("patient/getPatientById/{Id}")
Call<PatientBean> getPatientById(@Path("Id") String id);
}
如果您的 PatientBean
设置正确,您应该能够调用以下命令以获得 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();
}
});