简体   繁体   English

构建请求时出现Android Retrofit错误

[英]Android Retrofit error while building a request

I was wondering if anyone could tell me why I am getting a bad request error when I attempt to perform a RESTfull service using Retrofit 当我尝试使用Retrofit执行RESTfull服务时,我想知道是否有人能告诉我为什么我收到错误的请求错误

Error : HTTP/1.1 400 Bad Request 错误:HTTP / 1.1 400错误请求

Here are my two classes: 这是我的两个班级:

RetrofitInterface: RetrofitInterface:

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);
    }


}

Asyntask within MainActivity MainActivity中的Asyntask

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

            }
        });

    }

}

The result is always a failure. 结果总是失败。 I thought originally that the problem was Retrofit's built in gson converter was having trouble converting the response to a stock object, as I was only receiving a "Retrofit.retrofiterror' response. However the 'Bad Request' response has me thinking the problem is in the URL for the api. Here is my desired response url: 我原本以为问题是Retrofit内置的gson转换器无法将响应转换为stock对象,因为我只收到了“Retrofit.retrofiterror”响应。但是'Bad Request'响应让我觉得问题在于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&format=json&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=

Could anyone tell me if I am building this correctly in my code? 谁能告诉我,如果我在我的代码中正确构建这个? One possible issue is that I escape my quotations in the query. 一个可能的问题是我在查询中逃避了我的引用。 Maybe this is causing problems? 也许这会导致问题?

Just in case I will post my Stock object. 以防我发布我的Stock对象。 I wrote this with the help of an online POJO converter 我是在在线POJO转换器的帮助下写的

public class Stock {

    @Expose
    private Query query;

    public Query getQuery() {
        return query;
    }


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

}

Any help on this would be greatly appreciated. 任何有关这方面的帮助将不胜感激。

You're trying to use one query string parameter instead of multiple. 您尝试使用一个查询字符串参数而不是多个。 This is not going to work, please refer to this question . 这不起作用,请参考这个问题 Also, there is no need to encode query contents, Retrofit will do it automatically (see docs ): 此外,无需编码查询内容,Retrofit将自动执行(请参阅文档 ):

Parameter values are URL encoded by default. 参数值默认为URL编码。 Specify encodeValue=false to change this behavior. 指定encodeValue = false以更改此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM