如何进行监控上传状态?

问题描述 投票:0回答:2

好吧,我无法解决这个问题,而且我真的需要获取上传状态,当你使用MultipartUploadRequest上传某些内容时,我想要使用toast或其他任何其他状态打印通知栏上显示的状态。我一直在关注这些教程,但我没有成功:https://github.com/gotev/android-upload-service/wiki/Monitoring-upload-status

我真的很感激有人告诉我为什么我不能做到这一点。这是我的代码:

 public class CargarDatos extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {

        ////////////////-------------
        String correo = campo_correo.getText().toString().trim();
        String nombre = campo_nombre.getText().toString().trim();
        String apellido= campo_apellido.getText().toString().trim();
        String telefono= campo_telefono.getText().toString().trim();
        String categoria= customspinner.getSelectedItem().toString().trim();
        String titulo = campo_titulo.getText().toString().trim();
        String descripcion = campo_descripcion.getText().toString().trim();
        String latitud = Double.toString(latitudeeeee);
        String longitud = Double.toString(longitudeeee);

        String s = (campo_publico.isChecked() ? "1" : "0");
        String r = (campo_terminos.isChecked() ? "1" : "0");

        //getting the actual path of the image
        String path = getPath(filePath);

        //Uploading code
        try {
            String uploadId = UUID.randomUUID().toString();

            //Creating a multi part request
            new MultipartUploadRequest(TerminosYC.this.getActivity(), uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload(path, "image") //Adding file
                    .addParameter("name",correo) //Adding text parameter to the request
                    .addParameter("nombre",nombre)
                    .addParameter("apellido",apellido)
                    .addParameter("telefono", telefono)
                    .addParameter("categoria", categoria)
                    .addParameter("titulo", titulo)
                    .addParameter("descripcion", descripcion)
                    .addParameter("publico", s)
                    .addParameter("terminos", r)
                    .addParameter("latitud",latitud)
                    .addParameter("longitud",longitud)
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); //Starting the upload

        } catch (Exception exc) {
            Toast.makeText(TerminosYC.this.getActivity(), exc.getMessage(), Toast.LENGTH_SHORT).show();

        }

        // params comes from the execute() call: params[0] is the url.
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }
android android-studio upload monitoring
2个回答
1
投票

如您提供的链接中所述,使用setDelegate方法获取上载状态。请求将如下所示

            new MultipartUploadRequest(TerminosYC.this.getActivity(), uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload(path, "image") //Adding file
                    .addParameter("name", correo) //Adding text parameter to the request
                    .addParameter("nombre", nombre)
                    .addParameter("apellido", apellido)
                    .addParameter("telefono", telefono)
                    .addParameter("categoria", categoria)
                    .addParameter("titulo", titulo)
                    .addParameter("descripcion", descripcion)
                    .addParameter("publico", s)
                    .addParameter("terminos", r)
                    .addParameter("latitud", latitud)
                    .addParameter("longitud", longitud)
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .setDelegate(new UploadStatusDelegate() {  //Add these lines to get upload status
                        @Override
                        public void onProgress(Context context, UploadInfo uploadInfo) {
                            // your code here
                        }

                        @Override
                        public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse,
                                            Exception exception) {
                            // your code here
                        }

                        @Override
                        public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
                            Toast.makeText(context, "Your success message here" + exception, Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancelled(Context context, UploadInfo uploadInfo) {
                            // your code here
                        }
                    }).startUpload();

文件上传后,将调用上述任何一种方法。在那边打印你的消息


0
投票

调试使用Stetho facebook的最佳方式

link

© www.soinside.com 2019 - 2024. All rights reserved.