2014-02-26 3 views
0

내 JSON 파일을 표시 할 수없는 이유를 다른 사람이 도울 수 있습니까? 초보자를위한 JSON. 이것은 내 코드이며, 유일한 빈 문서를 보여줍니다. 이 웹 사이트 http://contohprogramandroid.blogspot.com/2013/10/contoh-program-android-aplikasi-wisata.html에서이 자습서를 배우고 있습니다. 정말 고맙습니다.json 파일을 가져 오는 중 PHP 오류가 발생했습니다.

코드를 실행할 때 내 이미지. enter image description here

//this is the code webservice.php 

<?php 
class Database { 
private $host = "localhost"; 
private $user = "root"; 
private $pass = ""; 
private $db = "wisata_jogja"; 
private $conn; 

// constructor 
function __construct() { 
    try{ 
     $this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db, $this->user, $this->pass); 
    }catch(PDOException $e) { 
     echo "error pdo $e"; 
    } 
} 


public function showAllData($table) { 
    $sql ="SELECT * FROM $table"; 
    $q = $this->conn->query($sql) or die("Failed !!"); 
    while ($r = $q->fetch(PDO::FETCH_ASSOC)) { 
     $data[] = $r; 
    } 
    return $data; 
} 

} 

$database = new Database(); 
$response = array(); 

if (isset($_GET['get']) && $_GET['get']=='lokasi') { 
$response['location'] = array(); 

foreach ($database->showAllData('lokasi') as $value) { 
    $kode = array(); 
    extract($value); 

    $kode['id'] = $id; 
    $kode['nama'] = $nama; 
    $kode['alamat'] = $alamat; 
    $kode['gambar'] = $gambar; 
    $kode['lat'] = $lat; 
    $kode['lng'] = $lng; 
    array_push($response['location'], $kode); 
} 
echo json_encode($response); 
} 
?> 
+1

마우스 오른쪽 버튼으로 클릭하고 '표시 source'을 선택, 당신은 아마 당신의 출력을 볼 수 있습니다. Chrome은 플러그인이없는 형식의 JSON 데이터 표시를 지원하지 않습니다. –

+1

은 Chrome에서 F12를 사용하여 개발자 콘솔을 엽니 다. 응답 본문에서는 플러그인이 필요없이 JSON을 올바르게 확인할 수 있습니다. – NDM

+0

당신은 또한 CLI (command line interface) 모드에서 PHP 코드를 실행할 수 있습니다. 의심 스러울 때 선호하는 해결책입니다 ...'$ php script.php' ... – MarcoS

답변

0
if (isset($_GET['get']) && $_GET['get']=='lokasi') { 

따라서, 스크립트가 출력 조건 이후 어떤 JSON이 충족되지 않을 것이다 : 출력귀하의 조건이 있습니다.

캡쳐 화면에서 GET 매개 변수가 누락되었습니다. 브라우저에서

는 URL에 매개 변수를 추가 :

http://localhost/wisata/webservice.php?get=lokasi 

그것은 조건이 참이 아닌 경우 다른 작업을 수행하는 것이 좋습니다.

당신은 당신 때문에 항상 echo 일이 무슨 일이 일어나고 있는지 알고 있어야합니다 :

if (isset($_GET['get']) && $_GET['get']=='lokasi') { 
    //.............. 
    echo json_encode($response); 
}else{ 
    echo "cannot output JSON data: parameter is missing!!; 
} 
0

당신은 당신이 당신의 스크립트에 GET 변수 "lokasi"을 통과해야 있습니까?

그렇지 않으면 경우 조건을 통과하지 것이다

경우 (는 isset ($ _) [ '수'] GET & & $ _GET [ '수'] == 'lokasi')

당신은 예를 들어, 코드가 멀리 간다 확인하려면 변수 또는 경우 조건 내부에는 foobar 데이터를 덤프 시도하여이를 확인할 수 있습니다

if(isset($_GET['get']) && $_GET['get']=='lokasi') { 
    $response['location'] = array(); 

    $myTest = array('test'); 
    var_dump($myTest); // Should display something on screen 

    // The rest of your code here 
관련 문제