URL作为Java对象中的字段

时间:2013-04-24 20:33:57

标签: java url object constructor try-catch

我有一个对象,其中包含String类型的字段,其值类似于“http://stackoverflow.com

我有一个必须采用URL的方法。所以我在一个方法中包含String,然后从URL构建一个String(显然需要包装在try catch中......)

似乎应该有一种更简单的方法。是否可以将我的值作为URL存储在对象中,而不是String?我试过这个public URL url = "http://stackoverflow.com";

是否有可能沿着这些方向做点什么?有没有比我原来做的更好的方式?

由于

原:

public String urlString = "http://stackoverflow.com";


public boolean myMethod(String urlString) {
 try {
        URL newURL = new URL(urlString);
        useUrl(newURL);
 } catch (MalformedURLException e) {
                e.printStackTrace();
            }

}

不起作用,但似乎更好:

public URL url = "http://stackoverflow.com";


public boolean myMethod(URL url) {
        useUrl(url);
  }

7 个答案:

答案 0 :(得分:3)

Java不支持隐式转换。如果你想将一种类型转换为另一种类型,则必须明确地将其转换为

final URL url = new URL("http://stackoverflow.com/");

然而,这会引发一个检查异常,因此您需要一个辅助方法,如

private static final URL url = getUrl("http://stackoverflow.com/");

private static URL getUrl(String spec)  {
    try {
        return new URL(spec);
    } catch (MalformedURLException e) {
        throw new AssertionError(e);
    }
}

或使用构造函数

private final URL url;

public MyClass(String spec) throws MalformedURLException {
    url = new URL(spec);
}

答案 1 :(得分:0)

我不确定我是否明白你想做什么,但这有助于你吗?

class SomeClass {
  private final String urlString = "http://stackoverflow.com";
  public URL url;

  public SomeClass() {
     try {
        this.url = new URL(this.urlString); // suppose the URL is correct
     } catch (MalformedURLException e) {
        e.printStackTrace();
     }
  }
}

答案 2 :(得分:0)

您可以稍微缩短一下,但需要创建一个新的URL对象。

public boolean myMethod(String urlString) {
 try {

        useUrl(new URL(urlString));
 } catch (MalformedURLException e) {
                e.printStackTrace();
            }

}

答案 3 :(得分:0)

你可能想看一下Spring和Apache中的URIBuilder,以获得一些额外的魔力。

URIBuilder uriBuilder = URIBuilder.fromUri( "http://stackoverflow.com/" );
URI uri = uriBuilder.build();

答案 4 :(得分:0)

你做不到。根据定义,URL不会格式错误。因此,如果问题是要避免使用try / catch,那么您的类应该使用String而不是URL,并在需要时在内部转换为URL

您可以创建一个可以安全转换为URL的新对象:

private class UnsafeURL {
    private String url;

    public UnsafeURL(String url){
        this.url = url;
    }

    public URL toURL() throws MalformedURLException {
        return new URL(url);
    }
}

答案 5 :(得分:0)

public class Foo {  

  private URL test;
    {
        try {
            test = new URL("http://www.google.com");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }
   public Foo() {

   }
}

public class Foo {  

  private URL test;

   public Foo() {
        try {
            test = new URL("http://www.google.com");
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
   }
}

要么工作。最好的方法是在构造函数中执行它。

答案 6 :(得分:0)

异常处理是必要的,因为在尝试执行此操作之前,Java无法知道您的String可以转换为URL。

你真正想做的事情可能是:

static URL URLofString(String s){
 URL r=null;
 try { 
   r= new URL("http://stackoverflow.com");  
  } catch (MalformedURLException e) { } 
  return r;
 }

然后

final static URL myURL=URLofString("http://stackoverflow.com");
在Java 8中,您将能够使用lambda,尽管它几乎没有更好:

final static URL myURL = ()->{
  URL r=null; 
  try {  r= new URL("http://stackoverflow.com");  }
  catch (MalformedURLException e) { } return r;};