我已經做了REST Web服務,讓我接觸的一些信息,如號碼,年齡......我得到所有這些信息,在此功能CodenameOne連接請求等待postResponse
public static void getRest(String search) {
if(search.equals("")){
json="http://localhost:8080/com.vogella.jersey.first/rest/jsonServices/print/";
} else {
json="http://localhost:8080/com.vogella.jersey.first/rest/jsonServices/print/"+search;
}
ConnectionRequest req = new ConnectionRequest() {
@Override
protected void postResponse() {
}
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser p = new JSONParser();
Map<String, Object> h = p.parseJSON(new InputStreamReader(input));
ArrayList object=new ArrayList();
for (Entry<String, Object> entry : h.entrySet()) {
object = (ArrayList) entry.getValue();
int i=object.size();
}
for(int i=0; i<object.size();i++){
LinkedHashMap s= (LinkedHashMap) object.get(i);
Risultati.add(s);
}
}
};
req.setUrl(json);
req.setPost(false);
req.addRequestHeader("Accept", "application/json");
InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
req.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueue(req);
Risultati是的屬性類:ArrayList<LinkedHashMap> Risultati;
的問題是,當我調用函數getRest("")
這樣:
getRest("");
Label contatto=null;
for(int j=0;j<Risultati.size();j++){
LinkedHashMap s=Risultati.get(j);
String nome=(String) s.get("firstName");
String cognome=(String) s.get("lastName");
String numero =(String) s.get("numero");
contatto=new Label(nome+" "+cognome+" "+numero);
}
hi.addComponent(contatto);
事實是Risultati是空,如果我評論的週期我注意到內部函數readResponse執行後......我不知道我在做什麼錯
很好的答案。然而,一個挑剔的是,最後一段是不準確的。 addToQueueAndWait會在不阻塞UI線程的情況下「正確」阻止UI線程(是的,這就是禪),請參閱:http://www.codenameone.com/blog/callserially-the-edt-invokeandblock-part-1.html和http:/ /你確實需要展現無限進步或其他東西...... –