java–NetWork On主线程异常

java–NetWork On主线程异常,第1张

概述嗨,我尝试使用以下方法检查网络连接和Internet存在check=newConnectionDetector(getApplicationContext());conn=check.isConnectingToInternet();publicclassConnectionDetector{privateContext_context;publicConnectionDetector(Contextcontext){thi

嗨,我尝试使用以下方法检查网络连接和Internet存在

check = new ConnectionDetector(getApplicationContext());

conn = check.isConnectingToInternet();

public class ConnectionDetector {private Context _context;public ConnectionDetector(Context context){    this._context = context;}/** * Checking for all possible internet provIDers * **//*public boolean isConnectingToInternet(){    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);      if (connectivity != null)      {          NetworkInfo[] info = connectivity.getAllNetworkInfo();          if (info != null)              for (int i = 0; i < info.length; i++)                  if (info[i].getState() == NetworkInfo.State.CONNECTED)                  {                      return true;                  }      }      return false;}*/public boolean isConnectingToInternet(){    try{        ConnectivityManager cm = (ConnectivityManager)_context.getSystemService                                                    (Context.CONNECTIVITY_SERVICE);        NetworkInfo netInfo = cm.getActiveNetworkInfo();        Log.d("NetInfo", String.valueOf(netInfo));        if (netInfo != null && netInfo.isConnected())        {            //Network is available but check if we can get access from the network.            URL url = new URL("http://www.Google.com/");            httpURLConnection urlc = (httpURLConnection) url.openConnection();            urlc.setRequestProperty("Connection", "close");            urlc.setConnectTimeout(2000); // Timeout 2 seconds.            urlc.connect();            Log.d("NetInfo Response Code", String.valueOf(urlc.getResponseCode()));           // Toast.makeText(getApplicationContext(), String.valueOf(urlc.getResponseCode()), Toast.LENGTH_LONG).show();            if (urlc.getResponseCode() == 200)  //Successful response.            {                return true;            }             else             {                 Log.d("NO INTERNET", "NO INTERNET");                return false;            }        }    }    catch(Exception e)    {        e.printstacktrace();    }    return false;}

}

注意 :

但这将返回networkonmainthread异常,如下所示.任何人都建议我犯了什么错误…….

03-27 12:53:35.617: W/System.err(1095): androID.os.networkonmainthreadException03-27 12:53:35.627: W/System.err(1095):     at androID.os.StrictMode$AndroIDBlockGuardPolicy.onNetwork(StrictMode.java:1084)03-27 12:53:35.637: W/System.err(1095):     at java.net.InetAddress.lookupHostByname(InetAddress.java:391)03-27 12:53:35.637: W/System.err(1095):     at java.net.InetAddress.getAllBynameImpl(InetAddress.java:242)03-27 12:53:35.647: W/System.err(1095):     at java.net.InetAddress.getAllByname(InetAddress.java:220)03-27 12:53:35.647: W/System.err(1095):     at libcore.net.http.httpconnection.<init>(httpconnection.java:71)03-27 12:53:35.657: W/System.err(1095):     at libcore.net.http.httpconnection.<init>(httpconnection.java:50)03-27 12:53:35.668: W/System.err(1095):     at libcore.net.http.httpconnection$Address.connect(httpconnection.java:351)03-27 12:53:35.668: W/System.err(1095):     at libcore.net.http.httpconnectionPool.get(httpconnectionPool.java:86)03-27 12:53:35.677: W/System.err(1095):     at libcore.net.http.httpconnection.connect(httpconnection.java:128)03-27 12:53:35.687: W/System.err(1095):     at libcore.net.http.httpEngine.openSocketConnection(httpEngine.java:308)03-27 12:53:35.699: W/System.err(1095):     at libcore.net.http.httpEngine.connect(httpEngine.java:303)03-27 12:53:35.699: W/System.err(1095):     at libcore.net.http.httpEngine.sendSocketRequest(httpEngine.java:282)03-27 12:53:35.707: W/System.err(1095):     at libcore.net.http.httpEngine.sendRequest(httpEngine.java:232)03-27 12:53:35.718: W/System.err(1095):     at libcore.net.http.httpURLConnectionImpl.connect(httpURLConnectionImpl.java:80)03-27 12:53:35.727: W/System.err(1095):     at com.slet.routemytrips.beta.ConnectionDetector.isConnectingToInternet(ConnectionDetector.java:50)03-27 12:53:35.727: W/System.err(1095):     at com.slet.routemytrips.beta.disclaimer.onClick(disclaimer.java:178)03-27 12:53:35.738: W/System.err(1095):     at androID.vIEw.VIEw.performClick(VIEw.java:3480)03-27 12:53:35.738: W/System.err(1095):     at androID.vIEw.VIEw$PerformClick.run(VIEw.java:13983)03-27 12:53:35.748: W/System.err(1095):     at androID.os.Handler.handleCallback(Handler.java:605)03-27 12:53:35.757: W/System.err(1095):     at androID.os.Handler.dispatchMessage(Handler.java:92)03-27 12:53:35.757: W/System.err(1095):     at androID.os.Looper.loop(Looper.java:137)03-27 12:53:35.767: W/System.err(1095):     at androID.app.ActivityThread.main(ActivityThread.java:4340)03-27 12:53:35.777: W/System.err(1095):     at java.lang.reflect.Method.invokeNative(Native Method)03-27 12:53:35.777: W/System.err(1095):     at java.lang.reflect.Method.invoke(Method.java:511)03-27 12:53:35.787: W/System.err(1095):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)03-27 12:53:35.797: W/System.err(1095):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)03-27 12:53:35.807: W/System.err(1095):     at dalvik.system.NativeStart.main(Native Method)03-27 12:57:05.237: D/dalvikvm(90): GC_CONCURRENT freed 666K, 10% free 12624K/14023K, paused 6ms+10ms

解决方法:

您无法在主线程上发出http请求,这会导致UI冻结.所以它引发了异常.您需要在AsyncTask或其他Thread中执行此 *** 作.

总结

以上是内存溢出为你收集整理的java – NetWork On主线程异常全部内容,希望文章能够帮你解决java – NetWork On主线程异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1115550.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存