Android Solver ~

Source: Internet
Author: User
Tags sendmsg

============ Problem Description ============


public class mainactivity extends activity {private static final  string log_tag =  "Ip test";p rivate edittext chattxt;private textview  chattxt2;private button chatok; @Overridepublic  void oncreate (bundle  savedinstancestate)  {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Findviews ( ); Setonclick (); Chattxt2.settext (GetLocalIpAddressV4 ());} Public void findviews ()  {//chattxt =  (EditText)  this.findviewbyid (r.id.chattxt); chattxt2 =  (TextView)  this.findviewbyid (r.id.chattxt2);chatok =  (Button)   This.findviewbyid (R.id.chatok);} Private void setonclick ()  {chatok.setonclicklistener (New view.onclicklistener ()  {@ Overridepublic void onclick (VIEW V)  {try {connecttoserver ("I ' m a client!"); /connecttoserver (Chattxt.gettext (). toString ());}  catch (unknownhostexception e)  {// todo auto-generated catch blocke.printstacktrace () ;}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}});} Public void connecttoserver (string socketdata)  throws unknownhostexception, Ioexception {socket socket = requestsocket ("10.13.33.136",  5000);//  program opens and executes here and stops running, wondering why? The phone is connected to WiFi, Address is 10.24.28.139, gateway 10.24.16.1, computer IP is 10.13.33.136, gateway is 10.13.33.1, so here feel should be not a cause, but why mobile phone can open my website, address is:/http 10.13.33.136:8080/phnt ps: The bad ~t_t of network science Socket socket = new socket ("10.13.33.136",  5000);//sendmsg (Socket, socketdata) ;//string txt = receivemsg (socket);//this.chattxt2.settext (TXT);} Private socket requestsocket (String host, int port) throws unknownhostexception,  ioexception {socket socket = new socket (host, port); return socket;} Private void sendmsg (socket socket, string msg)  throws ioexception { Bufferedwriter writer = new bufferedwriter (New outputstreamwriter ( Socket.getoutputstream ())); Writer.write (Msg.replace ("\ n",  " ")  +  "\ n"); Writer.flush ();} Private string receivemsg (Socket socket)  throws ioexception {bufferedreader  reader = new bufferedreader (New inputstreamreader (Socket.getinputstream ())); String txt = reader.readline (); return txt;}

============ Solution 1============


Network Request write line thread

============ Solution 2============


android4.0 later versions, do not allow network operations in the UI thread, network operations should be placed in child threads, or Asynctask executed.

============ Solution 3============


Reference 6 floor Lionfresh reply:
android4.0 later versions, do not allow network operations in the UI thread, network operations should be placed in sub-threads, or asynctask execution.


Top ~

============ Solution 4============


Log there is a hint not to let the main thread access the network!

============ Solution 5============


New Thread (New Runnable () {
public void Run () {
Connecttoserver ("I ' m a client!");
}
}). Start ();
Put this piece of code in your OnClick method ~

Android Solver ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.