我正在开发一个Android
应用程序,其中包含一个名为userFunctions
的类
它的所有功能都使用一个名为getIP()
的函数来定义要使用的IP地址:
public class UserFunctions {
private JSONParser jsonParser;
private static String loginURL = "/android_login_api/";
private static String mplayerURL = "/android_mplayer_api/";
private static String registerURL = "/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();
}
public String getIP(){
if(isConnectedToServer("192.168.1.2", 500)==false)
{
if(isConnectedToServer("www.google.com", 500)==true)
{
ReadEmails re = new ReadEmails();
return re.processMail();
}
}
return "192.168.1.2";
}
/**
* 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("http://"+this.getIP()+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("http://"+this.getIP()+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("http://"+this.getIP()+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("http://"+this.getIP()+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("http://"+this.getIP()+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("http://"+this.getIP()+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("http://"+this.getIP()+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("http://"+this.getIP()+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", "Rambo_007");
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) {
}
}
}
我几乎在每个活动中使用此类的功能,所以每次使用其中一个功能时都会运行测试 我想知道是否有一种方法可以在应用程序开始时一劳永逸地初始化IP地址。
答案 0 :(得分:0)
答案 1 :(得分:0)
声明一个最终的String。在构造函数中设置它。