I have two java classes Configuration.java and login.java
Configuration.java
public class Configuration {
public Configuration() {}
public String getparams() throws IOException {
Properties properties = new Properties();
FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
try {
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return ip + port;
} finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
login.java
...
Configuration cfg=new Configuration();
String ip=cfg.getparams();// error
String port=cfg.getparams();//error
private final String LOGIN_URL = "http://"+ ip +":"+port+"/webservice/login.php";
can anyone help me please to resolve the error
public class Login extends Activity implements OnClickListener {
Configuration cfg;
String ip;
String port;
private String LOGIN_URL;
//other variables
protected void onCreate(Bundle savedInstanceState) {
//here you initialize your variables
try {
cfg = new Configuration();
String[] params = cfg.getparams();
ip = params[0];
port = params[1];
LOGIN_URL = "http://"+ ip +":"+port+"/webservice/login.php";
} catch (IOException e) {
//handle the exception
}
//.. your other code ...
}
//... your other methods ...
}
Also change your Configuration file like this:
public class Configuration {
public Configuration() {}
public String[] getparams() throws IOException {
Properties properties = new Properties();
FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
try {
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return new String[]{ip, port};
} finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Why don't you change the signature of getParam to not throw an error. I see you are doing it partially for call to properties.load();
Something like:
public class Configuration {
public Configuration() {}
public String getparams() {
Properties properties = new Properties();
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return ip + port;
}
catch(IOException ex)
{
return null;
}
finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
You might want to move new Properties into the try block if it can also throw.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.