-1

I am using Urls class(code given below) to set different urls I want to use in my app

    public interface Urls {
        public String SERVER_BASE_URL = "http://webaddress.com/";
    public String URL_DIRECT =SERVER_BASE_URL + "token.php";
        public String URL_DIRECT2= SERVER_BASE_URL + "token2.php";
        public String URL_DIRECT3= SERVER_BASE_URL + "token3.php";
    }

Now I want to change the value of SERVER_BASE_URL from static value to a value from my database. The IP address is correctly stored in the database. For this I use the basic code as I need this page appear only once to store the IP address.

mydatabase = openOrCreateDatabase("IP", MODE_PRIVATE, null);
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS value1(ipaddress VARCHAR);");
String a = ip.getText().toString();
mydatabase.execSQL("INSERT INTO value1 VALUES('" + a + "');");

I am getting the correct IP from database using this code

Cursor resultSet = mydatabase.rawQuery("Select * from value1", null);
                resultSet.moveToFirst();
                address = resultSet.getString(0);

But I don't know how to set this IP to the SERVER_BASE_URL programmatically. Or I want to get value from database on each activity ??? Please help!!!!

Pratheesh
  • 764
  • 1
  • 11
  • 24

1 Answers1

0

App first launch

InputActivity

XML:

<EditText
android:id = "@+id/ip_address_text"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint = "Enter your IP Address"
android:inputType = "text"/>

<Button
android:id = "@+id/button_submit"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "SUBMIT"/>

Java:

EditText ipText = (EditText) findViewById(R.id.ip_address_text); 
Button submitButton = (Button) findViewById(R.id.button_submit);
submitButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
SharedPreference sp = getSharedPreferences("ANY NAME FOR PREFERENCE", Context.MODE_PRIVATE);
sp.edit().putString("ip_address", ipText.getText().toString()).apply();
      }
    });

Script Calling Activity

Java:

Before hitting the server.

SharedPreference sp = getSharedPreferences("ANY NAME FOR PREFERENCE", Context.MODE_PRIVATE);
String ipAddress = sp.getString("ip_address", "Default value");

Updated code

public interface Urls {
SharedPreference sp = getSharedPreferences("ANY NAME FOR PREFERENCE", Context.MODE_PRIVATE);
    public String SERVER_BASE_URL = sp.getString("ip_address", "Default value");
    public String URL_DIRECT =SERVER_BASE_URL + "token.php";
    public String URL_DIRECT2= SERVER_BASE_URL + "token2.php";
    public String URL_DIRECT3= SERVER_BASE_URL + "token3.php";
    }

and use the variable url to hit the server.

Your question is not pointing to the particular part instead asking for whole implementation. Try to narrow your issue which will be helpful for the solvers to answer rather than asking for the entire code for implementation.

Good Luck!!

Reference links to use

Shared Preferences 1

Shared Preferences 2

Swaminathan V
  • 4,663
  • 2
  • 22
  • 32
  • Thanks Ragu Swaminathan. Let me try it. – Pratheesh Jan 04 '16 at 10:23
  • Also Ragu Swaminathan I dont need the whole code. I need to get the idea which can be implemented here correctly. Also I actually want to change the value in `Urls` class so that I only need to call `URL_DIRECT` or `URL_DIRECT2` in every activity instead of calling `String url = ipAddress+"token2.php"`; – Pratheesh Jan 04 '16 at 10:31
  • kindly post your url code. will me to answer better for you. – Swaminathan V Jan 04 '16 at 11:35
  • I am saying about Urls (interface class) given in my question. – Pratheesh Jan 04 '16 at 11:38