2013-04-24 1 views
0

의 유형을 선언. 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 += "&"; 
      } 
     } 
    } 
} 

답변

3

이 범위 문제입니다 : 참고로

, 여기에 지금까지 내 클래스입니다.

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에 액세스를 시도했다. Ergo,이 변수에 대한 액세스 권한이없는 것은 없습니다.

+0

고맙습니다! 유형을 캐스팅 할 수 있는지 전혀 몰랐습니다. – Aaron

0

변수는 매우 짧은 블록 내에서 선언되며 변수를 호출 할 때까지 범위를 벗어납니다.

블록 앞에 하나의 변수 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"); 
관련 문제