ECONNREFUSED(连接被拒绝)android连接到webservice

时间:2014-08-17 23:52:51

标签: java android web-services econnrefused

尝试使用soapui连接到webservice时出现连接拒绝错误。当我尝试在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;
    }

3 个答案:

答案 0 :(得分:5)

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

对于 Genymotion ,请使用: 10.0.3.2 而不是10.0.2.2

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

PS:已经在connect failed: ECONNREFUSED

中回答了问题

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

检查你的IP地址,我认为不需要端口号。尝试删除URL中的端口号。