Integrate oneconnect SDK for Android

OneConnect SDK Android Library

OneConnect SDK is an Android library that provides easy access to OneConnect API, allowing you to retrieve server information for VPN services. You can use this library to fetch both free and premium servers for your VPN application.

Installation

To use this library, you should add jitpack repository.

Add this to root build.gradle

allprojects {
    repositories {
        ...
        maven {
            url "https://jitpack.io"
        }
    }
}

Add this in Androidmainfest.xml

<activity
    android:name="top.oneconnectapi.app.DisconnectVPNActivity"
    android:excludeFromRecents="true"
    android:noHistory="true"
    android:taskAffinity=".DisconnectVPN"
    android:theme="@style/blinkt.dialog" />
<service
    android:name="top.oneconnectapi.app.core.OpenVPNService"
    android:permission="android.permission.BIND_VPN_SERVICE"
    android:exported="true">
    <intent-filter>
        <action android:name="android.net.VpnService" />
    </intent-filter>
</service>

To use OneConnect SDK in your Android project, add the following dependency to your app-level build.gradle file:

implementation 'com.github.oneconnectapi:OneConnectLib:v1.1.0'

Usage

Here's how you can use OneConnectLib to fetch server information for your VPN application:

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            OneConnect oneConnect = new OneConnect();
            String apiKey = "nwc6VcLemvnZQxaAgmMGMLtLMxmNT7tud.X.WXBqFbXjWogVWk"; //Sample API key
            oneConnect.initialize(this, apiKey);
            try {
                //Returns a Json string containing all the server details and credentials
                Constants.FREE_SERVERS = oneConnect.fetch(true);
                Constants.PREMIUM_SERVERS = oneConnect.fetch(false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

thread.start();

In the code snippet above:

  • Create a new thread to perform network operations asynchronously.

  • Initialize the OneConnect instance with your API key.

  • Use oneConnect.fetch(true) to retrieve free servers and oneConnect.fetch(false) to retrieve premium servers.

Connect VPN

OpenVpnApi.startVpn(this, config, countryName, userName, passWord);

Parameters:

  • this - The activity context

  • config - configuration file

  • countryName - Text to be displayed in the notification

  • userName - handle by SDK itself

  • passWord - handle by SDK itself

Disconnect VPN

OpenVPNThread vpnThread = new OpenVPNThread();

try {
    vpnThread.stop();
} catch (Exception e) {
    e.printStackTrace();
}

Parsing JSON Response

Once you have fetched server information, you can parse the JSON response to obtain the server details. Here's an example of how to convert the JSON response into an ArrayList of Countries objects:

ArrayList<Countries> servers = new ArrayList<>();

try {
    JSONArray jsonArray = new JSONArray(Constants.FREE_SERVERS);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject object = jsonArray.getJSONObject(i);
        servers.add(new Countries(
            object.getString("serverName"),
            object.getString("flag_url"),
            object.getString("ovpnConfiguration"),
            object.getString("vpnUserName"),
            object.getString("vpnPassword")
        ));
    }
} catch (JSONException e) {
    e.printStackTrace();
}

Make sure to replace Countries with the appropriate data model in your project.

Ready demo project

Here we have ready demo project , you can frog from GitHub and use

Usage

This SDK Project is Created By OneConnect Team.

This Project Content OneConnect Library

For Use, the SDK Required OneConnect API Key,

Visit on this Website https://developer.oneconnect.top and Create Account and get the Key.

Once You have the OneConnect API Key, Then You have Put inside SplashScreen.java

You will Get the Key inside API Tab https://developer.oneconnect.top/dashboard/api/ After that, You need to Put Your App Package Name Inside OneConnect API Tab.

You have to Put inside SplashScreen.java Please See Screenshot

# This Project Content AdMob & Facebook Ads those developers want to Add Another Ads SDK they need to follow the Exiting Method of Ads SDK and Also Have IAP Subscription.

And Need to Put Your Google-Services.JSON file inside Project, You will get from Firebase.

Note :- Any Developer, those who want to Use or Sell for end Client

they need to make the sufficient Changes in the Original Demo Project.

Just Smile after get the Project..... (^_^)

Last updated