Error while executing Http message in cells

I have two Android mobile devices: one v2.3 api 9 and one cellular v3.1 I want to publish an http api link for sms code. It looks like I got a honeycomb error, and the other mobile is working fine, this is the code

public void sendSMS(String phone_num, int password)
{
try
{               
    HttpClient hc = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://www.google.com/");
    hc.execute(post); // I got an error here    
}
catch(IOException e)
{

    Log.e("error", "error");
}  
} 
+3
source share
4 answers

StrictMode is included in HoneyComb, you must disable it to avoid NetworkOnMainThreadException

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

but this is not recommended , use Asynctask , here you can find an example:

http://developer.android.com/reference/android/os/AsyncTask.html

+11
source

- Android Honeycomb. , , NetworkOnMainThreadException

Android Honeycomb , .

, , : StrictMode$AndroidBlockGuardPolicy.onNetwork

, , HTTP-.

+6

100% !

super.onCreate protected void onCreate:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

! ! , ~

+5

Async Task.So httppost . Async, http post. . http://www.vogella.com/articles/AndroidPerformance/article.html

+2

All Articles