2011-11-18 57 views
1

我必須做一些查詢才能從數據庫中獲取所需的所有信息。這是大約7個查詢。如果我只用一個就能完成,但是當我嘗試添加更多的時候,我得到一個錯誤。Android,PHP和JSON之間的多個查詢的問題

這是我的PHP代碼。

<?php 
    //connection etc 

$sql="SELECT * FROM paramedic"; 
$result=mysql_query($sql) or die(mysql_error()); 

$sql2="SELECT * FROM doctor"; 
$result2=mysql_query($sql2) or die(mysql_error()); 

while($row=mysql_fetch_array($result)) 
$output[]=$row; 

while($rows=mysql_fetch_array($result2)) 
$output2[]=$rows; 


    print(json_encode(array($output, $output2))); 

    mysql_close(); 
    ?> 

這裏是我的Android代碼:

btnLogin.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Check Login 
      String username = etUsername.getText().toString().trim(); 
      String passwrd = etPassword.getText().toString().trim(); 

      try{ 
       httpclient=new DefaultHttpClient(); 
       httppost= new HttpPost("websitegoesher.php"); 

       nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("user1",username)); 
       nameValuePairs.add(new BasicNameValuePair("pass1",passwrd));     

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       response=httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
      }catch(Exception e){ 
       Log.e("log_tag", "Eror at httpost "+e.toString()); 
      } 

      //Convert response to string     
      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 "+e.toString()); 
      } 

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

        if(!json_data.getString("paramedic_serial").equals(null)) 
        Log.i("log_tag","paramedic_license:   "+json_data.getString("paramedic_serial")); 
       // if(!json_data.getString("dr_serial").equals(null))   Log.i("log_tag","dr_serial:   "+json_data.getString("dr_serial")); 



       } 
      }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 

所以當我收到並嘗試運行它,我得到這個錯誤

11-18 02:26:28.905: E/log_tag(27401): Error parsing data org.json.JSONException: Value [{"3":"1","2":"passwrd","inst_serial":"1","passwrd":"passwrd","username":"carlis","1":"carlis","paramedic_license":"123443","paramedic_serial":"100","0":"100","email":"[email protected]","5":"123443","4":"[email protected]"},{"3":"23","2":"passwrd","inst_serial":"23","passwrd":"passwrd","username":"paramedic","1":"paramedic","paramedic_license":"123111","paramedic_serial":"111","0":"111","email":"[email protected]","5":"123111","4":"[email protected]"},{"3":"23","2":"bb","inst_serial":"23","passwrd":"bb","username":"aa","1":"aa","paramedic_license":"1234","paramedic_serial":"138","0":"138","email":"[email protected]","5":"1234","4":"[email protected]"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject 

在此先感謝。

回答

2

你的代碼在PHP:

print(json_encode(array($output, $output2))); 

您在這裏犯錯。我覺得你的輸出是一樣的東西:{{{"":"","":""}}}

你需要編寫的代碼是這樣的:

$output3[]=array_merge($output,$output2); 
print(json_encode($output3)); 

將這個代碼,而不是上面和嘗試。現在結賬。希望這可以幫助。