2014-10-03 36 views
0

我有選擇查詢mysql它給我輸出像{"flag":"true"}如何提取並比較HttpPost響應字符串?

在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

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(); 

Android的代碼,現在我要檢查響應與一些字符串,這樣我可以做進一步的操作。

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? 
+0

這可能是有時間閱讀教程和/或應用一些代碼。無論如何,這個問題可能是「如何在Android中訪問/讀取JSON響應?」,這遠非罕見。 (但是請檢查*響應,當前發佈的PHP看起來無效,這完全是另一個問題。) – user2864740 2014-10-03 16:00:38

回答

1

這是你如何提取從InputStream響應String

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