-5
public static byte[] sendRequest(String url ,Object params ,boolean isHttps){
        if(isHttps){
            HttpsURLConnection urlConnection = (HttpsURLConnection)newURL(url).openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(60000);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
       }

        }else{
            HttpURLConnection urlConnection = (HttpURLConnection)new URL(url).openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setConnectTimeout(5000);
            urlConnection.setReadTimeout(60000);
            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        }


}

在这种情况下, if 和 else 都包含除了类型转换之外的相同代码行。如果我再次将这行代码移动到一个通用方法中,我想键入 cast 并再次执行此过程。我怎样才能减少这些多余的行,任何人都可以帮助我。

我在 python 中有一个解决方案。

     //pseudo code .
     if(isHttps) :
           urlConnection = https . 
     else : 
         urlConnection = http .
     urlConnection.setDoOutput(true);
     urlConnection.setDoInput(true);
     urlConnection.setRequestMethod("POST");
     urlConnection.setConnectTimeout(5000);
     urlConnection.setReadTimeout(60000);

因为在 if 内部的 python 变量中,也可以从外部 block 访问。在java中我该怎么做。

4

6 回答 6

3

简单的

public static byte[] sendRequest(String url ,Object params ,boolean isHttps){
     HttpsURLConnection urlConnection;
     if(isHttps){
         urlConnection = (HttpsURLConnection)newURL(url).openConnection();
      }else{
         urlConnection = (HttpURLConnection)new URL(url).openConnection();
      }
         urlConnection.setDoOutput(true);
         urlConnection.setDoInput(true);
         urlConnection.setRequestMethod("POST");
         urlConnection.setConnectTimeout(5000);
         urlConnection.setReadTimeout(60000);
         urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
}
于 2012-05-19T11:29:04.100 回答
2

为什么不:

HttpsURLConnection urlConnection = isHttps
                                 ? (HttpsURLConnection)newURL(url).openConnection()
                                 : (HttpsURLConnection)new URL(url).openConnection();

urlConnection.setDoOutput(true);
// etc.

与“在变量之前声明”方法相比,它的优点if是即使变量是final.

于 2012-05-19T11:27:31.657 回答
2

我假设isHttps变量检查是有原因的(?),因此第二个演员实际上应该是HttpURLConnection,这意味着问题中有错字吗?如果是这样,问题中使用的大多数方法都可以在URLConnection没有强制转换的父类中使用,但不是全部。

幸运的是 HttpsURLConnection 是 HttpUrlConnection 的一个子类,所以只要总是转换为它就可以在这里工作,全部替换为(不需要 isHttps 检查):

HttpURLConnection urlConnection = (HttpURLConnection)new URL(url).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(60000);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
于 2012-05-19T11:32:50.273 回答
1

将变量声明放在 if 块之前

HttpsURLConnection urlConnection;
if (isHttps) {
    urlConnection= (HttpsURLConnection) new URL(url).openConnection();
} else {
    urlConnection = (HttpsURLConnection) new URL(url).openConnection();
}
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(60000);
urlConnection.setRequestProperty("Content-Type",
        "application/x-www-form-urlencoded;charset=UTF-8");
于 2012-05-19T11:28:08.580 回答
0

在 if 语句之前声明变量,并为其分配“null”(以避免警告变量可能未初始化)。然后按照您的伪代码示例中的样式。

于 2012-05-19T11:27:29.733 回答
0

这两个块有什么区别?只需使用:

HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(60000);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

你类型转换没有什么特别的。是HttpsURLConnection一个子类HttpURLConnection

于 2012-05-19T11:34:39.493 回答