this is my first Android app and I’m trying to use Sockets to send text from my computer (server) to my phone (client). I’ve programmed in Java a lot before, but always with DrJava, not Eclipse, which is what I’m having to use for Android programming.
The following code is simply an example Java program using Sockets that I just added and removed a few lines and pasted it into the Android code. The code is found here: http://zerioh.tripod.com/ressources/sockets.html
If you don’t trust links, you can Google “java socket example.” It should be the first link.
I’m getting an uncaught exception error, but I’ve looked through the code many times and I can’t find a place without a catch line. The code works with Java, but not Android, so I think it’s some issue with Android-specific syntax.
Here is the Android code (client):
package com.example.helloandroid;
import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//TextView tv = new TextView(this);
//tv.setText("Eclipse is a horrible program");
Requester client = new Requester();
client.run();
//setContentView(tv);
}
}
class Requester{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
void run()
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("localhost", 2004);
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
try{
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage("asdfjksajdflksjdklfjsdklfjlsdf");
message = "bye";
sendMessage(message);
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}
catch(UnknownHostException unknownHost) {
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException) {
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(Exception e) {
e.printStackTrace();
}
/*catch(IOException ioException){
ioException.printStackTrace();
}*/
}
}
The server code is the same as on the website.
Here is the catlog lines that I think are relevant. If I missed some, let me know and I’ll post those too:
11-20 22:37:35.314: W/System.err(545): java.net.SocketException: Permission denied (maybe missing INTERNET permission)
11-20 22:37:35.324: W/System.err(545): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
11-20 22:37:35.324: W/System.err(545): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:187)
11-20 22:37:35.324: W/System.err(545): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:266)
11-20 22:37:35.324: W/System.err(545): at java.net.Socket.startupSocket(Socket.java:773)
11-20 22:37:35.324: W/System.err(545): at java.net.Socket.tryAllAddresses(Socket.java:192)
11-20 22:37:35.324: W/System.err(545): at java.net.Socket.<init>(Socket.java:256)
11-20 22:37:35.334: W/System.err(545): at java.net.Socket.<init>(Socket.java:220)
11-20 22:37:35.334: W/System.err(545): at com.example.helloandroid.Requester.run(HelloAndroid.java:33)
11-20 22:37:35.334: W/System.err(545): at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:19)
11-20 22:37:35.334: W/System.err(545): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-20 22:37:35.334: W/System.err(545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-20 22:37:35.334: W/System.err(545): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-20 22:37:35.344: W/System.err(545): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-20 22:37:35.344: W/System.err(545): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-20 22:37:35.344: W/System.err(545): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 22:37:35.344: W/System.err(545): at android.os.Looper.loop(Looper.java:123)
11-20 22:37:35.344: W/System.err(545): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-20 22:37:35.344: W/System.err(545): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 22:37:35.344: W/System.err(545): at java.lang.reflect.Method.invoke(Method.java:521)
11-20 22:37:35.344: W/System.err(545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-20 22:37:35.344: W/System.err(545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-20 22:37:35.354: W/System.err(545): at dalvik.system.NativeStart.main(Native Method)
11-20 22:37:35.354: D/AndroidRuntime(545): Shutting down VM
11-20 22:37:35.354: W/dalvikvm(545): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-20 22:37:35.354: E/AndroidRuntime(545): Uncaught handler: thread main exiting due to uncaught exception
11-20 22:37:35.364: E/AndroidRuntime(545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroid}: java.lang.NullPointerException
11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-20 22:37:35.364: E/AndroidRuntime(545): at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 22:37:35.364: E/AndroidRuntime(545): at android.os.Looper.loop(Looper.java:123)
11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.main(ActivityThread.java:4363)
11-20 22:37:35.364: E/AndroidRuntime(545): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 22:37:35.364: E/AndroidRuntime(545): at java.lang.reflect.Method.invoke(Method.java:521)
11-20 22:37:35.364: E/AndroidRuntime(545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-20 22:37:35.364: E/AndroidRuntime(545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-20 22:37:35.364: E/AndroidRuntime(545): at dalvik.system.NativeStart.main(Native Method)
11-20 22:37:35.364: E/AndroidRuntime(545): Caused by: java.lang.NullPointerException
11-20 22:37:35.364: E/AndroidRuntime(545): at com.example.helloandroid.Requester.run(HelloAndroid.java:60)
11-20 22:37:35.364: E/AndroidRuntime(545): at com.example.helloandroid.HelloAndroid.onCreate(HelloAndroid.java:19)
11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-20 22:37:35.364: E/AndroidRuntime(545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-20 22:37:35.364: E/AndroidRuntime(545): ... 11 more
11-20 22:37:35.384: I/dalvikvm(545): threadid=7: reacting to signal 3
11-20 22:37:35.404: I/dalvikvm(545): Wrote stack trace to '/data/anr/traces.txt'
When I run this code on an Android emulator, it says, “Sorry! The application…has stopped unexpectedly. Please try again.” Any help will be greatly appreciated! Thanks!
add this tag
in your manifest.
Its required that you add permissions to access internet before using it in your appplication