gpt4 book ai didi

php - Android、PHP 和 JSON 之间的多个查询问题

转载 作者:行者123 更新时间:2023-11-29 07:04:28 26 4
gpt4 key购买 nike

我必须执行一些查询才能从数据库中获取我需要的所有信息。大约有 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();
?>

这是我的安卓代码:

 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

提前致谢。

最佳答案

你的 php 代码:

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

你在这里犯了错误。我认为您的输出类似于:{{{"":"","":""}}}

你需要这样写你的代码:

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

用这段代码代替上面的代码试试。现在退房。希望这会有所帮助。

关于php - Android、PHP 和 JSON 之间的多个查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8178744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com