0

I'm new to Android development, and with some consulting on the internet I came up with the following code to connect to a Url, POST a string content and read it's response.

private static java.lang.String getRequest(java.lang.String Url, java.lang.String PostContent)
{
    java.lang.StringBuilder content = new java.lang.StringBuilder();
    try
    {
        java.lang.String line;
        java.net.URL url = new java.net.URL(Url);
        if (cweb.companion.MainActivity.url.startsWith("https"))
        {
            javax.net.ssl.HttpsURLConnection httpsConnection = (javax.net.ssl.HttpsURLConnection)url.openConnection();
            httpsConnection.setSSLSocketFactory((javax.net.ssl.SSLSocketFactory)javax.net.ssl.SSLSocketFactory.getDefault());
            httpsConnection.setRequestMethod("POST");
            httpsConnection.setDoOutput(true);
            httpsConnection.setRequestProperty("Content-Type", "text/plain");
            httpsConnection.addRequestProperty("Content-Length", java.lang.String.valueOf(PostContent.length()));
            java.io.OutputStreamWriter wr = new java.io.OutputStreamWriter(httpsConnection.getOutputStream());
            wr.write(PostContent);
            wr.flush();
            wr.close();
            java.io.BufferedReader bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(httpsConnection.getInputStream()));
            while ((line = bufferedReader.readLine()) != null) { content.append(line + "\n"); }
            bufferedReader.close();
        }
        else
        {
            java.net.HttpURLConnection httpConnection = (java.net.HttpURLConnection)url.openConnection();
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);
            httpConnection.setRequestProperty("Content-Type", "text/plain");
            httpConnection.addRequestProperty("Content-Length", java.lang.String.valueOf(PostContent.length()));
            java.io.OutputStreamWriter wr = new java.io.OutputStreamWriter(httpConnection.getOutputStream());
            wr.write(PostContent);
            wr.flush();
            wr.close();
            java.io.BufferedReader bufferedReader = new java.io.BufferedReader(new java.io.InputStreamReader(httpConnection.getInputStream()));
            while ((line = bufferedReader.readLine()) != null) { content.append(line + "\n"); }
            bufferedReader.close();
        }
        return content.toString();
    } catch (java.lang.Exception e) { return "ERROR{" + e.getMessage() + "}"; }
}

I left it very "duplicated" to help me debugging (I know could push out some code to other methods).

That code is in the MainActivity that implements like this:

public class MainActivity extends androidx.appcompat.app.AppCompatActivity
{
    private static java.lang.String getRequest(java.lang.String Url, java.lang.String PostContent)
    {
        //my code (as posted earlier)
    }

    @Override protected void onCreate(android.os.Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        //here i call the code to something like:
        java.lang.String retHTTP = getRequest("http://httpaddress.example.com", "hello world!");
        java.lang.String retHTTPS = getRequest("https://httpsaddress.example.com", "hello world!");
    }
}

If I target a HTTPS address the function throws and empty exception and ends (the getMessage() method of the Exception is a null/empty string) and if it targets a HTTP address it gives me a "Cleartext HTTP traffic to localhost not permitted" message.

My manifest and Gradle are like this:

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:targetSandboxVersion="1" package="cweb.companion">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <application
        android:usesCleartextTraffic="true"
        android:allowBackup="true"
        android:icon="@drawable/logo_standard"
        android:label="@string/app_name"
        android:roundIcon="@drawable/logo_standard"
        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>

Gradle

apply plugin: 'my.application.com'
android
    {
        compileSdkVersion 29
        buildToolsVersion "29.0.3"
        defaultConfig
                {
                    applicationId "my.application.com"
                    minSdkVersion 23
                    targetSdkVersion 29
                    versionCode 1
                    versionName "1.0"
                    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
                }
        buildTypes
                {
                    release
                            {
                                minifyEnabled true
                                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                            }
                }
        sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/'] } }
    }
dependencies
    {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'androidx.appcompat:appcompat:1.0.2'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.0'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    }

So, why the connection does not happen (I have the server side code as well and nothing is even sent to it, in both cases)?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
SammuelMiranda
  • 420
  • 4
  • 29
  • It will be very easy for you if you use [retrofit](https://github.com/square/retrofit) or some other client library. Few lines of code will do what you want. – Afsar edrisy Feb 28 '20 at 13:22
  • I don't work with `HttpUrlConnection` but every Http call shouldn't be done at main thread. It just won't work. But yeah, `Retrofit` will be better alternative and it even have built-in thread handling. – SkypeDogg Feb 28 '20 at 13:24
  • @Afsaredrisy maybe, but i'm knew to this IDE and language, so merge things is complicated, but wouldn't it work? is anything wrong here? – SammuelMiranda Feb 28 '20 at 13:24
  • @SkypeDogg did not know that, so have to be in a different class? – SammuelMiranda Feb 28 '20 at 13:26
  • For Http call you need add network security config file see [this](https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted) & You should call `getRequest()` method from background thread use [Asyncktask](https://developer.android.com/reference/android/os/AsyncTask) or Handler for that – Afsar edrisy Feb 28 '20 at 13:27
  • 1
    Just make the call inside `AsyncTask` even inside same activity – SkypeDogg Feb 28 '20 at 13:27
  • Are you using a real device or an emulated device to test? I had the same Problem with a Samsung S9. After an update it started to throw a security exception for HTTP and only HTTPS calls were permitted. – Dominik Wuttke Feb 28 '20 at 13:29
  • @Afsaredrisy tryed that post before nothing changed with the security file, but never done in a Asynctask so maybe the problem is there as well (requires both) - i'll read about AsyncTask and try to implement to see if it solves the issue. – SammuelMiranda Feb 28 '20 at 13:29
  • @DominikWuttke emulated, by Android studio itself – SammuelMiranda Feb 28 '20 at 13:29
  • @SammuelMiranda Here you have great explanation of `AsyncTask`: https://stackoverflow.com/a/25647882/7074278 . Happy Coding ;) – SkypeDogg Feb 28 '20 at 13:31
  • @SkypeDogg thankt, will do – SammuelMiranda Feb 28 '20 at 13:31

1 Answers1

1

For HTTP request as you said that you have added security configuration to your project then here is working HTTP call

...

 java.net.URL url = new java.net.URL(Url);
          ...
         else
        {
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000000);
            conn.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("POST");
            OutputStream os = conn.getOutputStream();
            os.write(PostContent.getBytes("UTF-8"));
            os.close();
            // read the response
            InputStream in = new BufferedInputStream(conn.getInputStream());

            String result = IOUtils.toString(in, "UTF-8");
            conn.disconnect();
            return result;
        }
    } catch (java.lang.Exception e) { return "ERROR{" + e.getMessage() + "}"; }

...

Now consider to do network operation on background thread So you can make a call to getRequest method as follows.


  protected void onCreate(android.os.Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        //here you call the code to something like in Background thread:
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                java.lang.String retHTTP = getRequest("http://httpaddress.example.com", "hello world!");
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    }


Same you can success HTTPS as well with SSL.

Note: Use implementation 'org.apache.commons:commons-io:1.3.2' for IOUtils in app.gradle file.

Afsar edrisy
  • 1,985
  • 12
  • 28
  • everything freezes at conn.getOutputStream() so far, still working on it though - i'll update my question with the code changes as soon as i exhaust all attempts – SammuelMiranda Feb 28 '20 at 14:39
  • worked! did not put the implementation tag though, but used the xml solution with only the tag (no domain tag) - in the end isn't exactly like your post but took a lot from it and for the "how to do asynctask" post as well. Thanks everyone for the help! – SammuelMiranda Feb 28 '20 at 17:26