2017-10-23 2 views
1

엔드 포인트가없는 "GET"요청을 보내려합니다. 요청을 보내면 내 "onFailure"메소드에서 응답을 얻습니다. 개장 2 엔드 포인트가없는 GET 요청?

내 URL입니다 : http://api.gios.gov.pl/pjp-api/rest/station/findAll (나는 우체부에이 URL을 입증하고, 거기에 한 나는 올바른 응답을 얻을 수 있습니다)이 내 개조 클라이언트입니다

:

여기
 private static final String BASE_URL = "http://api.gios.gov.pl/pjp-api/rest/station/findAll/"; 

private static Retrofit retrofit = null; 
public static Retrofit getRetrofitClient(String url){ 
    if(retrofit==null){ 
     retrofit = new Retrofit.Builder() 
       .baseUrl(BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .build(); 


    } 
    return retrofit; 
} 

이 인터페이스입니다 :

MainActivity에서
 @GET() 
    Call<ServerResponse> showTheStations(@Url String url); 

내 응답 방법 :

 public void retrofitRespond(){ 

    retrofitInterface = ApiUtil.getRetrofitInterface(); 

    retrofitInterface.showTheStations("http://api.gios.gov.pl/pjp- 
    api/rest/station/findAll").enqueue(new Callback<ServerResponse>() { 
     @Override 
     public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) { 



     } 

     @Override 
     public void onFailure(Call<ServerResponse> call, Throwable t) { 
      Toast.makeText(getApplicationContext(),"WENT WRONG", Toast.LENGTH_LONG).show(); 

     } 
    }); 

어떻게해야 제대로 할 수 있습니까? Retrofit 2에서 동적 URL에 대해 읽었지만 제대로 처리 할 수 ​​없습니다.

+0

당신이 오류를 게시 할 수 있습니다 ...? – tebitoq

+0

오류가 없습니다. "public void onFailure"메서드가 내 응답을 포착하고 있습니다. 나는 어떤 응답을 알아낼 수 없다. 코드()는 내가 – Adam

+0

@Adam onFailure()에서 Error.e (TAG, t.toString())를 추가하는 것을 볼 수있다; –

답변

1

사용 :

@GET() 
    Call<List<ServerResponse>> showTheStations(@Url String url); 

그리고 MainActivity에서

가 :

Call<List<ServerResponse>> call = retrofitInterface.showTheStations("http://api.gios.gov.pl/pjp-api/rest/station/findAll/"); 
     call.enqueue(new Callback<List<ServerResponse>>() { 
      @Override 
      public void onResponse(Call<List<ServerResponse>> call, Response<List<ServerResponse>> response) { 
       Log.i(TAG, response.body().toString()); 

       List<ServerResponse> body = response.body(); 

       Log.i(TAG, body.get(0).getId()+""); 
      } 

      @Override 
      public void onFailure(Call<List<ServerResponse>> call, Throwable t) { 
       // Log error here since request failed 
       Log.e(TAG, t.toString()); 
      } 
     }); 
+0

나는 이것을 시도했지만 도움이되지 않습니다. 나는 이미 많은 요청을했지만, 나는 이것을 발견 할 수 없다. – Adam

+0

@Adam 나는 왜 @GET ("findAll")을 사용하지 않는지를 의미한다. 호출 showTheStations(); –

+0

이 솔루션을 시도했지만 작동하지 않습니다.이 예외는 onFailure 메서드 "com.google.gson.stream.MalformedJsonException : JsonReader.setLenient (true)를 사용하여 1 행 1 열의 잘못된 형식의 JSON을 승인합니다. $" – Adam