這是我使用的查詢結果的網址:我如何構建這個網址改造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)
粘貼您的代碼和崩潰日誌 – grantonzhuang
@grantonzhunag我發佈了崩潰日誌可以聯繫我的一些IM? – Contextioner
@grantonzhunag我已添加到代碼進行改造 – Contextioner