无法返回正确的json响应android

时间:2013-07-20 12:26:55

标签: java php

我已将php脚本放在服务器上。当我将它与表单一起用于测试时,该脚本工作正常。我已经在我的Android应用程序中添加了php脚本所在的地址,现在当我执行代码时,它不会返回json数据,而是返回它。

07-20 16:42:13.839: I/System.out(2048): displaying <!-- www.freewebhost.com Analytics 
Code -->
<script src="http://www.freewebhost.com"></script>
<noscript>
<a title="Free hosting servers" href="http://www.freewebhost.com">Free servers</a><a title="Free websites hosting server" href="http://www.freewebhost.com">Free websites hosting server</a>
<a title="Free hosting server features" href="http://www.freewebhost.com/serverfeatures/">Free server features</a>
<a title="Free hosting" href="http://www.bugs3.com">Free hosting</a><a title="Page rank" href="http://www.1pagerank.com">Page rank</a>
</noscript>
<script type="text/javascript">  
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24425628-3']);
_gaq.push(['_setDomainName', window.location.host]);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {    var ga = document.createElement('script'); 
  ga.type = 'text/javascript';
  ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript" src="http://www.bugs3.com/ganalytics.js">
</script>
<!-- End Of Analytics Code -->

这是php脚本:

<?php

require("config.inc.php");

if (!empty($_POST)) {

$query = " 
        SELECT 
            id, 
            username, 
            password
        FROM userCredentials 
        WHERE 
            username = :username 
    ";

$query_params = array(
    ':username' => $_POST['username']
);

try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {


    $response["success"] = 0;
    $response["message"] = "Database Error1. Please Try Again!";
    die(json_encode($response));

}

$validated_info = false;


$row = $stmt->fetch();
if ($row) {

    if ($_POST['password'] === $row['password']) {
        $login_ok = true;
    }
}

if ($login_ok) {
    $response["success"] = 1;
    $response["message"] = "Login successful!";
    die(json_encode($response));

} else {
    $response["success"] = 0;
    $response["message"] = "Invalid Credentials!";

    die(json_encode($response));
}
 }
?>

的config.inc.php

<?php 

$host = "db.freewebhost.com"; 
$dbname = "hello";     
$username = "android"; 
$password = "12345"; 


$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 

try 
{ 
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
} 
catch(PDOException $ex) 
{ 
    die("Failed to connect to the database: " . $ex->getMessage()); 
} 

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 

if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
{ 
    function undo_magic_quotes_gpc(&$array) 
    { 
        foreach($array as &$value) 
        { 
            if(is_array($value)) 
            { 
                undo_magic_quotes_gpc($value); 
            } 
            else 
            { 
                $value = stripslashes($value); 
            } 
        } 
    } 

    undo_magic_quotes_gpc($_POST); 
    undo_magic_quotes_gpc($_GET); 
    undo_magic_quotes_gpc($_COOKIE); 
} 

header('Content-Type: text/html; charset=utf-8'); 


session_start(); 

?>

为什么这个脚本不返回json数据?请帮我解决这个问题。我没有PHP经验。

此致

1 个答案:

答案 0 :(得分:0)

标题内容类型应为text / json。在config.inc.php中,您将其设置为text / html:

header('Content-Type: text/html; charset=utf-8');