2013-04-09 81 views
1

我在製作Android應用程序,並試圖從遠程數據庫中檢索數據。從遠程數據庫獲取數據Android

我的問題是如何檢查查詢結果是否包含數據並且不是空的?

我正在使用JSON,但我對它很陌生。這裏是我的代碼:

public class MainActivity extends Activity { 

    private TextView txt; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     LinearLayout rootLayout = new LinearLayout(getApplicationContext()); 
     txt = new TextView(getApplicationContext()); 
     rootLayout.addView(txt); 
     setContentView(rootLayout); 

     txt.setText("Connexion..."); 
     txt.setText(getServerData(strURL)); 

    } 

    public static final String strURL = "http://.../marecherche/adresse.php"; 

    private String getServerData(String returnString) { 
     InputStream is = null; 
     String result = ""; 

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("adresse","adr casa")); 

     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(strURL); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
     }catch(Exception e){ 

      Log.e("log_tag", "Error in http connection " + e.toString()); 
     } 

     try{ 
      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"); 
      } 
      is.close(); 
      result=sb.toString(); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     try{ 


      JSONArray jArray = new JSONArray(result); 
      for(int i=0;i<jArray.length();i++){ 
       JSONObject json_data = jArray.getJSONObject(i); 

       Log.i("log_tag","raison sociale: "+json_data.getString("raison_social")+ 
         ", Adresse: "+json_data.getString("adresse") 
         ); 

       returnString += "\n\t" + jArray.getJSONObject(i); 
      } 
     }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 
     return returnString; 
    } 
} 

和PHP文件:

<?php 
mysql_connect("localhost", "root", "passwd"); 
mysql_select_db("dbname"); 
$mots = explode(' ', $_REQUEST['adresse']); 
$like = array(); 
foreach ($mots AS $mot) { 
    $like[] = ' "%' . mysql_real_escape_string($mot) . '%" '; 
} 

$condition = implode(' OR adresse LIKE ', $like); 

$sql = mysql_query("SELECT * FROM entreprise WHERE adresse like " . $condition); 
while ($row = mysql_fetch_assoc($sql)) 
    $output[] = $row; 
print(json_encode($output)); 
mysql_close(); 
?> 

回答

0

My question is how can I check if the query result contains data and is not empty?

=>檢查result字符串創建JSONArray前空或不是。

1

你可以只創建你自己的返回檢查,一些

PHP文件:

if (is_array($output)) { 
    print(json_encode($output)); 
} else 
    echo "empty"; 
} 

的Java:

if (result.equals("empty")) { 
    return; 
} 
JSONArray jArray = new JSONArray(result); 
// etc 
+0

謝謝你的回答,itried那但它給了我錯誤: – 2013-04-10 09:43:20

+0

謝謝你的答案,它的工作,但爲測試:如果(is_array($輸出))總是正確的,我將其更改爲!empty($ output)非常感謝 – 2013-04-10 10:11:04