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包含除了类型转换之外的相同代码行。如果我再次将这行代码移动到一个常用方法,我想输入强制转换并再次执行此过程。我怎样才能减少这种还原剂线,任何人都可以帮助我。
我在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);
因为在python变量里面一个if也可以从外部块访问。在java中我该怎么做。
答案 0 :(得分: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");
}
答案 1 :(得分:2)
为什么不:
HttpsURLConnection urlConnection = isHttps
? (HttpsURLConnection)newURL(url).openConnection()
: (HttpsURLConnection)new URL(url).openConnection();
urlConnection.setDoOutput(true);
// etc.
这比“在if
”方法之前声明变量的优势在于,即使变量为final
,这也会起作用。
答案 2 :(得分:2)
我认为isHttps
变量检查是有原因的(?),因此第二次演员应该是HttpURLConnection
,这意味着问题中有拼写错误?
如果是这样,问题中使用的大多数方法都可以在父类URLConnection
中使用而不需要强制转换,但不是全部。
幸运的是,HttpsURLConnection是HttpUrlConnection的一个子类,所以只需总是强制转换为可以在这里工作,替换为all(不需要进行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");
答案 3 :(得分: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");
答案 4 :(得分:0)
在if语句之前声明变量,并为其赋值“null”(以避免警告变量可能未初始化)。然后按照伪代码示例中的样式进行操作。
答案 5 :(得分: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