0

I have an android application which I want to send a simple command to my `public class ActivitymainActivity extends Activity {

private TextView textview;
private Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitymain);

    textview=(TextView) findViewById(R.id.EmailCount);
    button=(Button) findViewById(R.id.button1);
    textview.setText("Going in");
    try{
        Socket socket = new Socket("192.168.1.66", 2727);   

        OutputStream out = socket.getOutputStream();       
        PrintWriter output = new PrintWriter(out);         

        textview.setText("Sending Data to PC");         
        output.println("Hello from Android");
        output.flush();
        output.close();
        textview.setText("Data sent to PC");            

        socket.close();                                    
        textview.setText("Socket closed"); 
    }
    catch(Exception e){System.out.print(e+"shacso");}

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            textview.setText("Trying to send");
             try{
                    Socket socket = new Socket("192.168.1.66", 2727);   

                    OutputStream out = socket.getOutputStream();       
                    PrintWriter output = new PrintWriter(out);         

                    textview.setText("Sending Data to PC");         
                    output.println("Checking Email now:D");
                    output.flush();
                    output.close();
                    textview.setText("Data sent to PC");            

                    socket.close();                                    
                    textview.setText("Socket closed");                  }
                catch(Exception e){System.out.print(e+"");}

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activitymain, menu);
    return true;
}

} `

And this is my Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emailclient"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

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

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

However, I am not able to receive anything on the server. Downloaded an application from play store called "UDP TCP Server" it send packets to a specified Ip and port and I was able to get the sent data to my server. Anything wrong with my code?

Code explanation: Send data went app lunches and when a button on clicked

Thanks :)

Syntax_Error
  • 5,964
  • 15
  • 53
  • 73
  • 1
    Separate your network code on another thread and try again – minhaz Mar 29 '13 at 23:06
  • Could you explain more? – Syntax_Error Mar 29 '13 at 23:08
  • 1
    You have to post your logcat for more specific answer but, in general Android do not support netwrok on main UI thread. i can see that you are trying to open a network connection on onCreate which is strictly prohibited on new Android OS and created lot of issue on older version. I do not know which android version you are using but you will probably get ANR for older version. So at first try to separate your network code on a different thread. Then find out what next. Check [this]( http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – minhaz Mar 29 '13 at 23:17
  • 1
    [This][1] thread can help you implementing your need. [1]: http://stackoverflow.com/questions/5135438/example-android-bi-directional-network-socket-using-asynctask – ChristopheCVB Mar 29 '13 at 23:23
  • Perfect :) Worked Thanks... If any of you would like to go an answer I'll mark it there.. – Syntax_Error Mar 29 '13 at 23:43

2 Answers2

2

All Network Communications should be done in a separate Thread! Not in the main UI thread. That is the reason your code is not working.

Bhombol
  • 21
  • 2
0

You say:

I am not able to receive anything on the server.

Looking at your code it seems that your not actually attempting to READ the response from the server. After sending the message you then need to access the socket InputStream to read the response from the server.

Giles Thompson
  • 1,097
  • 1
  • 9
  • 24