【问题标题】:Connect MySQL database from Android从 Android 连接 MySQL 数据库
【发布时间】:2010-11-30 17:47:34
【问题描述】:

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

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());
        }

}

这个sn-p取自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();
?>

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

问候, 哈迪克先生

【问题讨论】:

  • 请发布异常的完整堆栈跟踪,并指出它们引用了哪些代码行。
  • 您是否在运行模拟器的同一系统上运行您的服务器。
  • 本教程对我有用:itweb-projects.com/wordpress/…

标签: mysql android


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2011-06-10
    相关资源
    最近更新 更多