如何连接Android模拟器与当地mysql数据库 [英] How to connect Android emulator with local mysql database

查看:1048
本文介绍了如何连接Android模拟器与当地mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Android模拟器本地连接MySQL数据库。我用HTTP GET和POST方法来访问数据从谷歌云SQL使用App Engine,但我想它在本地使用phpmyadmin的连接。

我用以下时,code这表明吐司连接失败。

 字符串结果=;
        //一年要发送的数据
        ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
        nameValuePairs.add(新BasicNameValuePair(姓名,哈马德));

        // HTTP POST
        尝试{
                HttpClient的HttpClient的=新DefaultHttpClient();
                HttpPost httppost =新HttpPost(HTTP://localhost/myApp/read_data.php);
                httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
                HTT presponse响应= httpclient.execute(httppost);
                HttpEntity实体= response.getEntity();
                是= entity.getContent();

                Log.e(log_tag,连接成功);
                Toast.makeText(getApplicationContext(),通,Toast.LENGTH_SHORT).show();
        }赶上(例外五){
                Log.e(log_tag,错误的HTTP连接+ e.toString());
                Toast.makeText(getApplicationContext(),连接失败,Toast.LENGTH_SHORT).show();

        }
        //转换响应串
        尝试{
                的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
                StringBuilder的SB =新的StringBuilder();
                串线= NULL;
                而((行= reader.readLine())!= NULL){
                        sb.append(行+\ N);
                        Toast.makeText(getApplicationContext(),输入读通,Toast.LENGTH_SHORT).show();
                }
                is.close();

                结果= sb.toString();
        }赶上(例外五){
               Log.e(log_tag,错误转换结果+ e.toString());
            Toast.makeText(getApplicationContext(),输入读取失败,Toast.LENGTH_SHORT).show();

        }

        //解析JSON数据
        尝试{
                JSONArray jArray =新JSONArray(结果);
                的for(int i = 0; I< jArray.length();我++){
                       的JSONObject json_data = jArray.getJSONObject(ⅰ);
                        Log.i(log_tag,I​​D:+ json_data.getInt(USER_ID)+
                                用户名:+ json_data.getString(用户名)+
                                姓名:+ json_data.getInt(姓名)+
                                城市:+ json_data.getInt(城市)
                        );
                        Toast.makeText(getApplicationContext(),JsonArray通,Toast.LENGTH_SHORT).show();
               }

        }赶上(JSONException E){
                Log.e(log_tag,错误分析数据+ e.toString());
                Toast.makeText(getApplicationContext(),JsonArray失败,Toast.LENGTH_SHORT).show();
        }
    }

  }
 

解决方案

如果您在模拟器中运行这个本地主机:3306 将无法正常工作。取代,与运行MySQL机器的IP地址。因此,举例来说,如果你的本地开​​发计算机(运行MySQL)使用IP 192.168.0.10,更改为 192.168.0.10:3306

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

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

编辑:(如下图)沃伦的技巧使我的细节在Android文档。可能要坚持用10.0.2.2,如果你不与你的操作系统防火墙想管闲事。

I want to connect mysql database locally with android emulator. 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..

when i use following code it show Toast for connection failed

        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. Replace that with the IP address of the machine running MySQL. So for example if your local dev machine (running MySQL) uses IP 192.168.0.10, change that to 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). You obviously don't have the SQL service running on the android so this doesn't make sense.

Also, depending on your OS configuration, you may have to open port 3306 in your firewall.

Edit: Warren's tip (below) leads me to the details in the Android docs. May want to stick with 10.0.2.2 if you don't want to mess around with your OS' firewall.

这篇关于如何连接Android模拟器与当地mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆