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)?