2012-01-19 60 views
1

我使用StringBuffer發送和接收來自Web服務的變量。return StringBuffer array

我的代碼是:

// Create connection 
      url = new URL(urlSCS + "/login"); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestMethod("POST"); 
  connection.setRequestProperty("Content-Type", 
        "application/x-www-form-urlencoded"); 

      connection.setRequestProperty("Content-Length", 
        "" + Integer.toString(urlParameters.getBytes().length)); 
      connection.setRequestProperty("Content-Language", "pl-PL"); 

      connection.setUseCaches(false); 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 

      // Send request 
      DataOutputStream wr = new DataOutputStream(
        connection.getOutputStream()); 
      wr.writeBytes(urlParameters); 
      wr.flush(); 
      wr.close(); 

      // Get Response 
      InputStream is = connection.getInputStream(); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
      String line; 
      StringBuffer response = new StringBuffer(); 
      while ((line = rd.readLine()) != null) { 
       response.append(line); 
       response.append('\r'); 
      } 
      rd.close(); 

我怎樣才能改變這個代碼能夠接收數組,而不是字符串?

我從Web服務獲得的請求是這樣的:{"var", "var"}

+0

什麼數組? '炭[]'? – adarshr

+1

你可能想看看[這裏] [1]。讓我知道它是否有幫助。 [1]:http://stackoverflow.com/questions/285712/java-reading-a-file-into-an-array – MacLuq

+1

從Javadoc中的StringBuffer'的 StringBuilder類通常應當使用而不是 這一個,因爲它支持所有相同的操作,但速度更快,因爲 它不執行同步。 –

回答

0

這可能有助於 http://www.coderanch.com/t/393008/java/java/explode-Java

記住削減從{}使用response.substring響應()!

看看:

String partsColl = "A,B,C"; 
    String[] partsCollArr; 
    String delimiter = ","; 
    partsCollArr = partsColl.split(delimiter); 

你將不得不在 「」 這樣子串他們,那麼你的反應。 祝你好運!