2017-10-23 7 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; 
} 

はインターフェイスです

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

MainActivityで私の応答方法:

 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」というメソッドが自分の応答をキャッチしていることを意味します。私は把握することができませんどのようなResponse.code()私は – Adam

+0

@アダムを持っているエラーを見るには、onFailure()を見て、Log.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

関連する問題