2016-10-22 23 views
0

正如问题中提到的,我需要构建一个API接口进行改造。这是我的网址:如何构建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编码详细介绍 参数和要求的方法。

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

但是在代码中缺少一部分它是您的异步​​操作。将此代码放入您的活动课程中。

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