Can't Connect To Internet In ONLY In My Android App. Each Method To Connect Internet Returns Null
I am using HttpURLConnection's connect method but it returns null. no other message no printstack just null. URL url = new URL('http://whatever'); URLConnection urlConnection = url
Solution 1:
Add below permissions in manifest file-
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Solution 2:
You said you are using HttpURLConnection
but in the code you poste URLConnection
. This is how you make a proper HttpURLConnection
:
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
}
You also have to do this connection outside the main thread (Using AsyncTask
or other method) or otherwise the app will crash.
Also add the right permissions for it:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Hope it helps! Good Luck!
Post a Comment for "Can't Connect To Internet In ONLY In My Android App. Each Method To Connect Internet Returns Null"