2013年9月12日星期四

android read server unsuccessful

/ / http get
try {
HttpClient httpclient = new DefaultHttpClient ();
HttpGet httpget = new HttpGet ("http://192.168.1.144/plus/taomei02/items_list2 . php? action = android ");
HttpResponse response = httpclient.execute (httpget);
HttpEntity entity = response.getEntity ();
is = entity.getContent ();
} catch (Exception e) {
Log.e ("log_tag", "Error in http connection" + e.toString ( ) ) ;
}


Tip Error : E / log_tag (5487): Error in http connectionandroid.os.NetworkOnMainThreadException

system is not allowed in the main thread to perform a network request , then how to solve it
------ Solution ------------------- -------------------------
4.0 after the main thread in the UI are not allowed to perform network requests , because it will cause slow response or a more severe UI the ANR, affecting the user experience .
You can make a new thread in the network operation .
        (new Thread() {

            @Override
            public void run() {
                // 这里做网络操作
            }

        }).start();

------ eference ------------------------------------- -
upstairs positive solutions, in addition there is a way :
After the program starts by adding the following code

 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()  
                .penaltyLog()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
                .detectLeakedClosableObjects()
                .penaltyLog()
                .penaltyDeath()
                .build());


------ eference ---------------------- -----------------
LS two kinds of finished, to mix points
------ eference ----------- ----------------------------
thank you with thread after it.

没有评论:

发表评论