Common error resolution for Android download files

Source: Internet
Author: User

Recently in the study of the Mars Teacher's Android development course, modeled after the http://www.cnblogs.com/Laupaul/archive/2012/02/12/2348293.html code to write the application, the results in the middle of a variety of problems, Here's a summary:

1.java.lang.NullPointerExceptionErrorandroid.os.NetworkOnMainThreadExceptionAbnormal

Cause: urlCon.getInputStream() error when executing, not getting InputStream . This exception probably means an exception when the main thread accesses the network. The cause of this error is that the code does not conform to the Android specification, Android version prior to 4.0 support access to the network in the main thread, but after 4.0 to optimize this part of the program, that is, access to the network code can not be written in the main thread.

Workaround:

To use a multi-threaded, asynchronous Load method:

    String LRC;                Handler Handler = new Handler () {public void Handlemessage (Message msg) {if (Msg.what = = 0x123) {            System.out.println (LRC);    }        }    };        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_download);        Downloadtxtbutton = (Button) Findviewbyid (r.id.downloadtxt);        Downloadmp3button = (Button) Findviewbyid (R.id.downloadmp3);        TextView = (TextView) Findviewbyid (R.id.textview);                Downloadtxtbutton.setonclicklistener (New View.onclicklistener () {public void OnClick (view view) { New Thread () {public void run () {Httpdownloader Httpdownloader = new Httpdo                        Wnloader ();                        LRC = Httpdownloader.download ("HTTP://127.0.0.1/ANDROID_DOWNLOAD/WHITE_BLACK.LRC");                 Handler.sendemptymessage (0x123);       Textview.settext (LRC);            }}.start ();    }        }); }


2. Android read file from Tomcat error:connect failed: ECONNREFUSED

Local boot tomcat, when you want Android to download files from the local server, the IP address write 127.0.0.1 or localhost in the URL is not valid in the emulator:

W/System.err(12527): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused)

Instead of the native IP address also shows that cannot connect, finally found on the Internet a very easy way: Android to the 127.0.0.1 as the simulator, and the computer local IP set to 10.0.2.2, so only need to connect to the local computer Web service address to: http://10.0.2.2 can be.

3.Only the original thread that created a view hierarchy can touch its viewsError

Android's related view and control is not thread-safe, from the error message can be learned only by the thread that generated the view to change it, so it will be textview.setText(lrc); put handleMessage() in.

After some toss, finally out of the results:

At present is still a novice, so out of the question on all kinds of overwhelmed, experience or need to slowly accumulate ~


Common error resolution for Android download files

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.