我试图通过从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值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)