0

I am trying to connect a JAVA server to an android application but I am failed to do it.....

This is my server....

package com.example.androidserver;

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

public class ServerMain 
{
public static void main(String []args)
{
    ServerSocket ss;
    try 
    {
        ss = new ServerSocket(7654);
        Socket socket = ss.accept();

        boolean t = socket.isConnected();
        if(t)
        {
            System.out.println("Client connected");
        }
    }
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}
}

And this is client...

package com.example.androidclient;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View; 
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

Button send;
EditText et1;
Socket socket;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new Thread()
    {
        public void run()
        {
            try
            {
                socket = new Socket("localhost", 7654);
                boolean t = socket.isConnected();
                if(t)
                {
                    Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_LONG).show();
                }
            } 
            catch (UnknownHostException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }.start();
    send = (Button)findViewById(R.id.button1);
    et1= (EditText)findViewById(R.id.editText1);

    send.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            String str;
            str = et1.getText().toString();
            Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
        }});
}

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

}

XML 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" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:text="Send" />

</RelativeLayout>

i didn't get what's wrong... My client n server are not getting connected....Please help me correct it...

Naveed S
  • 5,106
  • 4
  • 34
  • 52

2 Answers2

0

I think your problem is here: socket = new Socket("localhost", 7654); Why is localhost? It should be server IP address.

And there is another problem:

Toast.makeText(getBaseContext(), "Connected", Toast.LENGTH_LONG).show();

You cant show toast from non UI thread. Read this.

Community
  • 1
  • 1
Leonidos
  • 10,482
  • 2
  • 28
  • 37
0

localhost refers the the computer on which the code is executing, which in the context of an Android application is the phone or the emulator, not your development computer.

The best way to test your scenario would be to connect your phone to the same local wireless network your development machine is connected, and use the IP address of your machine, instead of localhost.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169