2012-03-29 45 views
3

我試圖JSON發送到PHP和存儲發送 的數據,但我不能做出PHP它的工作發送JSON採用Android

我的Android代碼

public void sendToServer(String txt) throws JSONException{ 

      String path = "http://10.0.0.6:8888/json.php"; 

      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
                        // Limit 
      HttpResponse response; 
      JSONObject json = new JSONObject(); 

      try { 
       HttpPost post = new HttpPost(path); 
       json.put("text", txt); 
       Log.i("jason Object", json.toString()); //This print the data perfectly 
       post.setHeader("json", json.toString()); 
       post.setHeader("Accept", "application/json"); 

       Log.i("Done", "DONE 1"); 

       StringEntity se = new StringEntity(json.toString()); 

       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, 
         "application/json")); 
       Log.i("Done", "DONE 2"); 

       ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("myjson", json.toString())); 
       post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 


       Log.i("Done", "DONE 3"); 

       response = client.execute(post); 

       /* Checking response */ 
       if (response != null) { 
        InputStream in = response.getEntity().getContent(); // Get the 
                     // data in 
                      // the 
                      // entity 
        String a = convertStreamToString(in); 
        Log.i("Read from Server", a); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
        } 

和我的PHP代碼

<?php 

$decoded = json_decode(stripslashes($_POST['myjson'])); 
if($decoded) 
$con = mysql_connect('localhost','root','root') 
     or die('Cannot connect to the DB'); 
mysql_select_db('deaf',$con); 
mysql_query("INSERT INTO text 
VALUES ('".$decoded->{'text'}"')"); 
mysql_close($con); 
$posts = array(1); 
header('Content-type: application/json'); 
echo json_encode(array('posts'=>$posts)); 

else 
echo "error"; 
    ?> 

insert語句不工作,我花了幾個小時試圖修復它,沒有什麼工作 請幫幫我,謝謝

+0

PHP讓我害怕。 – 2012-03-29 22:03:00

+0

@pst me too actually :) – Osaed 2012-03-29 22:04:49

+0

你在Java的in變量中得到了什麼?用'if else {}'替換你的模板'if'。它只適用於1行代碼,如果你在這裏有10行。它不會工作。 – 2012-03-29 22:10:15

回答

0

這有幾個問題。

  1. 你似乎只是將json填充到代碼的任意部分(json頭文件,一個從未使用的StringEntity,myjson url編碼的表單字段)。我會建議簡單地將請求的主體作爲JSON文檔。然而,還有其他方法,所以我們試着讓myjson工作。如果你這樣做,我會改名爲document。它不需要在標題中,並且可以刪除StringEntity。
  2. stripslashes的正確用途非常少。您不必在表單輸入中運行它。如果這樣做,那可能意味着啓用了magic_quotes。 magic_quotes已從PHP(5.4及更高版本)中刪除,不應使用。
  3. 我建議你使用準備好的語句。如果沒有,你應該使用mysql_real_escape_string。否則,您將很容易受到SQL注入的影響。
  4. 您可以簡化對象訪問。因此,代碼應該是:  

    mysql_query("INSERT INTO text VALUES ('" . mysql_real_scape_string($decoded->text) . "')");

如果插入語句失敗,請張貼錯誤是什麼,包括mysql_error

+0

感謝您的答覆,只是一個問題,myjson或文件,如何使用它的json對象? – Osaed 2012-03-29 22:33:23

+0

我的意思是在客戶端和服務器上用'document'替換'myjson'。 – 2012-03-29 22:39:48

+0

我知道,但JSON看起來怎麼樣 {「text」:「sometext」} 其中是此對象中的文檔還是僅用於處理? 對不起,但我真的很新:) – Osaed 2012-03-29 22:43:13