带有WebService / InsertDB的ProgressDialog并在AsyncTask中下载文件

时间:2012-03-12 12:44:04

标签: android web-services android-asynctask progressdialog

我尝试在调用Web服务期间添加ProgressDialog,将数据插入数据库并下载文件

我有类名Ws_Document,Ws_Participant,Ws_Rubrique,它们调用webservice,使用方法将数据插入数据库,以及导入文件的方法。

我将这些类称为AsyncTask中的主要活动。

我试图在类似的问题上找到答案,但不是..

这里是Ws_Document,但Ws_Participant和Ws_Rubrique的内容相同:

public void getWs(Context ctx) {
    ParticipantDbAdapter oPart_DbLog = new ParticipantDbAdapter(ctx);
    oPart_DbLog.open();
    oPart_DbLog.getAllParticipants();
    token = oPart_DbLog.getToken();
    oPart_DbLog.close();

    try {
        SoapObject request = new SoapObject(NAMESPACE, NAME_DOCUMENT);
        request.addProperty("sToken", token);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION, envelope);
        Log.d("test", androidHttpTransport.requestDump);
        Log.d("test", androidHttpTransport.responseDump);

        SoapObject result = (SoapObject) envelope.bodyIn;
        String resultData = result.getProperty(0).toString();
        System.out.println(resultData);

        JSONArray oJSonArray = new JSONArray(resultData);

        int total = oJSonArray.length();
        for (int i = 0; i < total; i++) {
            JSONObject row = oJSonArray.getJSONObject(i);
            Document oDoc = new Document();
            oDoc.setId(Integer.parseInt(row.getString("Id")));
            oDoc.setNom(row.getString("Nom"));
            sVignette = row.getString("Vignette");
            oDoc.setVisuel(sVignette);
            urlVignette = row.getString("sUrlVignette");
            oDoc.setRub_id(Integer.parseInt(row.getString("Rubrique")));
            oDoc.setOrdre(row.getInt("Ordre"));
            oDoc.setType(row.getString("Type"));
            sFichier = row.getString("Fichier");
            oDoc.setFichier(sFichier);
            urlFichier = row.getString("sUrlFichier");
            oDoc.setLan_code(row.getString("Langue"));
            oDoc.setPoids(Integer.parseInt(row.getString("Poids")));
            oDoc.setMotcle(row.getString("MotCle"));
            oDoc.setMea(row.getString("sMea"));
            //insertDoc(ctx, oDoc);

            if (sVignette != null)
                importDoc(sVignette, urlVignette);

            if (sFichier != null)
                importDoc(sFichier, urlFichier);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void insertDoc(Context ctx, Document document) {
    DocumentDbAdapter oDoc_DbAdapter = new DocumentDbAdapter(ctx);
    oDoc_DbAdapter.open();
    oDoc_DbAdapter.insertDoc(document);
    oDoc_DbAdapter.close();
}

private void importDoc(String file, String url) {
    try {
        if(file == sFichier)
            url = url +"&token=" + token;

        java.net.URL dlFrUrl = new java.net.URL(url);           
        long startTime = System.currentTimeMillis();
        Log.d("download begining", url);
        Log.d("downloaded file", file);

        HttpURLConnection urlConnextion = (HttpURLConnection) dlFrUrl.openConnection();
        urlConnextion.setRequestMethod("GET");
        urlConnextion.setDoOutput(true);
        urlConnextion.connect();

        File sdSaveFile = new File(Environment.getExternalStorageDirectory() + "/Doc");
        if (!sdSaveFile.exists()) {
            sdSaveFile.mkdir();
        }
        File oFile = new File(sdSaveFile, file);
        InputStream inputStream = urlConnextion.getInputStream();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        FileOutputStream fileOutput = new FileOutputStream(oFile);          

        byte[] buffer = new byte[1024];
        int bufferLength = 0;

        while (bufferLength != -1) {
            fileOutput.write(buffer, 0 , bufferLength); 
            bufferLength = bufferedInputStream.read(buffer, 0, 1024);
        }       

        fileOutput.close();
        Log.d("download ready in", ((System.currentTimeMillis() - startTime) / 1000)+ " sec");

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

活动:

private class set_Login extends AsyncTask<Void, Integer, SoapObject> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected SoapObject doInBackground(Void... arg0) {
        // TODO Auto-generated method stub

        Ws_Participant oGetWsPart = new Ws_Participant();
        Ws_Rubrique oGetWsRub = new Ws_Rubrique();
        Ws_Document oGetWsDoc = new Ws_Document();

        oGetWsPart.getWs(idOper, idDev, login, mot_de_passe, getApplicationContext());  
        oGetWsRub.getWs(getApplicationContext());
        oGetWsDoc.getWs(getApplicationContext());           
        return null;
    }

    @Override
    protected void onPostExecute(SoapObject result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);    
        startActivity(new Intent(PlugooplayActivity.this, MainMenuActivity.class));
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }
}

1 个答案:

答案 0 :(得分:0)

如果要更新ProgressDialog,有五个部分:

  1. 在AsyncTaskClass中声明一个进度对话框:

    private class set_Login extends AsyncTask<Void, Integer, SoapObject> {
        ProgressDialog pd;
    
  2. 在onPreExceute中启动它:

    @Override
    protected void onPreExecute() {
        pd = new ProgressDialog(theContext);
        pd.setTitle(sometitle);
        pd.setMessage(someMessage);
        pd.show();
        super.onPreExecute();
    }    
    
  3. 在步骤之间发布进度:

     @Override
    protected SoapObject doInBackground(Void... arg0) {
        // snip
    
        oGetWsPart.getWs(idOper, idDev, login, mot_de_passe, getApplicationContext()); 
        publishProgress(1); 
        oGetWsRub.getWs(getApplicationContext());
        publishProgress(2);
        oGetWsDoc.getWs(getApplicationContext());           
        publishProgress(3);
        // snap
        return null;
    }
    
  4. 显示进度:

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        pd.setProgress(values[0]);
    }
    
  5. 完成后解雇:

    @Override
    protected void onPostExecute(SoapObject result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);    
        pd.dismiss();
        startActivity(new Intent(PlugooplayActivity.this, MainMenuActivity.class));
    }