2013-07-03 1 views
2

에서 반환하고 싶습니다. Loading 대화 상자를 표시 할 수 있도록 진행 창을 사용하여 여기에서 응답을 반환하고 싶습니다. 코드가 잘 작동하지만 때로는 여기서 응답을 얻을 수 없습니다.여기서 응답을 j2me

코드 조각은 다음과 같습니다

public class Connection 
{ 

    StringBuffer messageBuffer = new StringBuffer(); 
    String response; 

    public String connect(String str, String type) 
    { 
    try 
    { 
     ConnectionRequest cr = new ConnectionRequest() 
     { 
      protected void readResponse(InputStream input) 
      { 
       try 
       { 
        // just read from the response input stream 
        System.out.println("input length: "+input.available()); 
        //byte[] b = new byte[input.available()]; 
        //input.read(b, 0, input.available()); 
        byte[] b ; 
        ByteArrayOutputStream bStrm = new ByteArrayOutputStream(); 
        int ch; 
        while ((ch = input.read()) != -1) 
         bStrm.write(ch); 
        b = bStrm.toByteArray(); 
        System.err.println(b.length);       
        response = new String(b);       
        System.err.println(new String(b)); 
       } 
       catch (IOException ex) 
       {       
        System.err.println("IOException :"+ex.getMessage()); 
       } 
      } 
      protected void postResponse() 
      {      
      } 
     }; 
     cr.setTimeout(3600); 
     cr.setUrl(str.trim());  
     Progress progress = new Progress("Loading...", cr,true); 
     progress.setAutoShow(true); 
     progress.setDisposeOnCompletion(true); 

     if (type.equals("POST")) 
     { 
      cr.setPost(true); 
     } else 
     { 
      cr.setPost(false); 
     } 
     NetworkManager nm = NetworkManager.getInstance(); 
     nm.setTimeout(60000); 
     nm.start(); 
     nm.addToQueueAndWait(cr); 
     return response; 
    }   
    catch (Exception ae) 
    { 
     System.out.println(ae.getMessage()); 
     return response; 
    } 
    finally 
    { 
     return response; 
    } 

    } 

} 



public class Progress extends Dialog implements ActionListener 
{ 
    private ConnectionRequest request; 
    private boolean disposeOnCompletion; 
    private boolean autoShow; 

    public Progress(String title, ConnectionRequest request) { 
     this(title, request, false); 
    } 

    public Progress(String title, ConnectionRequest request, boolean showPercentage) { 
     super(title); 
     try { 
      this.request = request; 
      SliderBridge b = new SliderBridge(request); 
      setLayout(new BoxLayout(BoxLayout.Y_AXIS)); 
      addComponent(b); 
      setDisposeWhenPointerOutOfBounds(false); 
      setAutoDispose(false); 
      NetworkManager.getInstance().addProgressListener(this); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    protected void actionCommand(Command cmd) { 
     if(Display.getInstance().isTouchScreenDevice() || getSoftButtonCount() < 2) { 
      for(int iter = 0 ; iter < getComponentCount() ; iter++) { 
       Component c = getComponentAt(iter); 
       if(c instanceof Button) { 
        c.setEnabled(false); 
       } 
      } 
     } else { 
      removeAllCommands(); 
     } 
     request.kill(); 
    } 
    public boolean isDisposeOnCompletion() { 
     return disposeOnCompletion; 
    } 
    public void setDisposeOnCompletion(boolean disposeOnCompletion) { 
     this.disposeOnCompletion = disposeOnCompletion; 
    } 
    public void actionPerformed(ActionEvent evt) { 
     NetworkEvent ev = (NetworkEvent)evt; 
     if(ev.getConnectionRequest() == request) { 
      if(disposeOnCompletion && ev.getProgressType() == NetworkEvent.PROGRESS_TYPE_COMPLETED) { 
       dispose(); 
       return; 
      } 
      if(autoShow && Display.getInstance().getCurrent() != this) { 
       show(); 
      } 
     } 
    } 
    public boolean isAutoShow() { 
     return autoShow; 
    } 

    public void setAutoShow(boolean autoShow) { 
     this.autoShow = autoShow; 
    } 
} 

답변

2

난 당신이 코드의 어딘가에서 setProgress(value) 방법을 사용해야한다고 생각합니다. 그리고 Thread을 사용해야합니다.

은 스레드를 사용하여 진행 표시 줄에 값을 설정하는 방법을 설명합니다 여기 LWUIT Progress bar

그 블로그에서보세요.

+0

죄송합니다. @jmunoz 님이 도움을받지 못했습니다. 저는 여기서부터 응답을 보내고 싶지만 일어나는 일은 때로는 여기에서 돌아오고 때로는 응답을 얻은 후에도 돌아올 수 없다는 것입니다. 또한 Progress 수업을 들려 줄 수 있습니다. 내 질문을 편집했습니다. –