如何提取和比较HttpPost响应字符串?

时间:2014-10-03 15:59:26

标签: java php android mysql http-post

我在mysql中选择了一个查询,它提供了{"flag":"true"}

之类的输出

在android代码中,如何将此响应与任何字符串进行比较?

select.php代码

<?php
    $host='domain.com';
    $uname='tempdata';
    $pwd='tempdata';
    $db="tempdata";

    $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
    mysql_select_db($db,$con) or die("db selection failed");


    $r=mysql_query("select * from test where message='hello'",$con);

    while($row=mysql_fetch_array($r))
    {
        $flag[name]="true";
    }

    print(json_encode($flag));
    mysql_close($con);
?>
MainActivity.java中的android代码

HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.myweb.com/select.php");
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

现在我想用一些字符串检查响应,以便我可以进一步操作。

for example
If response comes `true` then send email else not.

for that i need to check and compare for true value , how can I do so in android code? 

1 个答案:

答案 0 :(得分:1)

这是String 提取回复 InputStream的方式:

// load your data...

InputStream is = ...;

BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
}

if (is != null)
    is.close();

String result = sb.toString();

然后,您可以比较结果String,例如:

if(result.equals(...)) {
    // do something
}