返回StringBuffer数组

时间:2012-01-19 09:51:57

标签: java

我正在使用StringBuffer从Web服务发送和接收变量。

我的代码是:

// 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();

如何更改此代码以便能够接收Array而不是String?

我从网络服务获得的请求是这样的:{"var", "var"}

1 个答案:

答案 0 :(得分: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);

你会在“”中得到你的回答,然后将它们分开。 祝你好运!