2016-06-09 3 views
0

자바와 저지를 사용하여 웹 서비스를 개발했습니다. 이제 저는 그것으로 연결하고, 메소드를 호출하고, 데이터를 얻으려고합니다. 안드로이드를 사용하고 있습니다.Android :`retrofit 2 '에서 호출 된 RESTfull API가`Method Not Allowed`라고 말합니다.

다음은 웹 서비스의 관련 부분입니다. 내 안드로이드 응용 프로그램에서

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; 
    } 

} 

, 나는 위의 REST 메서드를 호출 Retrofit 2을 사용하고 있습니다.

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(); 
       } 
      } 

     }); 
    } 

다음은 내가 코드를 실행하면

public interface YourEndpoints { 

    @POST("patient/getPatientById/{Id}") 
    Call<PatientBean>getPatientById(@Body String Id); 
} 

는 그러나, 나는 기본적으로 HTTP Status 405 - Method Not Allowed을 말한다 Apache Tomcat Server에서 HTML 응답을 얻을 내 EndPoint 인터페이스입니다.

이 문제를 어떻게 해결할 수 있습니까?

+1

을 –

+0

좋은 질문입니다. 하지만 우체부 호출을 어떻게해야하는지 모릅니다. –

+0

엔드 포인트가 일반적인 GET처럼 보입니다. POST를 사용하여 자원을 검색하는 이유는 무엇입니까? –

답변

3
는 WS는 @GET하는 엔드 포인트 변경 한 다음 코드 아래에 나머지 클라이언트를 변경

:

@GET("patient/getPatientById/{Id}") 
Call<PatientBean>getPatientById(@Path("Id") String Id); 

GET은 서버에서 데이터를 검색하는 데 사용되어야한다. 서버에 데이터를 보내려면 POST를 사용해야합니다.

+0

왜 해결할 수 있습니까? –

+0

나는 문제가 그가 path param으로 기대하는 것을 몸으로 보낸다라고 생각한다. 그래서 나는 그것을 get으로 바꾸는 것이 좋다는 것을 알았 는가? –

+0

수 있습니다. 난 그냥 당신의 대답 뒤에 설명을 찾고 있었어 –

1

RetroFit과 함께 GSON을 사용하는 경우 getPatientById() 내에 직접 구현할 필요가 없습니다. 그리고 예, GET 방법을 사용해야합니다.

public interface PatientService { 

    @GET("patient/getPatientById/{Id}") 
    Call<PatientBean> getPatientById(@Path("Id") String id); 

} 

당신의 PatientBean가 올바르게 설정하면, 당신은 클래스의 완전한 형태 예를 얻기 위해 다음 호출 할 수 있어야한다 : 사용자가 수동으로 우편 배달 또는 뭔가 요청을하면 어떻게됩니까

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(); 
    } 
});