我正在一个学校项目试图将一个Android应用程序连接到一个给我们一小时的服务器,但是当我尝试将套接字连接到我的应用程序时出现错误:
这是MainActivity.java的代码
public void ejecutar(View view) throws UnknownHostException,IOException{
InetAddress direServidor =InetAddress.getByName("192.168.1.68");
Socket socket=new Socket(direServidor, 1000);
System.out.println("Si me presionaron");
InputStream is=socket.getInputStream();
BufferedReader br= new BufferedReader (new InputStreamReader (is));
EditText et = (EditText)findViewById(R.id.fecha);
et.setText(br.readLine());
br.close();
socket.close();
}
这只是功能。
我已经在manifest.xml中配置了权限
<uses-permission android:name= "android.permission.INTERNET"/>
但似乎没有任何效果!
logcat的:
> 02-19 00:26:27.044: E/AndroidRuntime(16758): FATAL EXCEPTION: main
> 02-19 00:26:27.044: E/AndroidRuntime(16758): Process:
> com.example.clientehora, PID: 16758 02-19 00:26:27.044:
> E/AndroidRuntime(16758): java.lang.IllegalStateException: Could not
> execute method of the activity 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> android.view.View$1.onClick(View.java:3823) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> android.view.View.performClick(View.java:4438) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> android.view.View$PerformClick.run(View.java:18422) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> android.os.Handler.handleCallback(Handler.java:733) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> android.os.Handler.dispatchMessage(Handler.java:95) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> android.os.Looper.loop(Looper.java:136) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> android.app.ActivityThread.main(ActivityThread.java:5017) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> java.lang.reflect.Method.invokeNative(Native Method) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> java.lang.reflect.Method.invoke(Method.java:515) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
> 02-19 00:26:27.044: E/AndroidRuntime(16758): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> dalvik.system.NativeStart.main(Native Method) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): Caused by:
> java.lang.reflect.InvocationTargetException 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> java.lang.reflect.Method.invokeNative(Native Method) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> java.lang.reflect.Method.invoke(Method.java:515) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> android.view.View$1.onClick(View.java:3818) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): ... 11 more 02-19 00:26:27.044:
> E/AndroidRuntime(16758): Caused by:
> android.os.NetworkOnMainThreadException 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
> 02-19 00:26:27.044: E/AndroidRuntime(16758): at
> libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> libcore.io.IoBridge.connectErrno(IoBridge.java:127) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> libcore.io.IoBridge.connect(IoBridge.java:112) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at
> java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 02-19
> 00:26:27.044: E/AndroidRuntime(16758): at
> java.net.Socket.startupSocket(Socket.java:567) 02-19 00:26:27.044:
> E/AndroidRuntime(16758): at java.net.Socket.<init>(Socket.java:226)
> 02-19 00:26:27.044: E/AndroidRuntime(16758): at
> com.example.clientehora.MainActivity.ejecutar(MainActivity.java:37)
答案 0 :(得分:0)
您需要在主线程的单独线程中运行套接字才能完成此操作。在主线程上执行此类繁重活动的原因可能会锁定应用程序并导致其崩溃。
public void ejecutar(View view) throws UnknownHostException,IOException{
new Thread( new Runnable(){
InetAddress direServidor = InetAddress.getByName("192.168.1.68");
Socket socket = new Socket(direServidor, 1000);
System.out.println("Si me presionaron");
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader (new InputStreamReader (is));
EditText et = (EditText)findViewById(R.id.fecha);
et.setText(br.readLine());
br.close();
socket.close();
}).start();
}
给它一个旋转。