这是我用来查询结果的网址:
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=
到目前为止,我正在使用Retrofit,这就是我构建url的方式:
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输入,然后使用Retrofit 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 :(得分: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”,“format”,“diagnostics”,“env”,“callback”作为options
的键
实际值为options