java ECONNREFUSED(连接被拒绝)android 连接到网络服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25354723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:52:46  来源:igfitidea点击:

ECONNREFUSED (Connection refused) android connect to webservice

javaandroidweb-serviceseconnrefused

提问by user3055923

I am getting connection refused error when trying to connect to webservice using soapui. When I tried using 127.0.0.1 in the URL, the error is ECONNREFUSED but when I tried 10.0.2.2, the error is connection timed out. Please can someone help. Thanks. Here is my code on Main Activity.

尝试使用soapui 连接到Web 服务时出现连接被拒绝错误。当我尝试在 URL 中使用 127.0.0.1 时,错误是 ECONNREFUSED 但当我尝试 10.0.2.2 时,错误是连接超时。请有人帮忙。谢谢。这是我关于主要活动的代码。

private static final String SOAP_ACTION = "http://tempuri.org/GetSMSOutgoing";
    private static final String INSERT_INCOMING_SMS = "SaveSMSIncoming";
    private static final String GET_OUTGOING_SMS = "GetSMSOutgoing";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://127.0.0.1:62499/WSsmsandroid.asmx?wsdl";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            getOutgoingSMS();
        } catch (Exception e) {
            Log.d("NOT CONNECTED: IOException", "NOT CONNECTED");
            e.printStackTrace();
        }

    }
});

thread.start();

}

public String getOutgoingSMS() {
        String outgoingSMS = null;

        SoapObject request = new SoapObject(NAMESPACE, GET_OUTGOING_SMS);
        request.addProperty("sentBy", "+639209100000");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE ht = new HttpTransportSE(URL);
        try {
            ht.call(SOAP_ACTION, envelope);

            SoapObject response2 = (SoapObject) envelope.getResponse();
            denemeList = new String[response2.getPropertyCount()];

            for (int i = 0; i < response2.getPropertyCount(); i++) {
                denemeList[i] = response2.getProperty(i).toString();
            }
            outgoingSMS = response2.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return outgoingSMS;
    }

回答by Diego Souza

To access your PC localhost from Android emulator, use 10.0.2.2 instead of 127.0.0.1. localhost or 127.0.0.1 refers to the emulated device itself, not the host the emulator is running on.

要从 Android 模拟器访问您的 PC 本地主机,请使用 10.0.2.2 而不是 127.0.0.1。localhost 或 127.0.0.1 指的是模拟设备本身,而不是运行模拟器的主机。

For Genymotionuse: 10.0.3.2instead of 10.0.2.2

对于Genymotion使用:10.0.3.2而不是 10.0.2.2

Reference: http://developer.android.com/tools/devices/emulator.html#networkaddresses

参考:http: //developer.android.com/tools/devices/emulator.html#networkaddresses

PS.: it's already been answered in connect failed: ECONNREFUSED

PS.:已在连接失败中得到回答:ECONNREFUSED

回答by Gabe Sechan

Your ip is wrong. 127.0.0.1 is loopback. Unless you're running a webservice on your local phone, that isn't likely to be what you want. 10.0.2.2 is a random ip on an unassigned network used for NATs, so unless you're on a wifi connection that has the service on that IP its unlikely to be right. So where are you really trying to connect to?

你的ip不对。127.0.0.1 是环回。除非您在本地电话上运行网络服务,否则这不太可能是您想要的。10.0.2.2 是用于 NAT 的未分配网络上的随机 IP,因此除非您使用具有该 IP 上的服务的 wifi 连接,否则它不太可能是正确的。那么你真正想要连接到哪里呢?

回答by Gabe Sechan

Check your ip address,i think port number not required. Once try by removing port number in URL.

检查您的 IP 地址,我认为不需要端口号。一次尝试通过删除 URL 中的端口号。