1

I have an app that sends data to a database on a remote server. Until today it worked, but now suddenly it has stopped working. Instead of getting a JsonObject back from the php file which is on the remote server I am getting an html page which is titled "checking your browser". What happened and how can I fix it now? this is the code to my parser:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    public JSONParser() {

    }

    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.1.2; fr-fr; Nexus S Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);
                httpGet.setHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.1.2; fr-fr; Nexus S Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
                Log.d("URL", url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            json = (json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
             jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

 }

The logcat shows something like this in all the links..

08-10 01:09:55.814: E/result(6744): <html><body><h2>Checking your browser..<h2><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("7965e114a1dccaf35af3756261f75ad8");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";location.href="http://realroom.byethost24.com/medical/stokist.php?ckattempt=1";</script></body></html>
08-10 01:09:55.814: E/JSON Parser(6744): Error parsing data org.json.JSONException: Value <html><body><h2>Checking of type java.lang.String cannot be converted to JSONObject
08-10 01:09:55.816: E/AndroidRuntime(6744): FATAL EXCEPTION: AsyncTask #1
08-10 01:09:55.816: E/AndroidRuntime(6744): Process: com.example.medionline, PID: 6744
08-10 01:09:55.816: E/AndroidRuntime(6744): java.lang.RuntimeException: An error occured while executing doInBackground()
08-10 01:09:55.816: E/AndroidRuntime(6744):     at android.os.AsyncTask$3.done(AsyncTask.java:304)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.lang.Thread.run(Thread.java:818)
08-10 01:09:55.816: E/AndroidRuntime(6744): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
08-10 01:09:55.816: E/AndroidRuntime(6744):     at com.example.medionline.All_supplier_list$FetchMFG.doInBackground(All_supplier_list.java:182)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at com.example.medionline.All_supplier_list$FetchMFG.doInBackground(All_supplier_list.java:1)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at android.os.AsyncTask$2.call(AsyncTask.java:292)
08-10 01:09:55.816: E/AndroidRuntime(6744):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-10 01:09:55.816: E/AndroidRuntime(6744):     ... 4 more

There might be some configuration change in byethost server, but I cant figure it out..

Malav Shah
  • 472
  • 1
  • 6
  • 18
  • 1
    What URL are you trying to access? Are you using a different browser/device than previously when it worked? – Tim Biegeleisen Aug 08 '15 at 11:53
  • If your own code has not changed, then probably the server has changed. Have you tried the url in your browser? – Sascha Kolberg Aug 08 '15 at 12:21
  • I am using the same device, browser and url. I am trying to get to the database which is stored on https://byethost.com/ should I try moving my database to another host? – user2819654 Aug 08 '15 at 13:31
  • I am facing same problem.. 2 days back all was working great, suddenly it is not fetching the json string in correct format and passing html values with "checking the browser". What I notice that this is happening to links that are hosted on byethost.. I tried to transfer on other server and it worked well.. Can anyone help what happened..?? – Malav Shah Aug 09 '15 at 19:43
  • @malavshah to which server did you transfer? – user2819654 Aug 11 '15 at 05:36
  • @SaschaKolberg I tried the url in my browser and I am really directed to a webpage that says "checking your browser" before being directed to my php file. Is there something I can do about it? – user2819654 Aug 11 '15 at 06:44
  • http://www.000webhost.com/ Its good.. They also append a script, but they provide a way to stop it.. In the public html folder there is a inbuilt file called .htaccess. open that file and append the following code - "php_value auto_append_file none" now all done – Malav Shah Aug 12 '15 at 15:27
  • I haven't tried this in byethost server as I already transfer all my stuff.. you can try by making .htaccess file and append the code.. and do notify me if it works – Malav Shah Aug 12 '15 at 15:33
  • @malavshah I tried but couldn't find that file. there is a folder called "access" with what seems like files that log all my attempts to access the database. I don't think that's the file you meant and that is the only one I found. I'll try moving my project to 000webhost.com as well. thank you – user2819654 Aug 12 '15 at 18:12

1 Answers1

0

Is the remote server behind the CloudFlare CDN? It has an anti-DDOS feature that allows through only user agent that support cookies and JavaScript. Perhaps the server operator set the anti-DDOS setting to "I'm Under Attack", the strictest setting. Or the CDN might see an unfamiliar User-agent string, that of your Android application, and it might trip the DDOS heuristic.

If you want to access the server that a CDN is protecting from distributed denial of service, you may have to contact the server operator to get your IP address whitelisted in Threat Control. Or the server operator may make available a non-CDN-guarded endpoint URL to holders of OAuth API credentials that the server operator has issued.

As for your particular case, I'm not familiar with Byet. It may make the anti-DDOS settings available to hosting subscribers, or it may not.

Related question: Can one cache and secure a REST API with Cloudflare?

Community
  • 1
  • 1
Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64