【问题标题】:How can I construct this URL for RetroFit API如何为 RetroFit API 构建此 URL
【发布时间】:2016-11-11 23:17:08
【问题描述】:

这是我用来查询结果的 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%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 输入,然后创建带有去年日期和今天日期的 url 如何使用 Retrofit API 接口实现这一点?

这是用retrofit查询数据的方法

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)

【问题讨论】:

  • 粘贴您的代码和崩溃日志
  • @grantonzhunag 我已经发布了崩溃日志,你可以通过一些 IM 联系我吗?
  • @grantonzhunag 我已添加到代码中以进行改造
  • 可以在onResponse()方法中使用debug查看响应实例的详细信息
  • @grantonzhunag 谢谢,但我删除了改造它让我感到困惑。我继续使用gson。非常感谢

标签: android retrofit


【解决方案1】:

使用 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的值

【讨论】:

  • 我必须得到用户输入的字符串符号
  • 它有效,但问题是它在获取列表时崩溃了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
  • 1970-01-01
相关资源
最近更新 更多