Android 我如何为改造API构建此URL
这是我用来查询结果的URL:Android 我如何为改造API构建此URL,android,retrofit,Android,Retrofit,这是我用来查询结果的URL: 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%3
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=
到目前为止,我第一次使用改型,这是我如何构建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=";
我必须输入用户输入的股票符号,然后创建带有去年日期和今天日期的url。我如何通过改型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());
}
});
使用改装时应声明接口
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);
//String int@GET annotation应该是一个子路径,并且应该声明一个baseUrl
@得到(”http://query.yahooapis.com/v1/public/yql")
呼叫访问(@QueryMap-options);
将字符串“q”、“format”、“diagnostics”、“env”、“callback”作为选项的键
使用改型时,应将实际值声明为接口的选项值
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);
//String int@GET annotation应该是一个子路径,并且应该声明一个baseUrl
@得到(”http://query.yahooapis.com/v1/public/yql")
呼叫访问(@QueryMap-options);
将字符串“q”、“format”、“diagnostics”、“env”、“callback”作为选项的键
实际值作为选项的值
我必须获得字符串符号的用户输入,但问题是它在获取列表时崩溃。我必须获得字符串符号的用户输入,但问题是它在获取列表时崩溃粘贴代码并崩溃log@grantonzhunag我已经把撞车日志贴出来了您可以通过一些IM与我联系吗?@grantonzhunag我已经添加到代码中,您可以使用debug在onResponse()中查看响应实例的详细信息method@grantonzhunag谢谢,但我删除了改装,这让我很困惑。我转到了格森。非常感谢粘贴代码并崩溃log@grantonzhunag我已经发布了崩溃日志,您可以通过一些IM与我联系吗?@grantonzhunag我已经添加到代码中,您可以使用debug在onResponse()中查看响应实例的详细信息method@grantonzhunag谢谢,但我删除了改装,这让我很困惑。我转到了格森。不过非常感谢