0

when I connect to my server I display it's address in the java code like that :

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://192.168.1.13/spotnshare/syncAddress.php");

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);

But we told me that having the IP address in the code wasn't correct and that I should put it in a config file as an alias I guess.

Does anyone know about that ?

Karly
  • 389
  • 2
  • 7
  • 25

2 Answers2

2

Add an element to the app's config file:

<resources>
  <string name="syncAddress">http://192.168.1.13/spotnshare/syncAddress.php</string>

Then set a static string to hold it, and read the config:

public static String SYNCADDRESS = getResources().getString(R.string.syncAddress);

Per Accessing Resources:

When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory. For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R.drawable.icon). This integer is the resource ID that you can use to retrieve your resource.

tephyr
  • 1,001
  • 2
  • 14
  • 26
  • public static String SYNCADDRESS = R.string.syncAddress; type of R.string.syncAddress is int, it's doesn't to be work. – silentnuke Aug 01 '12 at 22:51
  • Good catch: I corrected the example code (the resource must be called with getResources().getString()). – tephyr Aug 02 '12 at 16:15
0

create a constant

public static final String URL_SYNCADRESS = "your url";

then use

HttpPost httppost = new HttpPost(URL_SYNCADRESS);
silentnuke
  • 2,622
  • 1
  • 18
  • 17