简体   繁体   中英

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

Error : HTTP/1.1 400 Bad Request

Here are my two classes:

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

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:

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. I wrote this with the help of an online POJO converter

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 ):

Parameter values are URL encoded by default. Specify encodeValue=false to change this behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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