从HTTP响应中获取单个字段
问题描述:
我想通过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;
}
?>
答
只是呼应了单场,再没有解析器,无JSON没有什么...
例如,如果你想'heat_st':(只有一个回声,因为回声是你的手机得到的回应)
echo $row['heat_st'];
然后到你的Android应用程序的响应将是只是一个字符串,它就是你想要的结果。(你可以很容易地将其转换为int例如在Java中,如果你需要)
,如果您需要多个领域,JSON是要走的路。
您还应该阅读有关清理数据库输入的教程。 – skrilled
但是你需要返回这3个值......如果没有,你可以为该值做一个选择,然后回显$ row ['myvalue']; – Hackerman
这就是我正在寻找从每个单独的答复获得这些值 – user1928775