For a long time not to write the Android code hands are born, find their own before the program found to run, and no special error prompted to see a useful error caused by: Android.os.NetworkOnMainThreadException, check the reason in 4.0 after the main thread in the implementation of HTTP request will be reported this error, probably is afraid of HTTP request time too long cause the procedure of suspended animation.
The solution has two approaches, namely:
The first method: direct neglect, mandatory use (strongly not recommended, but simple to modify)
Add the following code below the Setcontentview (R.layout.activity_main) of the mainactivity file
if (Android.os.Build.VERSION.SDK_INT > 9) {
Strictmode.threadpolicy policy = new StrictMode.ThreadPolicy.Builder (). Permitall (). build ();
Strictmode.setthreadpolicy (policy);
}
The second approach: use Thread, Runnable, Handler (recommended)
do HTTP requests in Runnable without blocking the UI thread ~
public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
This.setcontentview (R.layout.share_mblog_view);
New Thread (runnable). Start ();
Handler Handler = new Handler () {
@Override public
void Handlemessage (msg) {
super.handlemessage ( msg);
Bundle data = Msg.getdata ();
String val = data.getstring ("value");
LOG.I ("MyLog", "Request results-->" + val);
}
Runnable Runnable = new Runnable () {
@Override public
void Run () {////
todo:http request.
msg = new Message ();
Bundle data = new Bundle ();
Data.putstring ("value", "request Result");
Msg.setdata (data);
Handler.sendmessage (msg);
}
Attached: Another solution
Android 4.1 project: Using Sina Weibo share times:
Android.os.NetworkOnMainThreadException
Online search after know because of the version of the problem, after 4.0 in the main thread in the implementation of HTTP request will be reported this error, may be afraid of HTTP request time is too long to cause the procedure of suspended animation. Then the friends on the net also gave the corresponding solution, which is called policies under the policy has countermeasures:
One: Add the following code to the OnCreate function in the activity that initiates the HTTP request:
See Strictmode document
Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder (). Detectdiskreads ().
Detectdiskwrites (). Detectnetwork (). Penaltylog (). build ());
Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder (). Detectleakedsqlliteobjects (
). Detectleakedclosableobjects (). Penaltylog (). Penaltydeath (). build ());
If you are working on a project that is not Android 4.0, you cannot see the Strictmode class. I also use the Internet to give the Com_weibo_android.jar. But the jar pack was 2.3 when it was downloaded, converted to Android 4.0 and added two lines of code to the OnCreate () function that shared the corresponding shareactivity. This would not be a mistake.
Two: Use thread, Runnable, handler these three classes:
public void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
This.setcontentview (R.layout.share_mblog_view);
New Thread (runnable). Start ();
Handler Handler = new Handler () {
@Override public
void Handlemessage (msg) {
super.handlemessage ( msg);
Bundle data = Msg.getdata ();
String val = data.getstring ("value");
LOG.I ("MyLog", "request result is-->" val);
}
Runnable Runnable = new Runnable () {
@Override public
void Run () {////
todo:http request.
msg = new Message ();
Bundle data = new Bundle ();
Data.putstring ("value", "request Result");
Msg.setdata (data);
Handler.sendmessage (msg);
}