我正在尝试创建一个包含从服务器登录和注销的函数的类,服务器的 IP 地址在函数的开头以静态方式定义,如下所示:
private static String loginURL = "http://192.168.1.5/android_login_api/"
我想知道初始化条件的 loginURL 的最佳方法。例如:
if(this.isConnectedToServer("192.168.1.5", 500)) {
private static String loginURL = "http://192.168.1.5/android_login_api/";
}
if(this.isConnectedToServer("192.168.1.9", 500)) {
private static String loginURL = "http://192.168.1.9/android_login_api/";
}
我想将 loginURL 作为参数传递给这个类的每个函数,然后当我调用它们时,我使用这个条件来定义 IP 地址,但我认为这太傻了。
这是我的课:
public class UserFunctions {
private JSONParser jsonParser;
private static String loginURL = "http://192.168.1.5/android_login_api/";
private static String mplayerURL = "http://192.168.1.5/android_mplayer_api/";
private static String registerURL = "http://192.168.1.5/android_login_api/";
private static String login_tag = "login";
private static String register_tag = "register";
private static String delete_tag = "delete";
private static String update_tag = "update";
private static String music_tag = "music";
private static String getusers_tag = "getusers";
// constructor
public UserFunctions(){
jsonParser = new JSONParser();
}
/**
* function make Login Request
* @param email
* @param password
* */
public JSONObject loginUser(String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
Log.i("JSON", json.toString());
return json;
}
public JSONObject deleteUser(String email){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", delete_tag));
params.add(new BasicNameValuePair("email", email));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
Log.i("JSON", json.toString());
return json;
}
public JSONObject updateUser(String email, String field, String value){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", update_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("field", field));
params.add(new BasicNameValuePair("value", value));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
Log.i("JSON", json.toString());
return json;
}
public boolean isConnectedToServer(String url, int timeout) {
try{
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.connect();
return true;
} catch (Exception e) {
// Handle your exceptions
return false;
}
}
/**
* function make Login Request
* @param name
* @param email
* @param password
* */
public JSONObject registerUser(String name, String email, String mode, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("mode", mode));
params.add(new BasicNameValuePair("password", password));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}
public JSONObject PlayMusic(String name){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "play"));
params.add(new BasicNameValuePair("name", name));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(mplayerURL, params);
// return json
return json;
}
public JSONObject ChangeVol(String volume){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "volume"));
params.add(new BasicNameValuePair("volume", volume));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(mplayerURL, params);
// return json
return json;
}
public JSONObject StopMusic(){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "stop"));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(mplayerURL, params);
// return json
return json;
}
public JSONObject ParcourirMusic(){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "parcourir"));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(mplayerURL, params);
// return json
return json;
}
/**
* Function get Login status
* */
public boolean isUserLoggedIn(Context context){
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}
/**
* Function to logout user
* Reset Database
* */
public boolean logoutUser(Context context){
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return true;
}
public void ReadMail(){
try{
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "rami.mtiri@gmail.com", "pass");
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
SearchTerm term = new SearchTerm() {
@Override
public boolean match(Message mess) {
try {
return mess.getContent().toString().toLowerCase().indexOf("boston") != -1;
} catch (IOException ex) {
// Logger.getLogger(JavaMailTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (MessagingException ex) {
// Logger.getLogger(JavaMailTest.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
};
Message[] searchResults = inbox.search(term);
for(Message m:searchResults){
Log.e("mail",m.getFrom()[0].toString() );
System.out.println("MATCHED: " + m.getFrom()[0]);
}
} catch (Exception e) {
}
}
}
请任何人都可以使用我的代码UP给我一个完整的解决方案,我是Java新手,不在乎函数或变量是否是静态的,我想要的只是按条件初始化IP地址。我在每个其他类中调用这个类的函数,所以在每个类中进行测试是不礼貌的。我把它作为最后的手段