I set up django on AWS account and trying to connect it to Android Studio using the following code. I can check that link is working in the browser but using Log.d I debugged that problem is in urlConnection.connect().
Android Studio gives no error but any log after that is not printed.
I have also added permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
I am a beginner please help me.
protected String doInBackground(Void... params) {
try {
String site_url_json = "http://3.16.54.157:8002/post/";
URL url = new URL(site_url_json);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resultJson = buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return resultJson;
}
The easy way to implement this is to use this attribute to your AndroidManifest.xml where you allow all http for all requests:
android:usesCleartextTraffic="true"
This gets ignored on older platforms. You will have to set this to true only in Android Pie and above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With