简体   繁体   English

无法从我的Android模拟器连接到本地Web服务

[英]Cannot connect to a local web service from my android emulator

I am having a problem connecting from the emulator to a local web server. 我从仿真器连接到本地Web服务器时遇到问题。 I can connect to it outside the emulator. 我可以在模拟器外部连接到它。 Emulator gets: org.apache.http.conn.httphostconnectexception connection to http //localhost;808 refused 模拟器获取: org.apache.http.conn.httphostconnectexception connection to http //localhost;808 refusedorg.apache.http.conn.httphostconnectexception connection to http //localhost;808 refused

protected String doInBackground(String... args) {
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products,
                    "GET", params);
            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());
            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        String name = c.getString(TAG_NAME);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_NAME, name);

                        // adding HashList to ArrayList
                        productsList.add(map);
                    }
                } else {
                    // no products found
                    // Launch Add New product Activity
                    Intent i = new Intent(getApplicationContext(),
                            NewProductActivity.class);
                    // Closing all previous activities
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
             System.out.println("Got an IOException: " + e.getMessage());
            // TODO: handle exception
        }
        return null;
    }

Try connecting to http://10.0.2.2:8080 instead. 尝试改为连接到http://10.0.2.2:8080

The 10.0.2.2 IP address is special within the emulator and will transparently connect to your host development machine's localhost ( 127.0.0.1 ). 10.0.2.2 IP地址是仿真器中的特殊地址,它将透明地连接到主机开发计算机的localhost( 127.0.0.1 )。

If you use localhost or 127.0.0.1 within the emulator, this will refer to the internal emulated loopback interface instead. 如果您在仿真器中使用localhost127.0.0.1 ,则它将引用内部仿真环回接口。 This is useful if you have another program running in the emulator, acting as a TCP or UDP server at the same port. 如果您有另一个程序正在仿真器中运行,并且在同一端口上充当TCP或UDP服务器,这将很有用。 However, in your specific case, you really want to get 'out of the sandbox'. 但是,在您的特定情况下,您确实想“摆脱沙箱”。

The official documentation has more information (and examples). 官方文档提供了更多信息(和示例)。

You are probably using the wrong network address to access the web service. 您可能使用了错误的网络地址来访问Web服务。

http://localhost goes to the device in an Android emulator. http://localhost转到Android模拟器中的设备。 Use 10.0.2.2 to access your host loopback interface (ie, 127.0.0.1 on your development machine). 使用10.0.2.2访问主机回送接口(即,开发计算机上的127.0.0.1)。

See Android Emulator Networking 查看Android模拟器网络

EDIT: Your exception says http //localhost;808 , but your hostname should be http //localhost:808 with a : instead of ; 编辑:您的异常说http //localhost;808 ,但您的主机名应为http //localhost:808并带有:而不是; .

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

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