4

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
David Gordon
  • 287
  • 4
  • 17

1 Answers1

2

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.

Community
  • 1
  • 1
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Okay I'm requesting the api and getting the json response.. However converting it to a object is confusing me because the json is so verbose. https://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= This is the api. Is there anyway to add a paramter to the url to just return the "result" array, as thats all I need? – David Gordon Nov 04 '14 at 15:15
  • 1
    Sure, just remove "&diagnostics=true", there will be less information. – Vadim Kotov Nov 05 '14 at 10:57
  • You can map only the fields you need from your JSON response. Just create fields with the same name, and getters/setters for them in your class – Vadim Kotov Nov 05 '14 at 10:59