volly
volly

Reputation: 147

How to send data from Server to Client

i have file (data.txt) which contains 1000 numbers. I'd like to send this file from server to client.

Client

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

I don't know how send data .txt...

I create this function..

@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;                
}

and with function 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);

but it doesn't work... i am new in REST so it is possible that i make simply mistakes. Please help


I don't know how to send data using JSON.. Until now i create.

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

And

@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;
} 
}

My purpose is to send data to Client using JSON. Data are in file (data.txt), now i try to send simple array and when i run program i get -> "GET http:// localhost:8080/rest/data returned a response status of 404 Not Found" returned a response status of 404 Not Found ... I know how to send simple string, but with .txt i have trouble.. later i must receive those data and handle it as a numbers of int (because i must perform some mathematical operation on this data)

Upvotes: 0

Views: 355

Answers (1)

Elad Tabak
Elad Tabak

Reputation: 2417

First you need to decide how would you like the data to be sent. If you want to send a list of numbers, the JSON may look like this: { numbers: [ 1, 2, 3] }

To create such a JSON, use the JSONObject constructor and class methods like 'put'.


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

Then you can return "result".

Upvotes: 1

Related Questions