【问题标题】:How to send data from Server to Client如何将数据从服务器发送到客户端
【发布时间】:2013-11-09 18:43:35
【问题描述】:

我有包含 1000 个数字的文件 (data.txt)。我想将此文件从服务器发送到客户端。

客户

public class Test {
public static void main(String[] args) {    

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
 WebResource service = client.resource(getBaseURI());         

// Get JSON for application
    System.out.println(service.path("rest").accept(MediaType.APPLICATION_XML).get(String.class));

 }

 private static URI getBaseURI() {  
  return UriBuilder.fromUri("http://localhost8080/data").build();
  }

不知道怎么发送数据.txt...

我创建了这个函数..

@Path("/data")
public class Date
{               
@GET

@Produces(MediaType.TEXT_HTML)

int[] zmien(Scanner in)
{
    int i=0;
    int temp[] = new int [50];
    while ( in.hasNext() )
    {                       
        temp[i] = in.nextInt();
        i++;
    }
    in.close();
    return temp;                
}

和函数 main()

            Date test1 = new Data();        
    File file = new File ("data.txt");   
    Scanner in1 = new Scanner(file);
             int kaka[] = new int[10];
       kaka = test1.zmien(in1);

但它不起作用...我是 REST 的新手,所以我可能会犯简单的错误。 请帮忙


我不知道如何使用 JSON 发送数据.. 直到现在我都在创作。

public class Test{    
public static void main(String[] args){     

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());         

 // Get JSON for application
   System.out.println(service.path("rest").path("data").accept(MediaType.APPLICATION_JSON).get(String.class));
}

private static URI getBaseURI() 
{  
 return UriBuilder.fromUri("http://localhost:8080").build();
}

@Path("/data")
public class Rest { 

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)

public JSONObject sayData(JSONObject inputJsonObj) throws Exception {

  JSONArray numbers = new JSONArray();
    numbers.put(2);
    numbers.put(2);
    numbers.put(3);

    JSONObject result = new JSONObject();
    result.put("numbers", numbers);

return result;

return outputJsonObj;
} 
}

我的目的是使用 JSON 向客户端发送数据。数据在文件(data.txt)中,现在我尝试发送简单的数组,当我运行程序时我得到-> “GET http://localhost:8080/rest/data 返回了 404 Not Found 的响应状态” 返回了 404 Not Found 的响应状态......我知道如何发送简单的字符串,但是使用 .txt 我遇到了麻烦......稍后我必须接收这些数据并将其作为整数处理(因为我必须执行一些数学运算在这个数据上)

【问题讨论】:

标签: java json rest


【解决方案1】:

首先,您需要决定如何发送数据。 如果要发送数字列表,JSON 可能如下所示:{ numbers: [ 1, 2, 3] }

要创建这样的 JSON,请使用 JSONObject 构造函数和类方法,例如“put”。

JSONArray numbers = new JSONArray(); numbers.add(1); numbers.add(2); numbers.add(3); JSONObject result = new JSONObject(); result.put("numbers", numbers);

然后你可以返回“结果”。

【讨论】:

  • 当我将 numers.put(1) 替换为 number.add(1) 时出现错误 -> 对于 JSONArray 类型,方法 add(int) 未定义
  • 现在我将 JSON ({ numbers: [ 1, 2, 3] } ) 更改为 int?
  • @volly 你能更具体一点吗? org.codehaus.jettison.json.JSONArray 有 put(int) 方法,而 org.json.simple.JSONArray 有 add()。你的实现是什么?
猜你喜欢
  • 1970-01-01
  • 2013-04-11
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-29
  • 2020-08-17
相关资源
最近更新 更多