Zakaria Jawas Zakaria Jawas - 23 days ago 11
JSON Question

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

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.



enter image description here

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>

Answer Source

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.