如何将Android模拟器与本地mysql数据库连接

时间:2012-01-01 14:55:22

标签: java android emulation

我想在本地连接mysql数据库与android模拟器。我使用http GET和POST方法通过app引擎访问Google Cloud SQL中的数据,但我想使用phpmyadmin将其与本地连接..

当我使用以下代码时,它显示Toast连接失败

        String result = "";
        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("name","Hammad"));

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://localhost/myApp/read_data.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

                Log.e("log_tag", "connection success ");
                Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
                Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

        }
        //convert response to string
        try{
                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");
                        Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
                }
                is.close();

                result=sb.toString();
        }catch(Exception e){
               Log.e("log_tag", "Error converting result "+e.toString());
            Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();

        }

        //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("user_id")+
                                ", Username: "+json_data.getString("username")+
                                ", Name: "+json_data.getInt("name")+
                                ", City: "+json_data.getInt("city")
                        );
                        Toast.makeText(getApplicationContext(), "JsonArray pass", Toast.LENGTH_SHORT).show();
               }

        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
                Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
        }
    }

  }

1 个答案:

答案 0 :(得分:5)

如果您在模拟器localhost:3306内运行此功能将无效。将其替换为运行MySQL的计算机的IP地址。例如,如果您的本地开发机器(运行MySQL)使用IP 192.168.0.10,请将其更改为192.168.0.10:3306

只是扩展一下 - 当您尝试在模拟器(甚至设备)中访问http://localhost:3306时,它会尝试连接到环回地址上的端口3306(在模拟器/设备上)。你显然没有在android上运行SQL服务,所以这没有意义。

此外,根据您的操作系统配置,您可能必须在防火墙中打开端口3306。

编辑:Warren的提示(下方)引导我访问Android文档中的details。如果你不想乱用你的操作系统防火墙,可能想坚持使用10.0.2.2。