English 中文(简体)
安德列:在下载XML时显示教区的进展指标
原标题:Android: Show progress indicator of parse while downloading a XML

我需要展示一个进步指标栏,同时规定下载Xml。

我第一次下载,然后打平,但我不敢同时采取行动。

这是我的守则的一部分,但我不知道如何显示倒退指标。

   public class WS_Sincronizo  extends AsyncTask<Void,Integer,List<Alertas>>{


private Context conte;
// Variable para controlar ventana de progreso
private Sincronizacion actividad;
private Alertas user;
private long totalSize;
private WaitForCancelTask wft;
private HttpPost httpPost;
private HttpClient httpClient;
private HttpContext localContext;
private List<Alertas> resultado;
private BaseDatosHelper miBBDDHelper;
private SQLiteDatabase db;
private SincronizarXmlHelper manejadorXML;
private String fecha_bd;
private HttpEntity resEntity;

public static  double fileSize; 
private double downloaded; // number of bytes downloaded 


public WS_Sincronizo (Context conte,Object actividad, String fecha){
    this.conte=conte;
    this.actividad=(Sincronizacion) actividad;
    this.fecha_bd=fecha;
    fileSize = 0; 
    downloaded = 0; 

}


@Override
protected void onPreExecute() {
    super.onPreExecute();
    resultado=null;
    //TimeOut si exece el tiempo límite
    wft=new WaitForCancelTask(this,conte,Utiles.TimeOutWebServerSincro);
} 


@Override 
protected void onProgressUpdate(Integer... progress) { 
// TODO Auto-generated method stub 
    actividad.progreso((int) (progress[0]));

  if (progress[0].intValue()==100){
    /*** CANCELO TIMEOUT ***/
    wft.FinishWaitForCancelTask();
    }

} 


@Override
protected void onPostExecute(List<Alertas> rta) {
    super.onPostExecute(rta);
    // Envío mensaje vacio al manejador para indicar que ya terminó.

     actividad.FinSincronizacion(rta);



}


@Override
protected void onCancelled() {
    //Si había empezado una transacción la cierro
    try{
    db.releaseReference();
    }catch (Exception e) {
        // TODO: handle exception

    }


    httpPost=null;
    httpClient.getConnectionManager().shutdown();

    this.onPostExecute(null);

}

@Override
public List<Alertas> doInBackground(Void... params) {

      httpClient = new DefaultHttpClient();
      localContext = new BasicHttpContext();
      httpPost = new HttpPost(Utiles.UrlWebService + "GetSincro");


      try {  

        String version = conte.getPackageManager().getPackageInfo(conte.getPackageName(), 0).versionName; 

        multipartContent.addPart("fecha",new StringBody(fecha_bd));

        multipartContent.addPart("version",new StringBody(version));

        multipartContent.addPart("sistema",new StringBody(Utiles.SISTEMA));



    httpPost.setEntity(multipartContent);

    HttpResponse httpresponse = httpClient.execute(httpPost,localContext);

    if(httpresponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK){
        //Cancelo TimeOut porque resp es ok;


        resEntity = httpresponse.getEntity();


            if (resEntity != null) {

            fileSize = resEntity.getContentLength(); 

            //Log.d("SIZE:",MemoryStatus.formatSize((long)fileSize));
            Log.d("SIZE:",String.valueOf(fileSize));

            //Log.i("RESPONSE",EntityUtils.toString(resEntity));
                SAXParserFactory fabrica = SAXParserFactory.newInstance();
                SAXParser parser = fabrica.newSAXParser();
                XMLReader lector = parser.getXMLReader();






                try{
                    miBBDDHelper = new BaseDatosHelper(conte);
                    db=miBBDDHelper.SincronizarTablas();
                    manejadorXML = new SincronizarXmlHelper(db);
                    lector.setContentHandler(manejadorXML);
                    lector.parse(new InputSource(resEntity.getContent()));
                    // Obtengo el resultado del XML
                    resultado = manejadorXML.getListas();

                }catch (Exception e){

                }

                finally {
                    // TODO: handle exception
                    resEntity.consumeContent();
                    if (db.inTransaction()) {
                        db.endTransaction();
                    }
                    db.close();
                    miBBDDHelper.close();

                }

                if(resultado.isEmpty()) {
                    throw null;
                }
                else {
                    return resultado;
                }

                // Si dio error la RESPUESTA del SERWEB

            } else {

                throw null;
            }

            // Si no recibo la cabecera ok ,fuera
        } else {

            throw null;

        }           


    } catch (Exception e) {
        // TODO Auto-generated catch block
       // Log.d("Error",e.getMessage());
        httpClient.getConnectionManager().shutdown();
        return null;

    }



}
问题回答

Try:

public class SomeActivity extends Activity {

   private static final int PROGRESS_DIALOG_ID = 0;

   @Override
   protected Dialog onCreateDialog(int id) {
       if (id == PROGRESS_DIALOG_ID) {
           ProgressDialog dialog = new ProgressDialog(this);
           dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
           dialog.setMessage("Loading...");
           dialog.setCancelable(false);
           return dialog;
       }
       return super.onCreateDialog(id);
   }

   public void someMethod(){
       new SomeTask().execute();
   }

   class SomeTask extends AsyncTask<Void, Void, Void> {

      @Override
      protected void onPreExecute() {
           showDialog(PROGRESS_DIALOG_ID);
      }

      @Override
      protected Void doInBackground(Void... voids) {
        // download and parse
          return null;
      }
      @Override
      protected void onPostExecute(Void aVoid) {
          dismissDialog(PROGRESS_DIALOG_ID);
      }
  }
}




相关问题
How to upload and download file from a server using C#

I am developing a webpage in that the user can download their Resume for Edit. So I have a link to download the article. I use the following code for download. DataTable dt = user.getUserDetails(2); ...

Detecting PDF Download

I have recently added functionality for generating pdf reports to a web application. However the reports can be quite large and take some time to show the pdf download dialog. I want to show a spinner ...

Writing file to users giving sporadic error in IE

I have a very interesting issue with only specific IE implementations. I have an ASPX page that is used to write files down to the user, as part of the process the page uses the following code to ...

PHP - List of Excel files to download from Intranet Site?

I have a intranet site running PHP 5 that needs to list a folder containing only Excel files. Ideally the user needs to be able to input some search criteria (ie date) and the files would be filtered ...

Determine total size of SVN directory/trunk

Is there a way to count/calculate the total size of a svn directory if you were to checkout a revision? I have limited internet downloads so I need to know how big something is before I go and ...

Scala and html: download an image (*.jpg, etc) to Hard drive

I ve got a Scala program that downloads and parses html. I got the links to the image files form the html, Now I need to transfer those images to my hard drive. I m wondering what the best Scala ...

Downloading file with wxHTTP?

Im really stumped. Im using the wxHTTP class in wxWidgets to try and download two files. The first request succeeds but the second one fails when wxHTTP->GetInputStream is called. Between downloads, ...