如何在String []类型中返回{" a"," b"," c" ......}的字符串

时间:2015-07-31 17:24:41

标签: java

我有一个方法(如下)。我想要它返回什么' str.trim()'产生。 ' str.trim()'产生低于JSON。问题是我该如何归还?目前,我正在返回方法本身,getData()这不好。此外,返回类型需要为String[],因为我需要将此返回值传递给Object。

str.trim()生成此内容:{ "a", "b", "c" }

----------------------方法------------------------

      private String[] getData() {

    try {
        URL url = new URL("https://localhost:8443/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

        String output;
        StringBuilder sb = new StringBuilder();
        //System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            //System.out.println(output);
            sb.append(output + "");


        }
        String content = sb.toString();
        String str = content.replace("[", "{").replace("]", "}");
        //System.out.println(content.replace("[", "{").replace("]", "}"));
        System.out.println(str.trim());



        conn.disconnect();


      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

      }
    return getData();

1 个答案:

答案 0 :(得分:0)

我懂了!!解决。

我需要在顶部分配一个新变量。 将它分配给str.split();

现在有效!!