Nizar Abdelhedi Nizar Abdelhedi - 21 days ago 6
JSON Question

ability to update ip adresse in android application

I am working on an android application which talks to web application I run the Rest server locally
but if my server ip changes or port changes i have to build the application
is there any solution that make ip adresse update himself ?
this is my code to receive my web service

public class TableauDeBordFragment extends Fragment {


public static final String SERVEL_URL = "http://192.168.2.120:8082/access-control-web/rest/roles/attendance";

List<Employe> employes;
ListView tableauDeBordListview;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

employes = new ArrayList<Employe>();
View view = inflater.inflate(R.layout.fragment_tableau_de_bord, container, false);
tableauDeBordListview = (ListView) view.findViewById(R.id.tableaudebordlist);
DownloadJSON downloadJSON = new DownloadJSON();
downloadJSON.execute();
return view;
}

public class DownloadJSON extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {
try {
URL theURL = new URL(SERVEL_URL);
BufferedReader reader = new BufferedReader(new InputStreamReader(theURL.openConnection().getInputStream(), "UTF-8"));

String jsonStr = reader.readLine();
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i=0;i<jsonArray.length();i++)
{
JSONObject jsonAttendanceItem = jsonArray.getJSONObject(i);
JSONObject jsonEmployeeItem = jsonAttendanceItem.getJSONObject("employee");
Employe employe = new Employe();
employe.setFirstName(jsonEmployeeItem.getString("firstName"));
employe.setLastName(jsonEmployeeItem.getString("lastName"));
employe.setPhoneNumber(jsonEmployeeItem.getString("phoneNumber"));
employes.add(employe);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}


return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);


ArrayAdapter<Employe> adapter = new TableDeBordCustum(getActivity(), employes);
tableauDeBordListview.setAdapter(adapter);

}
}


}

Answer

I don't think there is a straightforward solution to your problem. However, a simple workaround would be to add in your app's shared preferences the ip/port of the server. This means that whenever the it changes you don't need to rebuild the app but modify the app settings only.