从Java服务器接收字符串到客户端

时间:2017-02-06 03:53:11

标签: java json rest stream inputstream

我有一个java客户端,它将JSON对象发送到服务器(REST服务)。 代码非常完美。我当前的服务器返回" RESPONSE",但我想修改我的代码以从服务器返回一个字符串到客户端(类似于"传输正常") - 除了从中发送对象客户端到服务器。 这是我的代码

服务器:

@Path("/w")
public class JSONRESTService {
@POST
@Path("/JSONService")
@Consumes(MediaType.APPLICATION_JSON)
public Response JSONREST(InputStream incomingData) {
    StringBuilder JSONBuilder = new StringBuilder();
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
        String line = null;
        while ((line = in.readLine()) != null) {
            JSONBuilder.append(line);
        }
    } catch (Exception e) {
        System.out.println("Error Parsing: - ");
    }
    System.out.println("Data Received: " + JSONBuilder.toString());

    // return HTTP response 200 in case of success
    return Response.status(200).entity(JSONBuilder.toString()).build();
}
}

客户端:

public class JSONRESTServiceClient {
public static void main(String[] args) {
    String string = "";
    try {

        JSONObject jsonObject = new JSONObject("string");

        // Step2: Now pass JSON File Data to REST Service
        try {
            URL url = new URL("http://localhost:8080/w/JSONService");
            URLConnection connection = url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
            out.write(jsonObject.toString());
            out.close();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            while (in.readLine() != null) {
            }
            System.out.println("\nJSON REST Service Invoked Successfully..");
            in.close();
        } catch (Exception e) {
            System.out.println("\nError while calling JSON REST Service");
            System.out.println(e);
        }

        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

因此,为了从服务器返回一个字符串到客户端 - 我首先修改了服务器中的方法以返回一个字符串, 我把它添加到我的客户端:

 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
 StringBuffer sb = new StringBuffer("");
 String line="";
 while (in.readLine() != null) {
                sb.append(line);
                break;
            }
  System.out.println("message from server: " + sb.toString());
  in.close();

但我的字符串是空的。 我究竟做错了什么?我应该如何修改我的服务器/客户端以接收简单的字符串甚至是对象? 感谢。

1 个答案:

答案 0 :(得分:1)

您的代码(示例):

@foreach($house as $value)
  <?php $check = 0; $ok;?>
  @foreach($house_img as $va)
    @if($va->house_id == $value->id)
    <?php
          $check = 1;
          $ok = $va->house_img;
    ?>
      @break
    @endif
  @endforeach

  @if($check == 1)
    <img src="{{asset('house_img/')}}/{{$ok}}" >
  @endif
@endforeach
相关问题