# Integrate oneconnect SDK for Android

Current version: ![1.0.9](https://jitpack.io/v/oneconnectapi/OneConnectLib.svg)

## [​](https://jitpack.io/#AnchorFreePartner/hydra-sdk-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.&#x20;

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:

```gradle
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

{% embed url="https://github.com/oneconnectapi/OneConnectSdk-demo-android" %}

**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](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/](https://developer.oneconnect.top/dashboard/api/) After that, You need to Put Your App Package Name Inside OneConnect API Tab.



<figure><img src="https://81250416-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPyYfGsGkm4GPlQG0OjaM%2Fuploads%2FOz3qjMGGFhmiCHnlE9ZZ%2FScreenshot%202023-09-02%20at%2003-56-44%20Api.png?alt=media&#x26;token=f61888a9-4ad7-4af9-a4d4-759827d9c838" alt=""><figcaption></figcaption></figure>

#### You have to Put inside SplashScreen.java Please See Screenshot

![](https://oneconnect.top/serverkey.png)

\# 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..... **(^\_^)**

####
