Java 安卓:从“改造2”调用的RESTfull API表示“不允许使用方法”`
我使用Java和Jersey开发了一个web服务。现在我正在尝试连接到它,调用它的方法,并使用android获取数据 下面是web服务的相关部分Java 安卓:从“改造2”调用的RESTfull API表示“不允许使用方法”`,java,android,post,retrofit,retrofit2,Java,Android,Post,Retrofit,Retrofit2,我使用Java和Jersey开发了一个web服务。现在我正在尝试连接到它,调用它的方法,并使用android获取数据 下面是web服务的相关部分 import bean.PatientBean; import bean.UserBean; import db.PatientImpl; import db.PatientInterface; import db.UserImpl; import db.UserInterface; import java.util.List; import java
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应用程序中,我使用改型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();
}
}
});
}
然而,当我运行代码时,我从ApacheTomcat服务器得到了一个HTML响应,基本上说是HTTP Status 405-Method Not Allowed
如何解决此问题?将ws-endpoint更改为@GET,然后将rest客户端更改为以下代码:
@GET("patient/getPatientById/{Id}")
Call<PatientBean>getPatientById(@Path("Id") String Id);
@GET(“patient/getPatientById/{Id}”)
CallgetPatientById(@Path(“Id”)字符串Id);
GET应用于从服务器检索数据。
POST应用于向服务器发送数据。将ws-endpoint更改为@GET,然后将rest客户端更改为以下代码:
@GET("patient/getPatientById/{Id}")
Call<PatientBean>getPatientById(@Path("Id") String Id);
@GET(“patient/getPatientById/{Id}”)
CallgetPatientById(@Path(“Id”)字符串Id);
GET应用于从服务器检索数据。
POST应用于向服务器发送数据。如果您在改型的同时使用GSON,则不需要在
getPatientById()中使用自己的实现。
。是的,您应该使用GET
方法
public interface PatientService {
@GET("patient/getPatientById/{Id}")
Call<PatientBean> getPatientById(@Path("Id") String id);
}
如果您在改型的同时使用GSON,则不需要在
getPatientById()
中使用自己的实现。是的,您应该使用GET
方法
public interface PatientService {
@GET("patient/getPatientById/{Id}")
Call<PatientBean> getPatientById(@Path("Id") String id);
}
如果你手动向邮递员或其他好问题提出请求,会发生什么。但我真的不知道如何对此发起邮递员呼叫。你的端点看起来像一个典型的GET to me,你为什么要使用POST来检索资源?@TimCastelijns:因为我传递了一个ID。。。安,如果这是GET,它会工作吗?不管它是否工作,我建议你仔细阅读rest和http请求方法,使用哪种方法来做什么,等等。只是一个提示,如果你与邮递员或其他好问题手动发出请求,会发生什么。但我真的不知道如何对此发起邮递员呼叫。你的端点看起来像一个典型的GET to me,你为什么要使用POST来检索资源?@TimCastelijns:因为我传递了一个ID。。。安维,如果这是GET,它会工作吗?不管它是否工作,我建议你仔细阅读rest和http请求方法,使用哪种方法来做什么,等等。只是一个tipi认为问题是,他将预期的路径参数作为身体发送,所以我建议将其更改为GET,你看到这方面的任何问题吗?可能是。我只是想在你的回答后面找一个解释另一个解释是@GET行为只是检索信息,不在数据库中做任何更改,而寻找@PeakGen的他不做任何更改。我认为问题是他将预期的路径参数作为身体发送,所以我建议将其更改为GET,你觉得这有什么问题吗?可能是。我只是想在你的回答后面找一个解释另一个解释是@GET行为只是检索信息,不在数据库中做任何更改,而寻找@PeakGen的他不做任何更改。