Search code examples
javaarraysgwtgwt-rpcgwt-highcharts

GWT RPC array access


I'm writing an application in which data from a text file is saved to the array and late transferred to the widget GWT Highcharts as an array of Number type. I wrote a servlet that writes data from a file into an array, and I'm stuck here. I don't know how to pass the contents of the array to the client part of the application. Is there a quick and easy way to do this?

This code written by me:

DataPointsImpl.java:

package com.pwste.gwt.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.pwste.gwt.client.DataPoints;

public class DataPointsImpl extends RemoteServiceServlet implements DataPoints {
    private static final long serialVersionUID = 1L;

    @Override
    public Number[] getDataPoints() throws IOException {
    File dataFile = new File("points.txt");
    FileReader dataFileReader = new FileReader(dataFile);
    BufferedReader dataBufferedReader = new BufferedReader(dataFileReader);
    Number[] arrayNumber = new Number[10000];
    String dataString = dataBufferedReader.readLine();

    for (int i = 0; i < arrayNumber.length; i++) {
        arrayNumber[i] = Integer.parseInt(dataString);
        dataString = dataBufferedReader.readLine();
    }
    dataBufferedReader.close();

    return arrayNumber;
    }
}

DataPoints.java:

package com.pwste.gwt.client;

import java.io.IOException;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("dataPoints")
public interface DataPoints extends RemoteService {
    Number[] getDataPoints() throws IOException;
}

DataPointsAsync.java:

package com.pwste.gwt.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface DataPointsAsync {
    void getDataPoints(AsyncCallback<Number[]> callback);
}

Solution

  • You have to use the Async-Interface on the client side:

    private DataPointsAsync dataPointsService = GWT.create(DataPoints.class);
    

    you can use the service in this way:

    dataPointsService.getDataPoints(AsyncCallback<Number[]>(){
    
              @Override
              public void onSuccess(Number[] result) {
                // result contains the returning values
              }
    
              @Override
              public void onFailure(Throwable caught) {
                Window.alert("panic");
              }
    });