在android中从服务器下载值时显示百分比进度条

时间:2017-06-29 10:46:35

标签: android soap

从类中下载文件时遇到问题。我使用webservice作为SOAP并将值获取到单独的类(WebServiceCall.class)。所以请任何人帮我解决这个问题。如何在下载文件时使用百分比的进度条?

以下是我正在进行下载部分的部分:

    public class ClientDownload extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pg = new ProgressDialog(SettingsActivityNav.this);
            pg.setMessage("Downloading...");
            pg.setIndeterminate(false);
            pg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pg.setMax(100);
            pg.setProgress(0);
   //         pg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pg.setCanceledOnTouchOutside(false);
            pg.setCancelable(false);
            pg.show();


            boolean num = rkrhelp.Ifvisitstatusnotempty();
            if (num) {
                rkrhelp.delete_visitStatus();
            }
            db1.execSQL("delete from " + "ADD_CLIENT_BASIC_INFO_TABLE where insert_type=2");
//            cmd

/*
            db1.execSQL("delete from " + "ADD_CLIENT_ACCOUNT_AND_PURCHASE_DETAILS_TABLE where insert_type=2");
*/
        }

        @Override
        protected String doInBackground(String... params) {

            final String v_id = getSharedPreferences("com.mobiz.pavizham", MODE_PRIVATE).getString("uid", "0");
            String dat = getSharedPreferences("com.mobiz.pavizham", MODE_PRIVATE).getString("dat", "2014-01-12");
            status=0;

            new Thread(new Runnable() {
                @Override
                public void run() {
                    while(status < 100){
                        **WebserviceCall com = new WebserviceCall(getApplicationContext());
                        com.getserver("getservertime");
                        com.getroute("Get_Route", ac_id, v_id);
                        com.getclient("getClientDetails_Roofing", v_id);** //This is the values I have to download
                        status+=1;

                        try{
                            Thread.sleep(2000);

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

                        new Handler().post(new Runnable() {
                            @Override
                            public void run() {
                                pg.setProgress(status);
                                if(status == 100){
                                    pg.dismiss();
                                }

                            }
                        });

                    }
                }
            }).start();


            return null;
        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            if (!SettingsActivityNav.this.isFinishing() && pg != null && pg.isShowing()) {
                pg.dismiss();
            }
            Toast.makeText(getApplicationContext(), "Download Complete",
                    Toast.LENGTH_SHORT).show();

        }
    }

以下是我在WebServiceCall中使用的一种方法:

    public void getserver(String MethodName) {

        try {
            SOAP_ACTION = namespace + MethodName;
            Map<String, String> input = new LinkedHashMap<>();           
            request = new SoapObject(namespace, MethodName);

             Iterator<String> itr = input.keySet().iterator();
            while (itr.hasNext()) {
                String name = itr.next();
                PropertyInfo propInfo = new PropertyInfo();
                propInfo.setName(name);
                propInfo.setValue(input.get(name));
                propInfo.setType(android.R.string.class);
                request.addProperty(propInfo);
            }

            Log.e("count", Integer.toString(9));
            Log.e("Sending Request", request.toString());

            SetEnvelope();


            //SOAP calling webservice
            androidHttpTransport.call(SOAP_ACTION, envelope);
            Log.e("count", Integer.toString(10));
            //Got Webservice response
            String result = envelope.getResponse().toString();
            Log.e("String result", result);
            //Log.e("count",Integer.toString(10));
//            SoapObject soapobject = (SoapObject) envelope.getResponse();

            //Log.e("Soap Object Response",soapobject.toString());
            // SoapObject soapobject = (SoapObject)envelope.getResponse();
//            DatabaseHelperAddClient db = new DatabaseHelperAddClient(context);
            // Log.e("Soap Object Response",soapobject.toString());
            RKRPavizhamSQLiteHelper DBHelper = new RKRPavizhamSQLiteHelper(context);
//        RKRPavizhamSQLiteHelper DBHelper = new DatabaseHelper(context, "OdyssiaDB", null);
            SQLiteDatabase db = DBHelper.getWritableDatabase();
            db.delete("SERVER_DATE", null, null);

//            int clientResultItemCount = soapobject.getPropertyCount();
//            for (int i = 0; i < clientResultItemCount; i++) {
//                SoapObject propertyItr = (SoapObject) soapobject.getProperty(0);
            ContentValues values = new ContentValues();
            values.put("server_time", result.toString());

            db.insert("SERVER_DATE", null, values);
//            }
        } catch (Exception e) {
            Log.e("EX", e.toString());
        }
    }

0 个答案:

没有答案