javaandroidweb-serviceseconnrefused

ECONNREFUSED (Connection refused) android connect to webservice


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.

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;
    }

Solution

  • 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.

    For Genymotion use: 10.0.3.2 instead of 10.0.2.2

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

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