我想使用异步调用将 ArrayList 从我的服务器发送到我的客户端。调用成功,但 ArrayList 条目在此过程中重复。我完全迷路了。代码非常简单。我无法弄清楚这个错误发生的时间和地点。
共享类:
package com.posteurs.cine.shared;
import java.io.Serializable;
import java.util.ArrayList;
public class Codes extends ArrayList<Integer> implements Serializable{
private static final long serialVersionUID = 1L;
//insert 2 values
public Codes(){
this.add(10);
this.add(20);
}
}
接口:
package com.posteurs.cine.client;
import java.util.Set;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.posteurs.cine.shared.Codes;
@RemoteServiceRelativePath("cineSvc")
public interface CineSvc extends RemoteService {
public Codes getCodes();
}
package com.posteurs.cine.client;
import java.util.Set;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.posteurs.cine.shared.Codes;
public interface CineSvcAsync {
public void getCodes(AsyncCallback<Codes> callback);
}
实施:
package com.posteurs.cine.server;
import java.util.Iterator;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.posteurs.cine.client.CineSvc;
import com.posteurs.cine.shared.Codes;
public class CineSvcImpl extends RemoteServiceServlet implements CineSvc {
private static final long serialVersionUID = 1L;
public CineSvcImpl(){
}
public Codes getCodes(){
Codes c = new Codes();
System.out.println("On server -----------------");
System.out.println("impl.getCodes: Codes size="+c.size());
return c;
}
}
来电:
package com.posteurs.cine.client;
import java.util.Iterator;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.posteurs.cine.client.CineSvcAsync;
import com.posteurs.cine.shared.Codes;
public class Cine implements EntryPoint {
@Override
public void onModuleLoad() {
CineSvcAsync svc = GWT.create(CineSvc.class);
svc.getCodes( new AsyncCallback<Codes>() {
public void onFailure(Throwable caught) {
System.out.println("getCodes::callback: Async failed");
}
public void onSuccess(Codes result) {
System.out.println("On client -----------------");
System.out.println("Callback result size="+result.size());
Iterator<Integer> it = result.iterator();
while (it.hasNext())
System.out.println(it.next());
}
});
}
追踪结果:
On server -----------------
impl.getCodes: Codes size=2
On client -----------------
Callback result size=4
10
20
10
20