2016-10-22 13 views
0

上記のように、私は改造のためのAPIインタフェースを構築する必要があります。これは私のURLです:改造のためのAPIインターフェイスを作成するには?

https://api.flightstats.com/flex/weather/rest/v1/json/all/COK?appId=XXXXXXXXX&appKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&codeType=IATA 
//--------------------------------------------------------^^^-------------------------------------------------------------------- 

上記マークとして、問題は、空港コードの位置COK、である、と私は、要求にそれを渡す必要があり、しかし、私は私の実装でそれを渡すことができません改造のインターフェイス作成のこれは私のコードです:

WeatherApiClient.class

public class WeatherApiClient { 

    public static final String BASE_URL = "https://api.flightstats.com/flex/weather/rest/v1/json/all/"; 
    private static Retrofit retrofit = null; 


    public static Retrofit getClient() { 
     if (retrofit==null) { 
      retrofit = new Retrofit.Builder() 
        .baseUrl(BASE_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 
     } 
     return retrofit; 
    } 
} 

ながら、私はまた、空港のコードを渡すことができるように私は私のコードを変更する必要がありますどのようにWeatherApiInterface.class

public interface WeatherApiInterface { 
    @GET("COK") // this should be dynamic 
    Call<WeatherPojo> getValues(@Query("appId") String appId, @Query("appKey") String key, @Query("codeType") String code); 
} 

リクエスト?

+0

私は改装についてよく分かりませんが、getValues()に "COK"を追加する方法はありますか?そして、あなたは位置のための質問キーが必要です。 (それはクエリ文字列でsomeKey = COKでなければなりません) – Toris

+0

おそらく@Get( "。")かそのようなものです。 – Toris

答えて

0

このintrefaceコードを試してみて、

public interface WeatherApiInterface{ 
    @GET("{id}") 
    Call<WeatherPojo> getValues(@Path("id") String airportId, @Query("appId") String appId, @Query("appKey") String key, @Query("codeType") String code); 
} 

あなたはこれを呼び出しているか示されていません。だから私はそれが良いと仮定します。

+0

いいえ、動作しません! – OBX

+0

エラーとは何ですか?私の魅力のように動作します。私のコード全体を追加する必要がありますか? –

0

あなたの質問には、すでにRetrofitインスタンスが作成されています。

public static final String BASE_URL = "https://api.flightstats.com/flex/weather/rest/v1/json/all/"; 
    private static Retrofit retrofit = null; 
    public static Retrofit getClient() { 
     if (retrofit==null) { 
      retrofit = new Retrofit.Builder() 
        .baseUrl(BASE_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 
     } 
     return retrofit; 

また、あなたのコード内のパラメータとリクエスト方法 についてEndpoint.Thisエンコードの詳細に言及。

あなたのコードに欠けている部分は、あなたの非同期操作です。このコードをアクティビティクラスに入れてください。

ApiInterface apiService = 
       ApiClient.getClient().create(WeatherApiInterface.class); 

     Call<WeatherPojo> call = apiService.getValues(appId,appKey,codeType); 
     call.enqueue(new Callback<WeatherPojo>() { 
      @Override 
      public void onResponse(Call<WeatherPojo>call, Response<WeatherPojo> response) { 
       List<Movie> movies = response.body().getResults(); 
       Log.d(TAG, "Number of movies received: " + movies.size()); 
      } 

      @Override 
      public void onFailure(Call<WeatherPojo>call, Throwable t) { 
       // Log error here since request failed 
       Log.e(TAG, t.toString()); 
      } 
     }); 
関連する問題