gpt4 book ai didi

mysql - 从 Android 连接 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 00:59:29 25 4
gpt4 key购买 nike

好吧,这是我用来访问 getUser.php 以从我的应用程序中的 MySQL 数据库检索用户详细信息的代码 fragment :

String result = "";

//the year data to send

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("uid","demo"));

//http post
try{
HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://192.xxx.xx.xxx/getUser.php");

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
InputStream is = null;
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 result "+e.toString());
}

//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("fname")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("dob")
);
}

}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}

}

此 fragment 摘自 http://helloandroid.com

一切都配置良好:MySQL Db、带有 FASTCGi 的 IIS、PHP 工具和驱动程序。从浏览器使用 url 调用时甚至是下面的脚本:http://192.xxx.xx.x.xxx/getUser.php?uid=demo工作正常,但在 android 中返回错误 java.lang.NullPointerExceptionorg.json.JSONEXCEPTION: End of input at character 0

<?php
mysql_connect("myhost","username","pwd");
mysql_select_db("mydb");

$q=mysql_query("SELECT * FROM userinfo WHERE uid ='".$_REQUEST['uid']."'");

while($e=mysql_fetch_assoc($q))
$output[]=$e;

print(json_encode($output));

mysql_close();
?>

有人可以在这部分提供帮助吗?

问候,哈迪克小姐

最佳答案

您正在使用 HttpPost 对象,这意味着您正在发起一个后请求!您确定不想执行 HttpGet 请求吗?我遇到了同样的问题并切换到 HttpGet 解决了我的问题!

关于mysql - 从 Android 连接 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4316763/

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