3
votes

I'm building a simple client-server system. I've built a Java server & an android client which runs on mine device (both connected to the same LAN).

When I'm just trying to setup a connection (by clicking a button) the client fails with the error

-"java.lang.IllegalStateException: Could not execute method of the activity"

In other answers to close problem was said that it might be connected to AsyncTask, but I'm not sure.

server code:

    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;

    public class Server { 
        public static void startConnection(int portNum)
        {   
            ServerSocket serverSocket=null;
            Socket clinetSocket=null;
            ObjectOutputStream serverOut=null;
            ObjectInputStream serverIn=null;
            String message=null;

            //create a socket
            try{
                serverSocket=new ServerSocket(portNum);
            } 
            catch (IOException e) {
                e.printStackTrace();
            } 
            System.out.println("Waiting for connection...");
            try{
                clinetSocket = serverSocket.accept();
            }
            catch (IOException e){
                e.printStackTrace();
            }

            System.out.println("connected to "+ clinetSocket.getInetAddress().getHostName());

        }

Android client code :

    Main.java :

    package com.example.user_pc.myapplication;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import java.net.Socket;
    import java.io.IOException;
    import java.net.UnknownHostException;
    import java.net.SocketException;
    import android.util.Log;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;

    public class Main extends ActionBarActivity {

        TextView text;

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

            text= (TextView) findViewById(R.id.textView1);
            text.setText("");
        }

        public void pushTheButton(View v)
        {
            Button button = (Button) findViewById(R.id.button);
            createSocket();
        }
        public void createSocket()
        {
            Socket sock = null;
            String dstIP = "192.168.2.103";//server ip
            int dstPort = 9632;


             try
            {
                sock = new Socket(dstIP, dstPort);
            }

            catch(SocketException ie)
            {
                Log.wtf("SocketException",ie);
            }

            catch(UnknownHostException ie)
            {
                Log.wtf("UnknownHostException",ie);
            }
            catch(IOException ie)
            {
                Log.wtf("IOException",ie);
            }

    }

        public static void main(String[] args) 
        {
            int portNum=9632;
            startConnection(portNum);
        }

    ``}

manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user_pc.myapplication" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main"
    android:onClick="pushTheButton">

    <Button
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:text="Click to connect"
        android:id="@+id/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="96dp"
        android:onClick="pushTheButton"
        />
</RelativeLayout>
  • Logcat

    01-19 16:28:02.425 10779-10779/com.example.user_pc.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:3599) at android.view.View.performClick(View.java:4204) at android.view.View$PerformClick.run(View.java:17355) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5226) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at android.view.View$1.onClick(View.java:3594)             at android.view.View.performClick(View.java:4204)             at android.view.View$PerformClick.run(View.java:17355)             at android.os.Handler.handleCallback(Handler.java:725)             at android.os.Handler.dispatchMessage(Handler.java:92)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:5226)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)             at dalvik.system.NativeStart.main(Native Method) Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) at libcore.io.IoBridge.connectErrno(IoBridge.java:127) at libcore.io.IoBridge.connect(IoBridge.java:112) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.Socket.startupSocket(Socket.java:566) at java.net.Socket.tryAllAddresses(Socket.java:127) at java.net.Socket.(Socket.java:177) at java.net.Socket.(Socket.java:149) at com.example.user_pc.myapplication.Main.createSocket(Main.java:50) at com.example.user_pc.myapplication.Main.pushTheButton(Main.java:35)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at android.view.View$1.onClick(View.java:3594)             at android.view.View.performClick(View.java:4204)             at android.view.View$PerformClick.run(View.java:17355)             at android.os.Handler.handleCallback(Handler.java:725)             at android.os.Handler.dispatchMessage(Handler.java:92)             at android.os.Looper.loop(Looper.java:137)             at android.app.ActivityThread.main(ActivityThread.java:5226)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:511)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)             at dalvik.system.NativeStart.main(Native Method)

1
post the logcat which will include error messages and stacktrace.David Wasser
You shouldn't be doing I/O on the main (UI) thread. You need to be using background threads (oir AsyncTask) to do network I/O.David Wasser
@DavidWasser Logcat added.gil

1 Answers

1
votes

Exactly as I said. You are getting a NetworkOnMainThreadException. You need to do network I/O in a background thread. Your createSocket() method must start a new background thread to do this work. Or use an AsyncTask.