I created a small demo program
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(new Requester()).execute();
}
static class Requester extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... voids) {
URL loginURL;
HttpURLConnection urlConnection;
try {
loginURL = new URL("https://google.com");
urlConnection = (HttpURLConnection) loginURL.openConnection();
int responseCode = urlConnection.getResponseCode();
Log.d("response", "response is" + responseCode);
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
return null;
}
}
}
I set a breakpoint at int responseCode = urlConnection.getResponseCode();
and then step through. It never gets to the next line for me to read the response code. There is no error; it just hangs forever.
I feel like this is the most basic urlconnection code I can come up with. Perhaps I'm missing something important.
Edit: I have indeed added the internet permission to my manifest. Here is my entire manifest just in case you want it:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testingcorruptbitmap">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Update: I've edited my code to include reading from the request.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(new Requester()).execute();
}
static class Requester extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... voids) {
URL loginURL;
HttpURLConnection urlConnection;
try {
loginURL = new URL("https://google.com");
urlConnection = (HttpURLConnection) loginURL.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();
Log.d("urlconnection", "line is " + line);
int responseCode = urlConnection.getResponseCode();
Log.d("urlconnection", "response is " + responseCode);
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
}
}
When I set a breakpoint at the beginning of the code and walkthrough, it freezes at InputStream in = new BufferedInputStream(urlConnection.getInputStream());
So this does not solve the issue.
Solution update: Something was wrong with the android emulated device I was working on. I restarted it and the problem is now fixed. If you know of the reason why this would happen please let me and the rest of the internet know, so we can avoid it!