2013-12-17 34 views
0

我想通過php從MySQL獲取單個字段,並在我的android應用中使用它。如何在不使用json的情況下從PHP讀取響應時向android發送單個字段? 或是否有任何教程,可以幫助我,我會感激從HTTP響應中獲取單個字段

這裏是我的代碼

public Boolean postData(String a,String b) { 



        response = null;   

        String response = null; 

        try 
        { 
         // url = new URL("http://"+"URL"+"/new/check2.php"); 
         ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
         postParameters.add(new BasicNameValuePair("check",x)); 
         postParameters.add(new BasicNameValuePair("username", a)); 
         postParameters.add(new BasicNameValuePair("password", b)); 

         response = CustomHttpClient.executeHttpPost("http://"+"URL"+"/new/checkedited.php",postParameters); 
        // result = response.toString(); 
        result = result.replaceAll("\\s+", ""); 



        } 
        catch(Exception e) 
        { 
         e.printStackTrace(); 
        } 
        return true; 
      } 

PHP

<?php 
    $host=""; // Host name 
    $user=""; // Mysql username 
    $pswd=""; // Mysql password 
    $db="pet_home"; // Database name 
    //$tbl_name="users"; // Table name 

    $conn = mysql_connect($host, $user, $pswd); 
    mysql_select_db($db, $conn); 
    $username=$_POST['username']; 
    $password=$_POST['password']; 
    $result=mysql_query("select * from users where username='$username' and  
    password='$password'")or die (mysql_error()); 
    $count=mysql_num_rows($result); 
    $row=mysql_fetch_array($result);  
      if ($count > 0){ 
      echo "\n"; 
      echo $row['filter_st']; 
      echo "\n"; 
      echo $row['heat_st']; 
      echo "\n"; 
      echo $row['led_st'];    
      }else{ 
      echo 0; 

      }  

     ?> 
+0

您還應該閱讀有關清理數據庫輸入的教程。 – skrilled

+0

但是你需要返回這3個值......如果沒有,你可以爲該值做一個選擇,然後回顯$ row ['myvalue']; – Hackerman

+0

這就是我正在尋找從每個單獨的答覆獲得這些值 – user1928775

回答

0

只是呼應了單場,再沒有解析器,無JSON沒有什麼...

例如,如果你想'heat_st':(只有一個回聲,因爲回聲是你的手機得到的迴應)

echo $row['heat_st']; 

然後到你的Android應用程序的響應將是隻是一個字符串,它就是你想要的結果。(你可以很容易地將其轉換爲int例如在Java中,如果你需要)

,如果您需要多個領域,JSON是要走的路。