android – 在jellybean中的NetworkOnMainThreadException错误

前端之家收集整理的这篇文章主要介绍了android – 在jellybean中的NetworkOnMainThreadException错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > How do I fix android.os.NetworkOnMainThreadException?49个
我一直试图在上周左右开始工作,但仍然不知道问题是什么.它适用于Android 2.1但不适用于4.1.我已经在服务中找到了这个字符串,检查我的应用程序中的更新.
latestVersion = Integer.parseInt(getHttpString(urlLatestVersion));

它在checkForUpdate()中调用;在onStart中调用(Intent intent,int startId);.

private String getHttpString(String urlString) throws IOException {
            InputStream in = null;
            int response = -1;

            URL url = new URL(urlString);
            URLConnection conn = url.openConnection();

            if (!(conn instanceof HttpURLConnection))
                    throw new IOException("Not an HTTP connection");

            try {
                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                    httpConn.setAllowUserInteraction(false);
                    httpConn.setInstanceFollowRedirects(true);
                    httpConn.setRequestMethod("GET");
                    httpConn.connect();

                    response = httpConn.getResponseCode();
                    if (response == HttpURLConnection.HTTP_OK) {
                            in = httpConn.getInputStream();
                    }
            } catch (Exception ex) {
                    ex.printStackTrace();
                    //
                    //main error currently lies here
                    // TODO fix errors that accures on android 4.0 and above.
                    //
            }
            if (in != null) {
                    StringBuilder sb = new StringBuilder();
                    String line;

                    try {
                            BufferedReader reader = new BufferedReader(
                                            new InputStreamReader(in,"UTF-8"));
                            while ((line = reader.readLine()) != null) {
                                    sb.append(line); // .append("\n");
                            }
                    } finally {
                            in.close();
                    }
                    return sb.toString();
            } else
                    return "";

    }

这是logcat的错误

W/System.err( 7077): android.os.NetworkOnMainThreadException
W/System.err( 7077):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
W/System.err( 7077):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
W/System.err( 7077):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
W/System.err( 7077):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
W/System.err( 7077):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
W/System.err( 7077):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
W/System.err( 7077):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
W/System.err( 7077):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
W/System.err( 7077):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
W/System.err( 7077):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
W/System.err( 7077):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
W/System.err( 7077):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
W/System.err( 7077):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
W/System.err( 7077):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
W/System.err( 7077):    at com.lukemovement.roottoolBox.pro.Update.getHttpString(Update.java:166)
W/System.err( 7077):    at com.lukemovement.roottoolBox.pro.Update.checkForUpdate(Update.java:98)
W/System.err( 7077):    at com.lukemovement.roottoolBox.pro.Update.onStart(Update.java:75)
W/System.err( 7077):    at android.app.Service.onStartCommand(Service.java:450)
W/System.err( 7077):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2620)
W/System.err( 7077):    at android.app.ActivityThread.access$1900(ActivityThread.java:143)
W/System.err( 7077):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
W/System.err( 7077):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 7077):    at android.os.Looper.loop(Looper.java:137)
W/System.err( 7077):    at android.app.ActivityThread.main(ActivityThread.java:4935)
W/System.err( 7077):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 7077):    at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err( 7077):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
W/System.err( 7077):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
W/System.err( 7077):    at dalvik.system.NativeStart.main(Native Method)
I/Example update( 7077): Invalid int: ""

所以android放弃了向后兼容还是只是忘了添加这个位?

香港专业教育学院读过我需要使用AsyncTask.但我似乎无法让它为我工作,有人可以帮我这个吗?

解决方法

从Android API v15开始,它不需要在主线程上运行繁重的进程.
因此,您应该将您的逻辑移动到另一个线程,如下面的源代码
new Thread(new Runnable() {
   public void run() {
        // your logic
   }                        
}).start();

更多信息请参考:Responsiveness

原文链接:https://www.f2er.com/android/310083.html

猜你在找的Android相关文章