php – 从android和MYSQL数据库之间的连接返回的null值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 从android和MYSQL数据库之间的连接返回的null值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2697字,纯文字阅读大概需要4分钟。
内容图文
![php – 从android和MYSQL数据库之间的连接返回的null值](/upload/InfoBanner/zyjiaocheng/905/08e28129a22b4183b6e1b8d499a6c269.jpg)
我试图通过从Android传递参数的值从MySQL数据库中检索特定数据,然后在查询中的PHP脚本中读取此值以返回数据.
当我运行应用程序时,出现错误解析数据异常,因为返回的结果值为null?
为什么结果为null?是来自PHP脚本还是来自我的java代码的错误?
请帮我
提前致谢!
city.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值全部内容,希望文章能够帮你解决php – 从android和MYSQL数据库之间的连接返回的null值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。