在条件语句中声明变量及其类型



我正试图在Java:中做到这一点

if(this.ssl == true) {
    HttpsURLConnection connection = (HttpsURLConnection) new URL(address).openConnection();
}
else {
    HttpURLConnection connection = (HttpURLConnection) new URL(address).openConnection();
}
connection.setDoOutput(true);
connection.setRequestMethod("POST");

但最后两行抛出了一个错误,表示找不到变量。这在Java中是不可能的吗?我知道在这种情况下用相同的类型声明变量(在条件外声明,在条件内初始化(,但在这种情况中,基于条件的类型不同。

作为参考,这是我迄今为止的课程:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class Post {
    private String data;
    private boolean ssl;
    public Post(boolean ssl) {
        this.ssl = ssl;
    }
    public String sendRequest(String address) throws IOException {
        //Only send the request if there is data to be sent!
        if (!data.isEmpty()) {
            if (this.ssl == true) {
                HttpsURLConnection connection = (HttpsURLConnection) new URL(address).openConnection();
            } else {
                HttpURLConnection connection = (HttpURLConnection) new URL(address).openConnection();
            }
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(data);
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            reader.close();
            writer.close();
            return response.toString();
        } else {
            return null;
        }
    }
    public void setData(String[] keys, String[] values) throws UnsupportedEncodingException {
        //Take in the values and put them in the right format for a post request
        for (int i = 0; i < values.length; i++) {
            this.data += URLEncoder.encode(keys[i], "UTF-8") + "=" + URLEncoder.encode(values[i], "UTF-8");
            if (i + 1 < values.length) {
                this.data += "&";
            }
        }
    }
}

这是一个范围问题。

将其更改为

public String sendRequest(String address) throws IOException {
   HttpURLConnection connection = null;
   //Only send the request if there is data to be sent!
   if (!data.isEmpty()) {
       if (this.ssl) {
         connection = (HttpsURLConnection) new URL(address).openConnection();
       } else {
         connection = (HttpURLConnection) new URL(address).openConnection();
       }
   }
   connection.setDoOutput(true);
   connection.setRequestMethod("POST");
   ...

if语句中定义connection时,您正试图访问它。埃尔戈,没有其他人可以访问这个变量。

请参阅此内容。在java中,如何基于url创建HttpsURLConnection或HttpURLConnection?

基本上,由于HttpsURLConnection扩展了HttpUrlConnection,所以不需要在if语句中声明。

您的connection变量是在很短的块中声明的,当您尝试调用它们上的方法时,它们已经超出了范围。

在块之前声明一个connection变量,并在每种情况下对其进行设置,以便在调用方法时该变量仍在作用域中。

HttpURLConnection connection;
if(this.ssl == true) {
    connection = (HttpsURLConnection) new URL(address).openConnection();
}
else {
    connection = (HttpURLConnection) new URL(address).openConnection();
}
connection.setDoOutput(true);
connection.setRequestMethod("POST");

相关内容

  • 没有找到相关文章

最新更新