I want to access variable value of normal Android Activity
in the Java Class
.
First
I have String
name link_sp
in which I have value of url link that I have stored in SharedPreferences
MainActivity.java
SharedPreferences sp;
String link_sp;
public static String linkUrl = "";
// onCreate Method {
...
sp = getSharedPreferences("MySharedPref", MODE_PRIVATE);
link_sp = sp.getString("link", "");
// Setting value of link_sp to static string linkUrl
linkUrl = link_sp;
...
}
// Also I have this method in MainActivity
public static String getLink() {
return linkUrl;
}
Now I have some url in link_sp
which I need in another class named RetrofitClient
RetrofitClient.java
// Getting value of link_sp here
String BASE_URL = MainActivity.getLink();
...
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Error:
Error when variable was empty as suggested by user cahyo
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.abc/com.abc.remotegsmmodem.MainActivity}: java.lang.IllegalArgumentException: Expected URL scheme 'http' or 'https' but no colon was found
Even if I didn't get an error I am not confident about this method that I am using, please anyone what is a STANDARD WAY to get the value of a variable from ACTIVITY in JAVA CLASS. Thank You