2013-07-03 4 views
2

Я хочу вернуть ответ здесь, используя панель прогресса, чтобы я мог показать диалог загрузки. Код работает нормально, но иногда я не могу получить ответ отсюда.Я хочу вернуть ответ здесь в 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. Я отредактировал мой вопрос. –