繁体   English   中英

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

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

我从仿真器连接到本地Web服务器时遇到问题。 我可以在模拟器外部连接到它。 模拟器获取: 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;
    }

尝试改为连接到http://10.0.2.2:8080

10.0.2.2 IP地址是仿真器中的特殊地址,它将透明地连接到主机开发计算机的localhost( 127.0.0.1 )。

如果您在仿真器中使用localhost127.0.0.1 ,则它将引用内部仿真环回接口。 如果您有另一个程序正在仿真器中运行,并且在同一端口上充当TCP或UDP服务器,这将很有用。 但是,在您的特定情况下,您确实想“摆脱沙箱”。

官方文档提供了更多信息(和示例)。

您可能使用了错误的网络地址来访问Web服务。

http://localhost转到Android模拟器中的设备。 使用10.0.2.2访问主机回送接口(即,开发计算机上的127.0.0.1)。

查看Android模拟器网络

编辑:您的异常说http //localhost;808 ,但您的主机名应为http //localhost:808并带有:而不是;

暂无
暂无

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

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