0
我在一個學校項目試圖將Android應用程序連接到該給我們一個小時的服務器,但是當我嘗試將套接字連接到我的應用程序我得到一個錯誤:可連接插座的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)
我認爲這可能是問題所在!但我無法理解如何在Java和Android中執行此操作。你能給我舉個例子嗎? –