我正在研究一個與web應用程序交談的android應用程序我在本地運行Rest服務器 但是如果我的服務器ip更改或端口更改,我必須構建應用程序 有沒有解決方案使IP地址更新自己? 這是我的代碼,即可獲得我的web服務在android應用程序中更新ip地址的能力
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);
}
}
}
你有關於解決任何想法提出申請更新自己。我只是想讓我的應用程序的用戶很容易 –