DefaultHttpClient à AndroidHttpClient

J'ai un problème avec mon code, et j'espérais de l'aide.
J'ai été tout d'abord à l'aide de ce code :

        new DefaultHttpClient().execute(new HttpGet(linkk)).getEntity().writeTo(
           new FileOutputStream(f));

Et il fonctionne très bien sur android 2.3, mais sur la 4.0, il ne le fait pas. Après quelques recherches, j'ai entendu dire que vaut mieux utiliser AndroidHttpClient et cette façon de travailler sur la 4.0 et 3.1. Le problème est que je ne sais pas si j'ai modifié mon code correctement et il n'y a pas trop d'exemples concernant AndroidhttpClient sur internet.

Voici mon code qui a été ajustée:

    AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    HttpGet request = new HttpGet(linkk);   
    HttpResponse response = client.execute(request); //here is where the exception is thrown    
    response.getEntity().writeTo(new FileOutputStream(f));

C'est ce que le logcat montre:

     01-03 01:32:11.950: W/dalvikvm(17991): threadid=1: thread exiting with uncaught exception (group=0x40a2e1f8)
01-03 01:32:11.986: E/AndroidRuntime(17991): FATAL EXCEPTION: main
01-03 01:32:11.986: E/AndroidRuntime(17991): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lacra.fbirthdays/com.lacra.fbirthdays.ListV}: android.os.NetworkOnMainThreadException
01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.os.Looper.loop(Looper.java:137)
01-03 01:32:11.986: E/AndroidRuntime(17991):   at android.app.ActivityThread.main(ActivityThread.java:4424)
01-03 01:32:11.986: E/AndroidRuntime(17991):   at java.lang.reflect.Method.invokeNative(Native Method)
01-03 01:32:11.986: E/AndroidRuntime(17991):   at java.lang.reflect.Method.invoke(Method.java:511)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at dalvik.system.NativeStart.main(Native Method)
01-03 01:32:11.986: E/AndroidRuntime(17991): Caused by: android.os.NetworkOnMainThreadException
01-03 01:32:11.986: E/AndroidRuntime(17991):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-03 01:32:11.986: E/AndroidRuntime(17991):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
InformationsquelleAutor Lara | 2012-01-02