t remember that doing so fixes the initial error of the compiler not recognizing the R class):
The ServerActivity class:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
public class ServerActivity extends Activity {
private TextView serverStatus;
// default ip
public static String SERVERIP = "10.0.2.15";
// designate a port
public static final int SERVERPORT = 8080;
private Handler handler = new Handler();
private ServerSocket serverSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
serverStatus = (TextView) findViewById(R.id.server_status);
SERVERIP = getLocalIpAddress();
Thread fst = new Thread(new ServerThread());
fst.start();
}
public class ServerThread implements Runnable {
public void run() {
try {
if (SERVERIP != null) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Listening on IP: " + SERVERIP);
}
});
serverSocket = new ServerSocket(SERVERPORT);
while (true) {
// listen for incoming clients
Socket client = serverSocket.accept();
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Connected.");
}
});
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
handler.post(new Runnable() {
@Override
public void run() {
// do whatever you want to the front end
// this is where you can be creative
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}
// gets the ip address of your phone's network
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)/> {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)/> {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}
@Override
protected void onstop() {
super.onstop();
try {
// make sure you close the socket upon exiting
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and, the ClientActivity class:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class ClientActivity extends Activity {
private EditText serverIp;
private Button connectPhones;
private String serverIpAddress = "";
private boolean connected = false;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setonclickListener(connectListener);
}
private onclickListener connectListener = new onclickListener() {
@Override
public void onclick(View v) {
if (!connected) {
serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
};
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
// where you issue the commands
out.println("Hey Server!");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}
}
The errors the compiler specifies are:
For the ServerActivity class:
error: cannot find symbol
setContentView(R.layout.server);
symbol: variable server
location: class layout
//At:
setContentView(R.layout.server);
error: cannot find symbol
serverStatus = (TextView) findViewById(R.id.server_status);
symbol: variable id
location: class R
//At:
serverStatus = (TextView) findViewById(R.id.server_status);
For the ClientActivity class:
error: cannot find symbol
setContentView(R.layout.client);
symbol: variable client
location: class layout
//At:
setContentView(R.layout.client);
error: cannot find symbol
serverIp = (EditText) findViewById(R.id.server_ip);
symbol: variable id
location: class R
//At:
serverIp = (EditText) findViewById(R.id.server_ip);
error: cannot find symbol
connectPhones = (Button) findViewById(R.id.connect_phones);
symbol: variable id
location: class R
//At:
connectPhones = (Button) findViewById(R.id.connect_phones);
And then I get some errors that I believe happen because I'm not writing something that goes against the rules of the programming for android, in the build.xml file:
The following error occurred while executing this line:
<do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
Compile failed; see the compiler error output for details.
//At this line in build.xml:
fork="${need.javac.fork}">
I read around, that it's necessary to declare new variables using classes and objects that are to be in the R class to also be declared inside an xml file, which I haven't clear if it is the manifest or main or strings file. I've tried looking for some kind of tutorial that explains how to properly make mention of these new variables in the xml, but haven't found anything significant that is simple to understand. I mean, it's almost obvious that, for the errors I mentioned above, the variables missing in class layout should be written in the main.xml file since it is inside the layout folder, but what about those variables in class R (namely, variable id)? Should they be written in the manifest file? And what should I write? Please please let me know how I can solve these errors to be able to run this program and make some sense of it. Any help as always is greatly appreciated =)
By the way, I wrote the two classes specified in the tutorial(s) inside a single android project. Is that correct? If not, and I should put them into separate projects, how should I fix this line in the ClientActivity class?
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT); //Since SERVERPORT is taken from the ServerActivity class
Maybe these errors are easy to fix, thereby making me a noob at this. But since I really love Java and have programmed a fair amount of time in it, I thought I'd give android a try =)

New Topic/Question
Reply



MultiQuote





|