【问题标题】:Android Retrofit error while building a request构建请求时出现Android Retrofit错误
【发布时间】:2014-12-29 07:58:41
【问题描述】:

我想知道是否有人能告诉我为什么在我尝试使用 Retrofit 执行 RESTfull 服务时收到错误的请求错误

错误:HTTP/1.1 400 错误请求

这是我的两个课程:

改造接口:

public class RetrofitInterface {
    private static StockApiInterface sStockService;

    public static StockApiInterface getStockApiClient() {
        if (sStockService == null) {
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint("http://query.yahooapis.com/v1/public")
                    .build();
            sStockService = restAdapter.create(StockApiInterface.class);
        }

        return sStockService;
    }

    public interface StockApiInterface {
        @GET("/yql")
        void listQuotes(@Query("q") String query,Callback<Stock> stockInfo);
    }


}

MainActivity 中的异步任务

public class extraThread extends AsyncTask<Void, Void, Void>{

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        RetrofitInterface.getStockApiClient().listQuotes("select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(\"AIB.IR\")%0A%09%09&format=json&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=", new Callback<Stock>() {


            @Override
            public void failure(RetrofitError arg0) {
                // TODO Auto-generated method stub
                arg0.printStackTrace();
            }

            @Override
            public void success(Stock arg0, Response arg1) {
                // TODO Auto-generated method stub

            }
        });

    }

}

结果总是失败。我最初认为问题在于改造的内置 gson 转换器无法将响应转换为库存对象,因为我只收到“Retrofit.retrofiterror”响应。但是“错误请求”响应让我认为问题出在api 的 URL。 这是我想要的回复网址:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AIB.IR%22)%0A%09%09&amp;format=json&amp;diagnostics=true&amp;env=http%3A%2F%2Fdatatables.org%2Falltables.env&amp;callback=

谁能告诉我我是否在我的代码中正确构建了这个?一个可能的问题是我在查询中转义了我的引用。也许这会导致问题?

以防万一我发布我的 Stock 对象。我是在在线 POJO 转换器的帮助下编写的

public class Stock {

    @Expose
    private Query query;

    public Query getQuery() {
        return query;
    }


    public void setQuery(Query query) {
        this.query = query;
    }

}

对此的任何帮助将不胜感激。

【问题讨论】:

  • 将日志记录添加到 Retrofit 以查看它发出的确切请求。您可以通过将 .setLogLevel(RestAdapter.LogLevel.FULL) 添加到您的 RestAdapter 来做到这一点。
  • 另外,由于您在请求中使用了回调,因此无需将其放在 AsyncTask 中。
  • 不要转义您的 YQL 查询字符串。像使用原始 SQL 字符串一样使用它。

标签: java android rest retrofit


【解决方案1】:

您正在尝试使用一个查询字符串参数而不是多个。这不起作用,请参考this question。 另外,不需要对查询内容进行编码,Retrofit 会自动完成(见docs):

默认情况下,参数值是 URL 编码的。指定 encodeValue=false 以更改此行为。

【讨论】:

  • 好的,我正在请求 api 并获得 json 响应。但是将它转换为对象让我感到困惑,因为 json 太冗长了。 query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AIB.IR%22%2C%22BIR.IR%22)%0A%09%09&format=json&diagnostics=true&env=http%3A %2F%2Fdatatables.org%2Falltables.env&callback= 这是 API。无论如何要在url中添加一个参数来返回“结果”数组,这就是我所需要的吗?
  • 可以,去掉“&diagnostics=true”,信息会少一些。
  • 您只能从 JSON 响应中映射您需要的字段。只需创建具有相同名称的字段,并在您的类中为它们创建 getter/setter
猜你喜欢
  • 2019-03-23
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 2021-02-26
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
相关资源
最近更新 更多