java - Android模拟器与本地mysql数据库连接的方法

标签 java android emulation

我想用android模拟器本地连接mysql数据库。我使用 http GET 和 POST 方法通过应用程序引擎从 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();
        }
    }

  }

最佳答案

如果您在模拟器中运行此命令,localhost:3306 将不起作用。将其替换为运行 MySQL 的计算机的 IP 地址。例如,如果您的本地开发计算机(运行 MySQL)使用 IP 192.168.0.10,请将其更改为 192.168.0.10:3306

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

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

编辑:沃伦的提示(如下)引导我到 details在 Android 文档中。如果您不想破坏操作系统的防火墙,可能需要坚持使用 10.0.2.2。

关于java - Android模拟器与本地mysql数据库连接的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8693665/

相关文章:

android - Cordova 是否支持 webkitSpeechRecognition?

java - JNI 应用程序的可移植 Makefile

android - ADB touchevent 不依赖于计算机工作

java - "Not in"使用 JPA 标准的约束

java - Android RunOnUIThread 不会永久改变 View 可见性

c# - Android 模拟器未运行 - Visual Studio 2017

android - Titanium 3.1.0 - 模拟器进程退出,代码为 1 - 无法编译 APK

java - Microemu 在 JavaSE 上运行 MIDlet

Ubuntu 中的 Java OutofMemory 错误,即使有足够的可用内存

java - 由 : org. hibernate.LazyInitializationException 引起:未能延迟初始化角色集合: