2016-03-29 11 views
0

Android'den PHP sunucusuna veri aktarımı sağlayan bir uygulama üzerinde çalışıyorum ve neden JSON'ı desteklemediğini bilmiyorum? JSON kodu php doğru olmayabilir daha geçerliysePHP sunucumdaki Unknown JSON hatası

<?php 
JSON.parse(); 
$decode = json_decode($_REQUEST['request']); 
$json = $decode->name; 
header('Content-type:application/json'); 
echo json_encode($json); 
?> 
+2

bazı code..it göstermek olacak problemi bulmak için yardım –

cevap

0

http://jsonlint.com adresinden JSON kontrol edin:

İşte benim kodudur.

Bazı özellikler için kodları göster.

0

Sen android kullanarak aşağıdaki koddan dize olarak Json veri gönderebilir:

BufferedReader reader = null; 

     // Send data 
     try { 

      /* forming th java.net.URL object */ 
      URL url = new URL(this.url); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setRequestProperty("Content-Type", "application/json"); 
      urlConnection.setRequestProperty("Accept", "application/json"); 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.connect(); 

      /* pass post data */ 
      byte[] outputBytes = jsonData.toString().getBytes("UTF-8"); 
      OutputStream os = urlConnection.getOutputStream(); 
      os.write(outputBytes); 
      os.close(); 

      /* Get Response and execute WebService request*/ 
      int statusCode = urlConnection.getResponseCode(); 

      /* 200 represents HTTP OK */ 
      if (statusCode == HttpsURLConnection.HTTP_OK) { 

       inputStream = new BufferedInputStream(urlConnection.getInputStream()); 
       ResponseData= convertStreamToString(inputStream); 

      } else { 

       ResponseData = null; 
      } 

ve php, aşağıdaki kodu ekleyerek verileri alabilirsiniz:

$post_body = file_get_contents('php://input'); 
    $post_body = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($post_body)); 
    $reqData[] = json_decode($post_body); 

    $postData = $reqData[0]; 
    echo $postData->name;