Volley Server error this site requires java script enabled

夙愿已清 提交于 2019-12-12 05:28:28

问题


I am using volley to send web service request and volley response is SERVER ERROR (this site requires java script enabled). I tried POST and GET methods. After some search on this issue I found these two questions.. Volley Server error (Requires javascript?) Android Development but there is no answer to solve this issue in 2 months

Disable Javascript on volley StringRequest but there is no answer to solve this issue in 6 months

No more questions and solution found. Can anyone tell the exact problem and solution with this issue. Here is my code

public void postRequest(final JSONObject jsonParams,final Handler handler){
    StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConstants.ADMIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Message message = handler.obtainMessage();
                    message.obj = response;
                    handler.sendMessage(message);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Message message = handler.obtainMessage();
                    message.obj = error.toString();
                    Log.d("error",error.toString());
                    handler.sendMessage(message);
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();

            Iterator<String> iter = jsonParams.keys();
            while (iter.hasNext()) {
                try {
                    String key = iter.next();
                    String value = jsonParams.get(key)+"";
                    params.put(key,value);

                } catch (JSONException e) {
                    // Something went wrong!
                    Message message = handler.obtainMessage();
                    message.obj = e.getMessage();
                    handler.sendMessage(message);
                }
            }
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> headers = new HashMap<String, String>();
            return headers;
        }
    };

    MyVolley.getInstance(context).getRequestQueue().add(stringRequest);
}

and web service code is

<?php if(true) { echo 'request arrived'; } ?>

回答1:


There is a nice workaround suggested in this answer.

In Volley, you can override getHeaders() method to send the cookie with your HTTP request.

Code :

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> map = new HashMap<>();
    map.put("Cookie", "__test=YOUR COOKIE HERE; expires=Friday, January 1, 2038 at 5:25:55 AM; path=/");
    return map;
}



回答2:


i faced the same problem , and there is no direct solution for this problem is:- some servers won't allow these type requests , they allow only web browsers

solution:-

  1. Implement a webview in your app without any UI (if you are expecting Json response).
  2. Override onPageFinished inside setWebwiewclient().
  3. get the page cookie by : String cookies = CookieManager.getInstance().getCookie(url);
  4. after getting cookie pass the cookie in header of volley request by key="cookie"

code reference:-

   WebView webViewRequest=new WebView("your app context");

    webViewRequest.getSettings().setJavaScriptEnabled(true);
    webViewRequest.setWebViewClient(new WebViewClient());
    webViewRequest.getSettings().setLoadWithOverviewMode(true);
    webViewRequest.getSettings().setUseWideViewPort(true);
    webViewRequest.getSettings().setSupportZoom(false);
    webViewRequest.getSettings().setBuiltInZoomControls(false);
    webViewRequest.getSettings().setDisplayZoomControls(false);
    webViewRequest.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webViewRequest.requestFocusFromTouch();
    webViewRequest.getSettings().setAppCacheEnabled(false);
    webViewRequest.setScrollbarFadingEnabled(false);

    webViewRequest.setWebViewClient(new WebViewClient(){


        @Override
        public void onPageFinished(WebView view, String url) {
      cookies = CookieManager.getInstance().getCookie(url);//here you will get cookie
            Log.d(TAG, "onPageFinished: "+cookies);
            StringRequest Request = new StringRequest(url, new 
     Response.Listener<String>() {


                @Override
                public void onResponse(String response) {
                    Log.d(TAG, "onResponse: "+response.toString());
                   // your data from server
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {

                    Map<String, String> map = new HashMap<>();
                    map.put("Cookie", cookies);
                    return map;
                }
            };
            Mysingleton.getMinstance(DashboardActivity.context).addToRequestQue(Request);

        }
    });
    webViewRequest.loadUrl("your url here");

}



回答3:


I came across this error recently! Your code is totally fine but the problem lies with your web host. (Is it byethost)? It has a bot detector running which does not let your Android volley request in.Unfortunately this cannot be disabled /turned off. Try changing your web hosting service. Hope this helps☺



来源:https://stackoverflow.com/questions/37394310/volley-server-error-this-site-requires-java-script-enabled

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