GWT RPC 数组访问
GWT RPC array access
我正在编写一个应用程序,其中将文本文件中的数据保存到数组中,并在稍后将其作为数字类型的数组传输到小部件 GWT Highcharts。我写了一个 servlet,将文件中的数据写入数组,但我被困在这里。我不知道如何将数组的内容传递给应用程序的客户端部分。有没有快速简便的方法来做到这一点?
这段代码是我写的:
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);
}
您必须在客户端使用异步接口:
private DataPointsAsync dataPointsService = GWT.create(DataPoints.class);
您可以这样使用服务:
dataPointsService.getDataPoints(AsyncCallback<Number[]>(){
@Override
public void onSuccess(Number[] result) {
// result contains the returning values
}
@Override
public void onFailure(Throwable caught) {
Window.alert("panic");
}
});
我正在编写一个应用程序,其中将文本文件中的数据保存到数组中,并在稍后将其作为数字类型的数组传输到小部件 GWT Highcharts。我写了一个 servlet,将文件中的数据写入数组,但我被困在这里。我不知道如何将数组的内容传递给应用程序的客户端部分。有没有快速简便的方法来做到这一点?
这段代码是我写的:
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);
}
您必须在客户端使用异步接口:
private DataPointsAsync dataPointsService = GWT.create(DataPoints.class);
您可以这样使用服务:
dataPointsService.getDataPoints(AsyncCallback<Number[]>(){
@Override
public void onSuccess(Number[] result) {
// result contains the returning values
}
@Override
public void onFailure(Throwable caught) {
Window.alert("panic");
}
});