2013-07-20 59 views
-1

我已經把PHP腳本放在服務器上。當我使用表單進行測試時,該腳本正常工作。我已經添加了php腳本位於我的android應用程序中的地址,現在,當我執行代碼時,它不會返回json數據,而是返回此值。無法返回正確的JSON響應android

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的經驗。

Regards

+0

似乎無論如何,web主機都會成爲問題,因爲它會自動附加一堆代碼。找一個不惹你輸出的主機,然後開始調試^^ – JimL

+0

你是否知道其他主機不會造成這樣的問題 –

回答

0

標題內容類型應該是text/json。在confing.inc.php中,將其設置爲text/html:

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

我該怎麼做 –

+0

不好。仍然是相同的結果 –