Getting HTML response instead of JSON in android

后端 未结 2 1473
天命终不由人
天命终不由人 2020-12-06 21:18

I\'m trying to retrieve data from mysql database located on a remote server through php into android. This worked fine while running in localhost. But when it is in remote s

相关标签:
2条回答
  • 2020-12-06 21:39

    Atlast found solution...

    The issue was not with the android or php part. It was the problem with the server. The hosting site which I used, sends cookies to client side which is not handled inside android but is automatically handled by browsers. I used to another hosting site where cookies are not involved and got the needed json output.

    0 讨论(0)
  • 2020-12-06 21:56

    I spent a long time to understand and solve the problem.

    Firstly, we need to understand that 0fess hosting has anti bot technique which blocks the calls from (none-browser) clients. The main idea of this technique is using a javascript script that checks if the request is coming from a normal web browser then the script encrypts the IP and sets a cookie with key __test and value of the encrypted IP.

    To solve such a problem we need to run a webview inside our application and request any page from the our 0fees site. then we can intercept the response and get the cookie. after that, we can use this cookie as a request header in our http rest requests.

    This is a sample code

     WebView browser=new WebView(getContext());
        browser.getSettings().setJavaScriptEnabled(true); 
        browser.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url){
                final String cookies = CookieManager.getInstance().getCookie(url);
                Log.d("any", "All the cookies by me in a string:" + cookies);
                HttpPost httppost = new HttpPost("http://ksos.0fees.us/pgm_list.php");
                httppost.setHeader("Accept", "application/json");
                httppost.setHeader("Cookie", cookies);
                //continue your request paramters 
               }
           }
      );
    browser.loadUrl("http://ksos.0fees.us/");
    
    0 讨论(0)
提交回复
热议问题