LorenzoBerti
LorenzoBerti

Reputation: 6974

Where I should save configuration data for backend Android

sorry if it is a stupid question but I have little experience with development app. I'm developing a native App that make ajax call to my backend (public API) , My question is: Where I should save configuration for ajax call? For now I have create a custom static class named ConfigData and in my app I call this class like

ConfigData.apiUrl

And in my ConfigData class I have

public static String apiUrl= "www.mysite.com/public/api";

But Is there a better method?

Upvotes: 4

Views: 408

Answers (3)

Eoin
Eoin

Reputation: 4066

One possible method is to use buildConfigField in you build.gradle

productFlavors {
    development {
        ....
        buildConfigField "String", "API_URL", "\"www.mysite.com/public/api\""
    }
}

You can then access it via BuildConfig.API_URL in code

You can define different urls for different build flavors, staging, production etc.

https://developer.android.com/studio/build/gradle-tips.html#share-custom-fields-and-resource-values-with-your-app-code

Upvotes: 4

Muhammad Hamza Shahid
Muhammad Hamza Shahid

Reputation: 956

You can use string.xml to save backend server URLs, Please try to add separate strings for base URL and each endpoint. Like

<string name="base_url">www.mysite.com/public/api/</string>
<string name="login">login.php</string>
<string name="get_data">getData.php</string>

And you can get above strings in java file like

String url=getResources().getString(R.string.base_url)+getResources().getString(R.string.login);

Upvotes: 0

user7154703
user7154703

Reputation:

If it is just simple key value set, you may save it to Shared Preference. Follow this url for details

https://developer.android.com/training/basics/data-storage/shared-preferences.html

In case, it is bit complex, you can save it to sqlite database

Upvotes: 1

Related Questions