2016-03-29 8 views
0

AndroidからPHPサーバーへのデータ転送を許可するアプリケーションで作業していますが、なぜJSONをサポートしないのかわかりません。 JSONをPHPのコードが正しくない可能性がより有効である場合はPHPサーバーで不明なJSONエラー

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

は、いくつかのcode..it意志を示し問題を見つけるのに役立つ –

答えて

0

http://jsonlint.com であなたのJSONを確認してください。

は、ここに私のコードです。

詳細については、いくつかのコードを表示してください。

0

あなたは、次のコードを使用してアンドロイドから文字列としてJSONデータを送信することができます

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; 
      } 

とPHPで、次のコード追加してデータを取得することができます:

$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; 
関連する問題