1

I'm using StringBuffer to send and receive variables from a web service.

My code is:

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

How can I change this code to be able to receive Array instead of String?

The request which I get from the web service is like this: {"var", "var"}.

Jesper
  • 202,709
  • 46
  • 318
  • 350
Ilkar
  • 2,113
  • 9
  • 45
  • 71
  • 1
    You may want to have a look [here][1]. Let me know if it helps. [1]: http://stackoverflow.com/questions/285712/java-reading-a-file-into-an-array – MacLuq Jan 19 '12 at 09:56
  • 1
    From the Javadoc for StringBuffer `The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.` – Peter Lawrey Jan 19 '12 at 10:00

1 Answers1

0

This might help http://www.coderanch.com/t/393008/java/java/explode-Java

Remember to cut the response from {} using response.substring()!

look at:

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

you will have your responses in "" so substring them then. Good luck!

noisy cat
  • 2,865
  • 5
  • 33
  • 51