Android .connect () program crashes on Android, not connection

I tried to figure this out for hours. I searched for the answers, but I can not find the answer. I never bothered to ask, and this is the first time I ask a question. Basically what I am doing is breaking my encoding for organization purposes. The following snippet works very well, but when I take it and put it in another class, urlConnect (); It connects just fine. I noted this below.

public String downloadUrl(String strUrl) throws IOException{ String data = ""; InputStream iStream = null; HttpURLConnection urlConnection = null; try{ URL url = new URL(strUrl); String line = ""; // Creating an http connection to communicate with url urlConnection = (HttpURLConnection) url.openConnection(); /* Connecting to url */ urlConnection.connect(); <-------------------------works in this snippet /* Reading data from url */ iStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); while((line = br.readLine()) != null){ sb.append(line); } data = sb.toString(); br.close(); } catch(Exception e){ Log.d("Exception while downloading url", e.toString()); } finally { iStream.close(); urlConnection.disconnect(); } return data; } 

So this next snippet is pretty much identical. But for some reason he does not want to connect:

 public String getJSONobject(String strUrl) throws IOException{ String data = ""; InputStream iStream = null; HttpURLConnection urlConnection = null; try{ URL url = new URL(strUrl); String line = ""; urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); <------------------ Does not work iStream = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); StringBuffer sb = new StringBuffer(); while((line = br.readLine()) != null){ sb.append(line); } data = sb.toString(); br.close(); iStream.close(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } finally { if(null != urlConnection) { urlConnection.disconnect(); } return data; 

LogCat:

 >at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131) >at java.net.InetAddress.lookupHostByName(InetAddress.java:385) >at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) >at java.net.InetAddress.getAllByName(InetAddress.java:214) >at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) >at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) >at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) >at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) >at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) >at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315) >at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461) >at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) >at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289) >at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) >at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80) >at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165) >at com.navsquad.shsu.katlas2.RouteJSONobject.getJSONobject(RouteJSONobject.java:57) >at com.navsquad.shsu.katlas2.CreateRoute.create(CreateRoute.java:41) >at com.navsquad.shsu.katlas2.MainActivity$3.onMapClick(MainActivity.java:84) >at com.google.android.gms.maps.GoogleMap$6.onMapClick(Unknown Source) >at com.google.android.gms.internal.t$a.onTransact(Unknown Source) >at android.os.Binder.transact(Binder.java:326) >at com.google.android.gms.maps.internal.IOnMapClickListener$Stub$Proxy.onMapClick(IOnMapClickListener.java:93) >at maps.isb(Unknown Source) >at maps.yvc(Unknown Source) >at maps.y.bf.onSingleTapConfirmed(Unknown Source) >at maps.dvonSingleTapConfirmed(Unknown Source) >at maps.djhandleMessage(Unknown Source) >at android.os.Handler.dispatchMessage(Handler.java:99) >at android.os.Looper.loop(Looper.java:137) >at android.app.ActivityThread.main(ActivityThread.java:5059) >at java.lang.reflect.Method.invokeNative(Native Method) >at java.lang.reflect.Method.invoke(Method.java:511) >at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) >at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) >at dalvik.system.NativeStart.main(Native Method) 

So, to make everything clear, everything I did was a separate class for my own organization. I originally included the parser, but now I have divided it into different classes, so I can use other parsers with the string / JSON Object. If you could help me figure out what is different from the previous ones, I would appreciate it. The source class was in an implemented AsyncTask class.

+4
source share
1 answer
 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131) 

It seems you were trying to cause an api network lock in your application's UI thread.

In Android 3.0 and above, there is a new application policy that does not allow network calls in the main thread.

Please view http://developer.android.com/reference/android/os/StrictMode.html .

You can disable strictmode by putting the following file fragment. (I would not recommend this approach in a production environment.)

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

All Articles