2016-11-10 32 views
1

這是我使用的查詢結果的網址:我如何構建這個網址改造API

http://query.yahooapis.com/v1/public/yql?q=select+*+from+yahoo.finance.historicaldata+where+symbol+%3D+%22YHOO%22+and+startDate+%3D+%222015-11-10%22+and+endDate+%3D+%222016-11-10%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback= 

我使用改造的第到目前爲止,這是我建立的網址:

public static String getStockDataUrl(String stock_symbol){ 
     String startDate = Utils.getLastYear() ; 
     String endDate = Utils.getTodayDate(); 
     try{ 
      String YAHOO_BASE_URL = Constants.YAHOO_BASE_QUERY; 
      String QUERY_STOCK_DATA = Constants.QUERY_STOCK_DATA + 
        Constants.SYMBOL_QUERY +stock_symbol+ Constants.START_DATE_QUERY +startDate+"\" " + 
        Constants.END_DATE_QUERY + endDate+"\""; 
      return YAHOO_BASE_URL + URLEncoder.encode(QUERY_STOCK_DATA, "UTF-8") 
        + Constants.FORMAT_QUERY 
        + Constants.TABLES_CALLBACK_QUERY; 
     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 

這些是用於創建URL

public static final String YAHOO_BASE_QUERY = "http://query.yahooapis.com/v1/public/yql?q="; 
    public static final String QUERY_STOCK_DATA = "select * from yahoo.finance.historicaldata where "; 
    public static final String SYMBOL_QUERY = "symbol = \""; 
    public static final String START_DATE_QUERY = "\" and startDate = \""; 
    public static final String END_DATE_QUERY = "and endDate = \""; 
    public static final String FORMAT_QUERY = "&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables."; 
    public static final String TABLES_CALLBACK_QUERY = "org%2Falltableswithkeys&callback="; 

我必須來自用戶的輸入STOCK_SYMBOL常量,然後創建噸他與去年的日期和今天的日期我怎麼能實現這與改造API接口?

這是改型查詢數據

public void getStockQuotes(String symbol) { 
     QuotesAPI apiService = 
       ApiClient.getClient().create(QuotesAPI.class); 

     String query = Utils.getStockDataUrl(symbol); 

     Log.d(LOG_TAG, query); 

     Call<QuotesResponse> call = apiService.getQuotes(query); 

     call.enqueue(new Callback<QuotesResponse>() { 

      @Override 
      public void onResponse(Call<QuotesResponse> call, retrofit2.Response<QuotesResponse> response) { 
       List<Quotes> movies = response.body().getResults(); 
       Log.d(TAG, "Number of movies received: " + movies.size()); 
      } 

      @Override 
      public void onFailure(Call<QuotesResponse> call, Throwable t) { 
       Log.d(TAG, t.toString()); 
      } 
     }); 

這是QuotesAPI接口我已創建

@GET 
     Call<QuotesResponse> getQuotes(@Url String url); 

這些是logcat的細節的方法:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.android.stockhawk.quotes.QuotesResponse.getResults()' on a null object reference 
                  at com.android.stockhawk.service.StockTaskService$1.onResponse(StockTaskService.java:175) 
                  at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) 
                  at android.os.Handler.handleCallback(Handler.java:746) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:5443) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
+0

粘貼您的代碼和崩潰日誌 – grantonzhuang

+0

@grantonzhunag我發佈了崩潰日誌可以聯繫我的一些IM? – Contextioner

+0

@grantonzhunag我已添加到代碼進行改造 – Contextioner

回答

0

的接口應該在使用Retrofit時聲明。

public interface StockDataService {} 

在這個界面中,聲明的方法:

// String int @GET annotation should be a subPath and you should declare a baseUrl 
@GET("http://query.yahooapis.com/v1/public/yql") 
Call<Result> access(@QueryMap Map<String, String> options); 

放字符串 「Q」, 「格式」, 「診斷」, 「ENV」, 「回調」 爲options 實際鍵值爲options

+0

我必須得到字符串符號 – Contextioner

+0

的用戶輸入它的工作,但問題是,它在獲取列表崩潰 – Contextioner

+0

@Contextioner粘貼您的代碼和崩潰日誌 – grantonzhuang