0

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'; } ?>
Community
  • 1
  • 1
  • What is the site you are sending the request to? – zgc7009 May 23 '16 at 15:28
  • I am using a free hosting site.. Base.pk – user3001551 May 23 '16 at 15:31
  • I think if you hit a webpage that requires javascript it will hit this error, but you shouldn't be making requests like this to sites like that, that is why I was wondering. Are you trying to hit a url that is designed to provide users with a UI or anything? – zgc7009 May 23 '16 at 15:34
  • no there is no UI in web service code. I I try a simple response and from browser request url works fine.. – user3001551 May 23 '16 at 15:40

3 Answers3

2

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;
}
Saeed Jassani
  • 1,079
  • 2
  • 11
  • 27
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");

}
Jayesh Babu
  • 1,389
  • 2
  • 20
  • 34
vivekanand
  • 21
  • 4
0

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☺