I haven't written Android code for a long time. I found out the program I wrote before and found that it could not run away. There was no special error prompt. I saw a useful error Caused by: After checking the reason, I will report this error when I execute Http requests in the main thread after 4.0. Maybe I am afraid that the Http request will be too long to cause the program to die.
There are two solutions, namely:
The first method: ignore it directly and force it to use (strongly not recommended, but the modification is simple)
Add the following code below setContentView(.activity_main) in MainActivity file
if (.SDK_INT > 9) { policy = new ().permitAll().build(); (policy); }
The second method: use Thread, Runnable, Handler (recommended)
Make HTTP requests in Runnable without blocking the UI thread
public void onCreate(Bundle savedInstanceState) { (savedInstanceState); (.share_mblog_view); new Thread(runnable).start(); } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { (msg); Bundle data = (); String val = ("value"); ("mylog","Request result-->" + val); } } Runnable runnable = new Runnable(){ @Override public void run() { // // TODO: http request. // Message msg = new Message(); Bundle data = new Bundle(); ("value","Response Results"); (data); (msg); } }
Attachment: Another solution
Android 4.1 project: Share Times using Sina Weibo:
After searching online, I found out that it was because of the version problem. After 4.0, I would report this error when I execute Http requests in the main thread. Maybe I am afraid that the Http request will be too long to cause the program to die. Then friends online have also given corresponding solutions, which are called policies above and countermeasures below:
1: Add the following code to the onCreate function in the Activity that initiates the Http request:
//See StrictMode documentation for details(new ().detectDiskReads(). detectDiskWrites().detectNetwork().penaltyLog().build()); (new ().detectLeakedSqlLiteObjects(). detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
If the project you are working on is not Android 4.0, you cannot see the StrictMode class. I also used com_weibo_android.jar given online. However, when this jar package was downloaded, it was 2.3. You must first convert it to an Android 4.0 project, and then add the above two lines of code to the onCreate() function that shares the corresponding ShareActivity. This way, this error will not be reported.
2: Use three categories: Thread, Runnable, and Handler:
public void onCreate(Bundle savedInstanceState) { (savedInstanceState); (.share_mblog_view); new Thread(runnable).start(); } Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { (msg); Bundle data = (); String val = ("value"); ("mylog","The request result is -->" val); } } Runnable runnable = new Runnable(){ @Override public void run() { // // TODO: http request. // Message msg = new Message(); Bundle data = new Bundle(); ("value","Response Results"); (data); (msg); } }