Android:从“改造2"调用的RESTfull API表示“方法不允许" [英] Android: RESTfull API called from `retrofit 2` says `Method Not Allowed`

查看:181
本文介绍了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表示“方法不允许"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆