这是我的第一个Android应用程序,我正在尝试使用套接字将文本从我的计算机(服务器)发送到我的手机(客户端)。我以前用Java编程很多,但总是使用DrJava,而不是Eclipse,这是我必须用于Android编程的。
以下代码只是一个使用套接字的示例Java程序,我刚添加并删除了几行并将其粘贴到Android代码中。代码可在此处找到:http://zerioh.tripod.com/ressources/sockets.html 如果你不信任链接,你可以谷歌“java socket例子”。它应该是第一个链接。
我收到了一个未被捕获的异常错误,但我已经多次查看了代码,并且找不到没有catch行的地方。该代码适用于Java,但不适用于Android,因此我认为这是Android特定语法的一些问题。
以下是Android代码(客户端):
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();
}*/
}
}
服务器代码与网站上的相同。
以下是我认为相关的catlog行。如果我错过了一些,请告诉我,我也会发布这些内容:
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'
当我在Android模拟器上运行此代码时,它说:“抱歉!应用程序......已意外停止。请再试一次。”任何帮助将不胜感激!谢谢!
答案 0 :(得分:1)
添加此标记
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
在你的清单中。
要求您在appplication中使用它之前添加访问互联网的权限
答案 1 :(得分:0)
根据您的日志,您需要添加使用互联网权限。 在你的清单中声明这个。
答案 2 :(得分:0)
您需要在Android清单中使用INTERNET权限:
java.net.SocketException: Permission denied (maybe missing INTERNET permission)
如果in
子句中out
和finally
Streams为空,则不会捕获Nullpointer异常。这些是null
,因为它们从未打开过。