1

My Android Client which is supposed to connect to the Webservice crashes. I know Im connecting to HTTP in my main thread but I added two lines that resolve the problem (temporarily). I will add the AsyncTask later.

MainActivity:

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy); 

        try {
            GameAndroidUtil.testGameWS();
        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

GameAndroidUtil:

package pl.webcentral.androidclient1;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class GameAndroidUtil {
    private static final String NAMESPACE = "http://game.webcentral.pl/";
    private static final String SOAP_ACTION = "";
    private static final String WSDL_URL = "http://localhost:8080/ReversiGameWS/services/GameWS?wsdl";

    public static void testGameWS() throws SoapFault {

        String session1 = callGameLogin("Marcin 1");


        GameStatus gameStatus = callGameStatus(session1);
        System.out.println("Status gracza 1 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());


        String session2 = callGameLogin("Marcin 2");


        gameStatus = callGameStatus(session2);
        System.out.println("Status gracza 1 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());

        gameStatus = callGameStatus(session1);
        System.out.println("Status gracza 2 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());


        try {
            callGameAddMove(session1, 5);
        } catch (Exception e) {
            System.out.println("Złapaliśmy wyjątek zgodnie z założeniem");
        }


        callGameAddMove(session2, 5);


        gameStatus = callGameStatus(session2);
        System.out.println("Status gracza 1 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());

        gameStatus = callGameStatus(session1);
        System.out.println("Status gracza 2 " + gameStatus.getLastMove() + " " + gameStatus.isYourMove());
    }

    private static String callGameLogin(String userName) {
        String METHOD_NAME = "login";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

        PropertyInfo propInfo=new PropertyInfo();
        propInfo.setName("arg0");
        propInfo.setType(PropertyInfo.STRING_CLASS);
        propInfo.setValue(userName);

        request.addProperty(propInfo);

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(WSDL_URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();

            return resultsRequestSOAP.toString();

        } catch (Exception e) {
            throw new RuntimeException("Unexpected exception", e);
        }
    }
    private static GameStatus callGameStatus(String sessionId) throws SoapFault {
        String METHOD_NAME = "getGameStatus";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

        PropertyInfo propInfo=new PropertyInfo();
        propInfo.setName("arg0");
        propInfo.setType(PropertyInfo.STRING_CLASS);
        propInfo.setValue(sessionId);

        request.addProperty(propInfo);

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(WSDL_URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
        } catch (Exception e) {
            throw new RuntimeException("Unexpected exception", e);
        }

        try {
            SoapObject response = (SoapObject)envelope.getResponse();

            GameStatus gameStatus = new GameStatus();
            if (response.hasProperty("lastMove")) {
                gameStatus.setLastMove(Integer.parseInt(response.getProperty("lastMove").toString()));
            }
            gameStatus.setYourMove(Boolean.parseBoolean(response.getProperty("yourMove").toString()));

            return gameStatus;
        } catch (SoapFault e) {
            System.out.println("Error adding move: " + e.faultstring);//można to ładnie jakoś pokazać na ekranie
            throw e;
        }
    }
    private static void callGameAddMove(String sessionId, Integer move) throws SoapFault {
        String METHOD_NAME = "addMove";

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

        PropertyInfo propInfo=new PropertyInfo();
        propInfo.setName("arg0");
        propInfo.setType(PropertyInfo.STRING_CLASS);
        propInfo.setValue(sessionId);

        request.addProperty(propInfo);

        propInfo=new PropertyInfo();
        propInfo.setName("arg1");
        propInfo.setType(PropertyInfo.INTEGER_CLASS);
        propInfo.setValue(move);

        request.addProperty(propInfo);

        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(WSDL_URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
        } catch (Exception e) {
            throw new RuntimeException("Unexpected exception", e);
        }

        try {
            SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
        } catch (SoapFault e) {
            System.out.println("Error adding move: " + e.faultstring);//można to ładnie jakoś pokazać na ekranie
            throw e;
        }
    }

}

LogCat:

01-12 16:54:27.930: E/AndroidRuntime(1070): FATAL EXCEPTION: main
01-12 16:54:27.930: E/AndroidRuntime(1070): java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.webcentral.androidclient1/pl.webcentral.androidclient1.MainActivity}: java.lang.RuntimeException: Unexpected exception
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.os.Looper.loop(Looper.java:137)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.lang.reflect.Method.invoke(Method.java:511)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at dalvik.system.NativeStart.main(Native Method)
01-12 16:54:27.930: E/AndroidRuntime(1070): Caused by: java.lang.RuntimeException: Unexpected exception
01-12 16:54:27.930: E/AndroidRuntime(1070):     at pl.webcentral.androidclient1.GameAndroidUtil.callGameLogin(GameAndroidUtil.java:76)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at pl.webcentral.androidclient1.GameAndroidUtil.testGameWS(GameAndroidUtil.java:18)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at pl.webcentral.androidclient1.MainActivity.onCreate(MainActivity.java:24)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.Activity.performCreate(Activity.java:5008)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-12 16:54:27.930: E/AndroidRuntime(1070):     ... 11 more
01-12 16:54:27.930: E/AndroidRuntime(1070): Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.io.IoBridge.isConnected(IoBridge.java:224)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.io.IoBridge.connect(IoBridge.java:112)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at java.net.Socket.connect(Socket.java:842)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:120)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:176)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at pl.webcentral.androidclient1.GameAndroidUtil.callGameLogin(GameAndroidUtil.java:69)
01-12 16:54:27.930: E/AndroidRuntime(1070):     ... 16 more
01-12 16:54:27.930: E/AndroidRuntime(1070): Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
01-12 16:54:27.930: E/AndroidRuntime(1070):     at libcore.io.IoBridge.isConnected(IoBridge.java:208)
01-12 16:54:27.930: E/AndroidRuntime(1070):     ... 36 more
01-12 16:54:32.910: I/Process(1070): Sending signal. PID: 1070 SIG: 9

I really can't figure what these exceptions are caused by...

user208030
  • 145
  • 2
  • 9

1 Answers1

1

I hope you have added the permission for Internet in your application manifest.

<manifest xlmns:android...>
 ...
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>

EDIT : You are trying to connect to the web-service by specifying localhost:8080 as the address of the system hosting the service. The Android emulator runs inside a Virtual Machine (QEMU). Hence, localhost:8080 would be the emulator's own loop back address, and not that of the system.

So, you can either go to CommandPrompt in your Windows to get the IP address of your system or just use http://10.0.2.2:8080/... instead of using localhost.

Swayam
  • 16,294
  • 14
  • 64
  • 102
  • Yes, I did. I've spent the whole day and still can't figure out why it's not working... Are the two Exceptions (Runtime and Connect) somehow related in my case? – user208030 Jan 12 '13 at 16:41
  • Yes, the RuntimeException is caused by the ConnectException. Your web-service seems to be refusing the connection due to some reason. – Swayam Jan 12 '13 at 16:43
  • What can it be caused by? I gotta admit that I am a real newbe in a world of Java web services and Android. How should I configure the Android Client to work with the WebService? Is providing the NAMESPACE and the WSDL_URL enough? – user208030 Jan 12 '13 at 16:48
  • Your code on the client side seems to be okay to me. Not being able to access the internet seemed the only reason to me. – Swayam Jan 12 '13 at 16:52
  • Maybe this is something with the web service then. I am not sure whether I configured it properly but it seems to be working at first glance... When I invoke a method login in Eclipse for example I get a response as planned. – user208030 Jan 12 '13 at 16:54
  • It seems to be much better! Now I get a NullPointerException at gameStatus.setLastMove(Integer.parseInt(response.getProperty("lastMove").toString())); – user208030 Jan 12 '13 at 17:11
  • I would recommend you to post it as a different question where the main problem is the Null pointer exception and not the connection refused error. It would help you get more response. Plus you would need to attach the full LogCat output to enable us to identify the actual problem. – Swayam Jan 12 '13 at 17:26
  • Could you post the LogCat error for the NullPointerException ? – Swayam Jan 12 '13 at 17:33