我正在研究一个与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);
}
}
}
你有关于解决任何想法提出申请更新自己。我只是想让我的应用程序的用户很容易 –