开发者

HonyComb and DefaultHttpClient

In my code I have this

        Log.d("WFlog (executeRequest)", request.toString()) ;
        httpResponse = client.execute(request);  
        Log.d("WFlog (execute)", request.toString()) ;

running the app using Android 2.2 is going开发者_JAVA百科 fine and in logcat I see both log lines.

Now running the app using HonyComb for the same piece of code it seems I never get passed the client.execute correctly. The last log line I get is the one having "WFlog (executeRequest)".

After this I see the following:

01-27 21:54:45.169: WARN/System.err(390): android.os.NetworkOnMainThreadException
01-27 21:54:45.196: WARN/System.err(390):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1069)
01-27 21:54:45.196: WARN/System.err(390):     at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:368)
01-27 21:54:45.205: WARN/System.err(390):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
01-27 21:54:45.215: WARN/System.err(390):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
01-27 21:54:45.237: WARN/System.err(390):     at java.net.Socket.connect(Socket.java:901)

Any idea what I should look for?


There are new policies that allow application and operating system developers to set performance expectations for code executing on certain threads. You've attempted to invoke blocking network api's on the ui thread of your application. Google has put in place a system that lets you know this is a bad idea and you can resolve this issue by executing your request in a separate thread or an asyncTask.

Please read this blog post. You can find information about doing async / multi-threaded apps all over SO and Google.


Thanks Nick. It worked for me. For dev purpose I just set the Thread Policy to default by doing this

*ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);*

This should be removed for the final version.


abhinaw's suggestion written as reflection, so code also works on older API versions:

   try {
        Class strictModeClass=Class.forName("android.os.StrictMode");
        Class strictModeThreadPolicyClass=Class.forName("android.os.StrictMode$ThreadPolicy");
        Object laxPolicy = strictModeThreadPolicyClass.getField("LAX").get(null);
        Method method_setThreadPolicy = strictModeClass.getMethod(
                "setThreadPolicy", strictModeThreadPolicyClass );
        method_setThreadPolicy.invoke(null,laxPolicy);
    } catch (Exception e) {

    }

Yes, this should be removed for the final version.


I used an asynctask, saw it in other threads, and it worked fine....i create a subclass where i wanted to call the network connection. i don't know if it is the best way but this code worked for me....

public miclase{

    public boolean comprobarMP3(int canal){
        boolean retorno=true;
        //This commented Code is what it was NOT working
        //swr= new ServicioWebRest();
        //contenido=swr.obtieneContenido(Channels.getInstance().getChannel().get(canal).getId());
        try{
           //this.get() was important, i had troubles in recovering the objet, .get() solved it
            contenido=new getContenidoAsync().execute(canal).get();
        }catch(Exception e){
            contenido=null;
        }
    }

    //Clase interna para acceder a los webservice de un modo asincrono en otro hilo
    //Desde el sdk 11, las politicas de seguridad de android no permiten acceder a internet desde el hilo principal 
    public class getContenidoAsync extends AsyncTask<Integer, Void, Contenido>{
        Contenido c=new Contenido();

        @Override
        protected Contenido doInBackground(Integer... urls) {
        //aqui el codigo q sea, yo llamo a este que llama a otra clase que es el q llama a http 
            return new ServicioWebRest().obtieneContenido(Channels.getInstance().getChannel().get(urls[0]).getId());
        }

        @Override
        protected void onPostExecute(Contenido result) {
            c=result;
        }       
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜