我必须做一些查询才能从数据库中获取所需的所有信息。这是大约7个查询。如果我只使用一个它可以完美地工作,但是当我尝试添加更多时,我会收到错误。
这是我的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":"gusti@upr.edu","5":"123443","4":"gusti@upr.edu"},{"3":"23","2":"passwrd","inst_serial":"23","passwrd":"passwrd","username":"paramedic","1":"paramedic","paramedic_license":"123111","paramedic_serial":"111","0":"111","email":"paramedic@aki.com","5":"123111","4":"paramedic@aki.com"},{"3":"23","2":"bb","inst_serial":"23","passwrd":"bb","username":"aa","1":"aa","paramedic_license":"1234","paramedic_serial":"138","0":"138","email":"email@email.com","5":"1234","4":"email@email.com"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
提前致谢。
答案 0 :(得分:2)
你在php中的代码:
print(json_encode(array($output, $output2)));
你这里犯了错误。我认为您的输出类似于:{{{"":"","":""}}}
您需要编写如下代码:
$output3[]=array_merge($output,$output2);
print(json_encode($output3));
将此代码替换为上述代码并尝试。现在退房。希望这会有所帮助。