2016-07-03 60 views
0

我正在研究一个与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); 

    } 
} 

}

回答

0

我不认为这是一个简单的解决您的问题。 但是,一个简单的解决方法是在您的应用程序的共享首选项中添加服务器的IP /端口。这意味着只要它发生变化,您就不需要重新构建应用程序,只需修改应用程序设置即可。

+0

你有关于解决任何想法提出申请更新自己。我只是想让我的应用程序的用户很容易 –

0

尝试的其他概念,,就像把你的代码在服务器联机,那么你就需要在URL中使用IP引用代码

相关问题