我已经使用Java和Jersey开发了一个Web服务.现在,我尝试使用android连接到它,调用它的方法并获取数据.
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.
下面是Web服务的相关部分.
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; } }在我的android应用程序中,我正在使用Retrofit 2调用上述REST方法.
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(); } } }); }下面是我的EndPoint界面
public interface YourEndpoints { @POST("patient/getPatientById/{Id}") Call<PatientBean>getPatientById(@Body String Id); }但是,当我运行代码时,我从Apache Tomcat Server收到了HTML响应,基本上是HTTP Status 405 - Method Not Allowed.
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?
推荐答案将ws端点更改为@GET,然后将其余客户端更改为以下代码:
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应该用于从服务器检索数据. POST应该用于将数据发送到服务器.
GET should be used to retrieve data from the server. POST should be used to send data to the server.
更多推荐
Android:从“改造2"调用的RESTfull API表示“方法不允许"
发布评论