Getting 400 volley server error which is working from Rest client successfully

只愿长相守 提交于 2019-12-11 03:28:52

问题


Am getting 400 server error for volley get request for below url

http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ('AEDALL')&format=json&env=store://datatables.org/alltableswithkeys&callback=

when i hit the same url from rest client its giving json response

Here is my code for volley request

private void convertCurrenctVolleyTask(String from, final String to, final String amount) {

    String url_yahoo = "http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ('"+from+to+"')&format=json&env=store://datatables.org/alltableswithkeys&callback=";

    final ProgressDialog progressDialog;
    progressDialog = new ProgressDialog(CurrencyConverter.this);
    progressDialog.setMessage("Please wait...");
    progressDialog.setCancelable(true);
    progressDialog.show();

    JsonObjectRequest GetCurrenciesjsObjRequest = new JsonObjectRequest(
            Request.Method.GET, url_yahoo, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    progressDialog.cancel();
                    if (response != null) {
                            parseGetCurrencyResp(response,to,amount);

                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    progressDialog.cancel();
                    Toast.makeText(CurrencyConverter.this,
                            "Server error..", Toast.LENGTH_SHORT).show();
                    error.printStackTrace();

                }
            });

    ConfigVolley.getInstance().addToRequestQueue(GetCurrenciesjsObjRequest);

}

Any help appreciated


回答1:


You will need to replace all occurences of a space(" ") with the %20 url_yahoo = url_yahoo.replace(" ", "%20");




回答2:


That is not a properly formatted URL. You need to encode your URL parameters so that they do not contain invalid characters.

Try using URLEncoder before using your url_yahoo like so:

String query = "select * from yahoo.finance.xchange where pair in ('"+from+to+"')";
query = URLEncoder.encode(query, "utf-8");

String env = "store://datatables.org/alltableswithkeys&callback=";
env = URLEncoder.encode(query, "utf-8");

url_yahoo = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&env=" + env;


来源:https://stackoverflow.com/questions/30125769/getting-400-volley-server-error-which-is-working-from-rest-client-successfully

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!