1

Is it necessary to write URL of web-service in each page in android application or any other way where i can save URL once for the whole application.

I have a android application where i am calling web-service . I am new to android so i don't have idea of saving the URL globally. How i can save the URL once in the whole application.

private final String URL = "http://192.1.1.1/Service1.asmx";

Instead of writing in every .java file what else i can do.

  • Create a new class and add public final String URL = "http://192.1.1.1/Service1.asmx"; and call from all the class like (YourClassName.URL) – Amsheer Jul 13 '15 at 12:58
  • Thanks ..If i want to get the URL value from application and set that value globally what code i have to write. – Asiyana2010 Jul 13 '15 at 13:04
  • And in my comment there is one mistake you need to add the variable as static. public static final String URL = "192.1.1.1/Service1.asmx"; – Amsheer Jul 13 '15 at 13:16
  • Exactly i did that its working fine. – Asiyana2010 Jul 14 '15 at 04:04
  • I just add this answer as comment before the other other people answering. And you accept someone else answer. Anyway your problem solved. well fine. – Amsheer Jul 14 '15 at 06:03

5 Answers5

3

Create a class in java and declare a String field like the following in that class,

public class MyConstants {
    public static final String Url = "http://192.1.1.1/Service1.asmx";
}

then you can access it globally from any class like the following

String url = MyConstants.Url;

As Url is a static final field of MyConstants class, so you can access it just with the name of the class(without creating the object of the class with new Operator). i.e MyConstants in this case.

For Learning more about static and to see how this thing works, Please refer to this link

final means that the value cannot be changed after initialization, that's what makes it a constant. static means that instead of having space allocated for the field in each object, only one instance is created for the class.

So, static final means only one instance of the variable no matter how many objects are created and the value of that variable can never change.

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
1

If you don't want to create a class you can just put it on res/values/strings

<string name="URLWebService">http://192.1.1.1/Service1.asmx</string>

Then in each class you need it do this :

getResources().getString(R.string.URLWebService);

And you can do it directly or just put a

public static final String URL = getResources().getString(R.string.URLWebService);

You can use what you want all are going to work fine.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

create a global constant class and save constants there :

public class Constants {
    public static final String Url = "http://192.1.1.1/Service1.asmx";
}

And access it anywhere in application by just calling Constants.Url

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
ReZa
  • 1,273
  • 2
  • 18
  • 33
0

You can also use the string file in res/values.

You can you like following:

<string name="ws_url">http://192.1.1.1/Service1.asmx</string>

and use it so: String wsUrl = getString(R.string.ws_url)

MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35
0

Create common class YourGlobalClass:

public class YourGlobalClass{
public static final String URL = "192.1.1.1/Service1.asmx";
}

And wherever you want just call the class name. your variable name ex. YourGlobalClass.URL;

Amsheer
  • 7,046
  • 8
  • 47
  • 81
  • declare that String ***static*** i.e public static final String URL = "192.1.1.1/Service1.asmx"; – nobalG Jul 13 '15 at 13:13
  • 1
    You have to put `public static final String URL = "192.1.1.1/Service1.asmx";` look at [@znobalG](http://stackoverflow.com/a/31384209/4385913) answer – Skizo-ozᴉʞS ツ Jul 13 '15 at 13:14
  • Thanks. You are right. I just copy that line from the question. And I add one comments in question i did the same mistake. Thanks for noticing this. – Amsheer Jul 13 '15 at 13:15