gpt4 book ai didi

php - android与MYSQL数据库连接返回null值

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:44 25 4
gpt4 key购买 nike

我试图通过从 Android 传递参数值来从我的 MySQL 数据库中检索特定数据,然后在查询的 PHP 脚本中读取该值以返回数据。

我运行应用程序时,由于返回的结果值为null,出现解析数据异常的错误?

为什么结果为空?错误是来自 PHP 脚本还是来 self 的 java 代码?

请帮帮我

提前致谢!

城市.php:

  <?php
mysql_connect("localhost","username","password");
mysql_select_db("Countries");
$sql=mysql_query("select City_Population from City where Name= "'.$_REQUEST['Name']."'");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

Blockquote

Java 类:

       public class ConnectActivity extends ListActivity {

String add="http://10.0.2.2/city.php";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

new Connect().execute();

}

private class Connect extends AsyncTask<Void,Void,String>
{
private String result = "";
private InputStream is=null;
private String city_name="London";
protected String doInBackground(Void... params) {
try
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Name",city_name));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(add);
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());
}


//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),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());
}


return result;
}
protected void onPostExecute(String result){

try{
JSONArray jArray = new JSONArray( result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++)
{
json_data = jArray.getJSONObject(i);
int population=json_data.getInt("City_Population");

TextView City_Name =(TextView)findViewById(R.id.city_name);
TextView City_population=(TextView)findViewById(R.id.city_pop);
City_Name.setText(json_data.getString(city_name));
City_population.setText(population+" " );
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}


}
}

}

最佳答案

     <?php
$name=$_POST['NAME'];
mysql_connect("localhost","username","password");
mysql_select_db("Countries");
$sql=mysql_query("select City_Population as citypop from City where Name='$name' ");
while($row=mysql_fetch_assoc($sql))
$output=$row['citypop'];
print(json_encode($output));
mysql_close();
?>

你试试这个肯定会成功。

关于php - android与MYSQL数据库连接返回null值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025655/

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