Cloud Firestore添加文档的问题-Android Studio

时间:2019-07-09 04:21:06

标签: java android google-cloud-firestore

请帮助我解决这个小问题

我有这个android代码:

private ArrayList serviceNameList = new ArrayList();

serviceNameList.add("windows");
serviceNameList.add("linux");
serviceNameList.add("mac");

然后,我使用Firestore保存数据:

Map<String, Object> service = new HashMap<>();
            service.put("full_name", full_name.getText().toString());
            service.put("Services_names", Collections.singletonList(serviceNameList));

            firestore.collection("service").document("new_service")
                    .set(service)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(getApplicationContext(), "Success..!",Toast.LENGTH_SHORT).show();
                            }
            })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                            public void onFailure(@NonNull Exception e) {
                                String error = e.getMessage();
                                Toast.makeText(getApplicationContext(), "Error: " + error,Toast.LENGTH_SHORT).show();
                        }
            });

当我评论或删除此行时,一切正常:

service.put("Services_names",Collections.singletonList(serviceNameList));

但是当我再次启用它时,我的应用程序就会失败...我想知道我该怎么做才能解决这个问题...

谢谢.. !!!!

顺便说一句,这是我的日志

2019-07-08 23:03:04.943 com.comas.app E: FATAL EXCEPTION: main
Process: com.comas.app, PID: 30989
java.lang.IllegalArgumentException: Invalid data. Nested arrays are not supported
    at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:firebase-firestore@@19.0.2:293)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:270)
    at com.google.firebase.firestore.UserDataConverter.parseList(com.google.firebase:firebase-firestore@@19.0.2:307)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:272)
    at com.google.firebase.firestore.UserDataConverter.parseMap(com.google.firebase:firebase-firestore@@19.0.2:294)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:250)
    at com.google.firebase.firestore.UserDataConverter.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@19.0.2:230)
    at com.google.firebase.firestore.UserDataConverter.parseSetData(com.google.firebase:firebase-firestore@@19.0.2:83)
    at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@19.0.2:175)
    at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@19.0.2:154)
    at com.comas.app.PopUpActivity$2.onClick(PopUpActivity.java:173)
    at android.view.View.performClick(View.java:6663)
    at android.view.View.performClickInternal(View.java:6635)
    at android.view.View.access$3100(View.java:794)
    at android.view.View$PerformClick.run(View.java:26199)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7593)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)2019-07-08 23:03:04.955 ? E: FaultDetect: DUMPTOOL_PRINTF return.

2 个答案:

答案 0 :(得分:1)

如果要将ArrayList数据存储到Firestore中,请首先迭代整个ArrayList。然后,仅将该变量传递到HashMap中。

private ArrayList serviceNameList = new ArrayList();

serviceNameList.add("windows");
serviceNameList.add("linux");
serviceNameList.add("mac");

Map<String, Object> service = new HashMap<>();
service.put("full_name", full_name.getText().toString());
service.put("Services_names", serviceNameList);

答案 1 :(得分:0)

这是将文件上传到Firestore的代码

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageReference = storage.getReference();

private void uploadImage() {
        if (filePath != null) { //path of the file you want to upload
            final ProgressDialog dialog = new ProgressDialog(this);
            dialog.setTitle("Uploading...");
            dialog.show();

            StorageReference ref = storageReference.child("images/" + UUID.randomUUID().toString());
            ref.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            dialog.dismiss();
                            System.out.println("image uploaded path " + taskSnapshot.getUploadSessionUri());
                            Toast.makeText(ListActivity.this, R.string.upload_success, Toast.LENGTH_LONG).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            dialog.dismiss();
                            Toast.makeText(ListActivity.this, R.string.upload_failure, Toast.LENGTH_LONG).show();
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
                                    .getTotalByteCount());
                            dialog.setMessage("Uploaded "+(int)progress+"%");
                        }
                    });

        }
    }