Why I can't retrieve data from my webserver as json but i can when i test it on localhost

╄→гoц情女王★ 提交于 2019-12-12 03:06:26

问题


I'm trying to create a simple login app that connects to mysql database. The app works find when i test it in localhost , but when i uploaded the database and the php scripts to a Hosting server ( Byethost ) it is not working, the problem seems with the response string form the server, it is not "JSON"

as i mentioned above, when i test the application in localhost it works just fine.

here is the Andorid Code

        requestQueue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try{
                Log.d("#-SERVER RESPONSE-#",response);
                JSONObject jsonObject = new JSONObject(response);
                Log.d("#-LOGIN STATUS-#",jsonObject.getString("status"));
            } catch (Exception e){
                Log.d("#-JSON ERROR-#",e.toString());
            }//end if
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("#-VOLLEY ERROR-#",error.toString());
                }
            }){

        @Override
        public Map<String,String> getParams(){
            Map<String,String> params = new HashMap<>();
            params.put(KEY_USERNAME,username);
            params.put(KEY_PASSWORD,password);
            return params;
        }
    };
    requestQueue.add(stringRequest);

And here is the php code

    $username = mysqli_real_escape_string($connection,$_POST['username']);
$password = mysqli_real_escape_string($connection,$_POST['password']);

$query = "SELECT * FROM user WHERE username='{$username}' AND password='{$password}'";
$result = mysqli_query($connection,$query);

$json_reply['status'] = 1; //erorr by default

if(mysqli_num_rows($result) > 0){
  $json_reply['status'] = 0; //success
}

print(json_encode($json_reply));

Here is what the output of the Android Monitor window.

Response string

<html><body><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("99efa97c59e1cf6146f7e801802ed6bc");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="http://zsoft.byethost8.com/login.php?ckattempt=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

回答1:


I had a similar problem a while ago, I do not know why but I managed to solve using echo json_encode ( $ response , JSON_UNESCAPED_UNICODE ) ; instead ofprint and escaping special characters in encode instead of mysqli , hope it helps




回答2:


Seems the problem was with the hosting itself, I tried another hosting (arvixe) and everything works as charm. i have no idea why but maybe because it is a payed hosting.




回答3:


Many of the free hosting provider are appending the analytics code at the end, that is the major reason why php files fails.

The best solution for this type of problem is to use

 <?php exit(); ?> 

after the last executable statement, which will explicitly close the PHP file.

When you exit explicitly PHP file, the code will not be appended to the JSON.



来源:https://stackoverflow.com/questions/37746956/why-i-cant-retrieve-data-from-my-webserver-as-json-but-i-can-when-i-test-it-on

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