【问题标题】:ability to update ip address in Android application在 Android 应用程序中更新 IP 地址的能力
【发布时间】:2016-07-03 16:30:55
【问题描述】:

我正在开发一个与 Web 应用程序对话的 Android 应用程序我在本地运行 Rest 服务器 但如果我的服务器 ip 更改或端口更改,我必须构建应用程序 有什么解决方案可以让自己更新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);

        }
    }
}

【问题讨论】:

    标签: android json rest


    【解决方案1】:

    我认为您的问题没有直接的解决方案。 但是,一个简单的解决方法是在您的应用程序的共享首选项中添加服务器的 ip/端口。这意味着每当它发生变化时,您无需重新构建应用程序,而只需修改应用程序设置。

    【讨论】:

    • 您对解决方案有任何想法,让应用程序自行更新。我只是想让我的应用程序的用户更容易
    【解决方案2】:

    尝试其他概念,例如将您的代码放在在线服务器中,然后您不需要在引用代码的 URL 中使用 IP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2011-05-12
      • 2011-02-24
      相关资源
      最近更新 更多