4

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?

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.