问题描述
publicclassMainActivityextendsActionBarActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Buttonopen=(Button)findViewById(R.id.openbutton);Buttonclose=(Button)findViewById(R.id.closebutton);open.setText("open");close.setText("close");open.setOnClickListener(newopenListener());close.setOnClickListener(newcloseListener());if(savedInstanceState==null){getSupportFragmentManager().beginTransaction().add(R.id.container,newPlaceholderFragment()).commit();}}classopenListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubtry{Sockets=newSocket("192.168.1.106",9995);PrintWriterprint=newPrintWriter(s.getOutputStream(),true);Stringres="1";print.println(res);}catch(Exceptione){e.printStackTrace();}}}classcloseListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubtry{Sockets=newSocket("192.168.1.106",9995);PrintWriterprint=newPrintWriter(s.getOutputStream(),true);Stringres="0";print.println(res);}catch(Exceptione){e.printStackTrace();}}}代码哪有问题?为什么发送不出信息??
解决方案
解决方案二:
publicclassMainActivityextendsActionBarActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Buttonopen=(Button)findViewById(R.id.openbutton);Buttonclose=(Button)findViewById(R.id.closebutton);open.setText("open");close.setText("close");open.setOnClickListener(newopenListener());close.setOnClickListener(newcloseListener());if(savedInstanceState==null){getSupportFragmentManager().beginTransaction().add(R.id.container,newPlaceholderFragment()).commit();}}classopenListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubtry{Sockets=newSocket("192.168.1.106",9995);PrintWriterprint=newPrintWriter(s.getOutputStream(),true);Stringres="1";print.println(res);}catch(Exceptione){e.printStackTrace();}}}classcloseListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubtry{Sockets=newSocket("192.168.1.106",9995);PrintWriterprint=newPrintWriter(s.getOutputStream(),true);Stringres="0";print.println(res);}catch(Exceptione){e.printStackTrace();}}}
解决方案三:
看看你的网络设置以及权限