March3April4
March3April4

Reputation: 2281

Android management API(java) - how to set OpenNetworkConfiguration

I have a policy manager server implemented in java using this library

com.google.apis:google-api-services-androidmanagement:v1-rev20211122-1.32.1

The documentation refers that I can set an openNetworkConfiguration inside my policy, and there's a function that supports it.

 Policy().setOpenNetworkConfiguration(Map<String, Object>)

I have no idea how to fill in data to the parameter Map<String, Object>, and failed to find any clue on searching google and the sample pages. (https://developers.google.com/android/management/sample-app)

How can I put data like this inside the above parameter? Is there a better sample than the link above?

"openNetworkConfiguration": {
  "NetworkConfigurations": [{
    "GUID": "a",
    "Name": "Example A",
    "Type": "WiFi",
    "WiFi": {
      "SSID": "Example A",
      "Security": "None",
      "AutoConnect": true
    }
  }
}

Upvotes: 0

Views: 306

Answers (1)

Rey V. Aquino
Rey V. Aquino

Reputation: 401

To fill in the data for the Map<String, Object> parameter in the setOpenNetworkConfiguration method, you can manually create a nested structure using Java's HashMap

import java.util.HashMap;
import java.util.Map;

// ...

Map<String, Object> openNetworkConfig = new HashMap<>();
Map<String, Object> networkConfiguration = new HashMap<>();
Map<String, Object> wifiConfig = new HashMap<>();

wifiConfig.put("SSID", "Example A");
wifiConfig.put("Security", "None");
wifiConfig.put("AutoConnect", true);

networkConfiguration.put("GUID", "a");
networkConfiguration.put("Name", "Example A");
networkConfiguration.put("Type", "WiFi");
networkConfiguration.put("WiFi", wifiConfig);

openNetworkConfig.put("NetworkConfigurations", new Object[]{networkConfiguration});

Policy policy = new Policy();
policy.setOpenNetworkConfiguration(openNetworkConfig);

Or you can use the Gson library to convert the JSON string to a Map<String, Object>

import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;


// ...

String json = "{'openNetworkConfiguration': {'NetworkConfigurations': [{'GUID': 'a', 'Name': 'Example A', 'Type': 'WiFi', 'WiFi': {'SSID': 'Example A', 'Security': 'None', 'AutoConnect': true}}]}}";

Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>() {}.getType();
Map<String, Object> openNetworkConfig = gson.fromJson(json, type);

Policy policy = new Policy();
policy.setOpenNetworkConfiguration(openNetworkConfig);

Upvotes: 2

Related Questions