简体   繁体   English

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

[英]How to connect Android emulator with local mysql database

I want to connect mysql database locally with android emulator. 我想在本地将mysql数据库与android仿真器连接。 I used http GET and POST methods for accessing data from Google Cloud SQL with app engine but i want to connect it with locally using phpmyadmin.. 我使用http GET和POST方法通过带有应用程序引擎的Google Cloud SQL访问数据,但我想使用phpmyadmin将其与本地连接。

when i use following code it show Toast for connection failed 当我使用以下代码时,它显示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();
        }
    }

  }

If you're running this within the emulator localhost:3306 won't work. 如果您在模拟器中运行此命令,则localhost:3306将无法工作。 Replace that with the IP address of the machine running MySQL. 将其替换为运行MySQL的计算机的IP地址。 So for example if your local dev machine (running MySQL) uses IP 192.168.0.10, change that to 192.168.0.10:3306 . 因此,例如,如果您的本地开发机(运行MySQL)使用IP 192.168.0.10,请将其更改为192.168.0.10:3306

Just to expand a bit - when you attempt to access http://localhost:3306 within the emulator (or even on a device) it tries to connect to the port 3306 on the loopback address (on the emulator/device). 只是扩展一下-当您尝试访问仿真器(甚至在设备上)中的http:// localhost:3306时 ,它尝试连接到回送地址(仿真器/设备上)上的端口3306。 You obviously don't have the SQL service running on the android so this doesn't make sense. 您显然没有在android上运行SQL服务,因此这没有任何意义。

Also, depending on your OS configuration, you may have to open port 3306 in your firewall. 另外,根据您的操作系统配置,您可能必须在防火墙中打开端口3306。

Edit: Warren's tip (below) leads me to the details in the Android docs. 编辑:沃伦的秘诀(下)将我带到Android文档中的详细信息 May want to stick with 10.0.2.2 if you don't want to mess around with your OS' firewall. 如果您不想弄乱操作系统的防火墙,不妨坚持使用10.0.2.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM