Android:从“改造2"调用的RESTfull API表示“方法不允许"

编程入门 行业动态 更新时间:2024-10-23 08:40:16
本文介绍了Android:从“改造2"调用的RESTfull API表示“方法不允许"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经使用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表示“方法不允许"

本文发布于:2023-10-31 07:28:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545434.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不允许   方法   quot   Android   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!