私は、Androidデバイスがサーバー(自分のコンピューター)にパケットを送信しようとする小さなアプリを書いています。
エラーが発生しました。修正方法がわかりません。
エラーは次のとおりです。
>07-02 14:58:56.125: E/AndroidRuntime(17976): FATAL EXCEPTION: main
>07-02 14:58:56.125: E/AndroidRuntime(17976): android.os.NetworkOnMainThreadException
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >libcore.io.IoBridge.connectErrno(IoBridge.java:127)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >libcore.io.IoBridge.connect(IoBridge.java:112)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.net.Socket.startupSocket(Socket.java:566)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.net.Socket.tryAllAddresses(Socket.java:127)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at java.net.Socket.<init>(Socket.java:177)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at java.net.Socket.<init>(Socket.java:149)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >com.amazon.hsyal.WifiSimulatorActivity$1.onClick(WifiSimulatorActivity.java:33)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.view.View.performClick(View.java:3536)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.view.View$PerformClick.run(View.java:14130)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.os.Handler.handleCallback(Handler.java:605)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.os.Handler.dispatchMessage(Handler.java:92)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at android.os.Looper.loop(Looper.java:137)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >android.app.ActivityThread.main(ActivityThread.java:4495)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.lang.reflect.Method.invokeNative(Native Method)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >java.lang.reflect.Method.invoke(Method.java:511)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at >com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
>07-02 14:58:56.125: E/AndroidRuntime(17976): at dalvik.system.NativeStart.main(Native >Method)
私のAndroidプログラムは次のとおりです。
public class WifiSimulatorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button send_packet_button = (Button) findViewById(R.id.send_packet_button);
send_packet_button.setOnClickListener(sendPacketListener);
}
Button.OnClickListener sendPacketListener = new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
try{
Socket s = new Socket("192.168.52.138", 12345);
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
output.write("Sending packet 1");
s.close();
}
catch(UnknownHostException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
};
}
public static void main(String[] args){
try{
Boolean end = false;
ServerSocket ss = new ServerSocket(12345);
System.out.println("Program running");
while(!end){
//Server is waiting for client here, if needed
Socket s = ss.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
String st = input.readLine();
System.out.println("Tcp Example From client: "+st);
output.println("Good bye and thanks for all the fish :)");
s.close();
}
ss.close();
}