J Doe
J Doe

Reputation: 31

AndroidStudio aws django: HttpUrlConnection not working

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;
}

Upvotes: 3

Views: 86

Answers (1)

Niveth Saran
Niveth Saran

Reputation: 180

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.

Upvotes: 1

Related Questions