使用Retrofit2的Android Studio上位置0的JSON中的意外令牌#错误 [英] Unexpected token # in JSON at position 0 Error on Android Studio using Retrofit2

查看:56
本文介绍了使用Retrofit2的Android Studio上位置0的JSON中的意外令牌#错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,"JSON在位置0出现意外的令牌#".

I have a problem that "Unexpected token # in JSON at position 0".

我想做的是在Firebase上使用CloudFunction删除FirebaseUser.

What I wanna do is FirebaseUser delete using CloudFunction on Firebase.

没有返回,但是错误提示有意外的json令牌#"

There is no return but the Error says that "there is unexpected json token #"

这是一些代码:

  • CloudFunctionsService(Interface)
public interface CloudFunctionsService {

    @POST("deleteUser")
    Call<Void> deleteUser(@Body String uid);
}

  • FunctionRetrofit(RetrofitClass)
  • public class FunctionRetrofit {
        private static FunctionRetrofit instance = new FunctionRetrofit();
    
        public static FunctionRetrofit getInstance(){
            return instance;
        }
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
        private CloudFunctionsService cfs = retrofit.create(CloudFunctionsService.class);
        public CloudFunctionsService getService(){
            return cfs;
        }
    
    }
    

    • 函数deleteUser(在FirebaseCloudFunction上)
    • exports.deleteUser = functions.https.onRequest((req, res) => {
          if (req.body.uid === undefined) {
            res.status(400).send('No user id defined');
          } else {
            var userId = req.body.uid;
      
            admin.auth().deleteUser(userId)
                .then(function() {
                    console.log("Successfully deleted user");    
                })
                .catch(error=> {
                    console.log("Error deleting user: ", error);
                });
      
            res.status(200).end();
          } 
        });
      
      

      • 代码执行(活动)结果就是成功,但实际上什么都没改变
      • Call<Void> res = FunctionRetrofit.getInstance().getService().deleteUser(user.getUid());
                        res.enqueue(new Callback() {
                            @Override
                            public void onResponse(Call call, Response response) {
                                Log.d("success", "suceess");
        
                            }
        
                            @Override
                            public void onFailure(Call call, Throwable t) {
                                Log.e("Error", t.getMessage().toLowerCase());
                            }
                        });
        
        

        • 错误
        • SyntaxError: Unexpected token # in JSON at position 0
              at JSON.parse (<anonymous>)
              at createStrictSyntaxError (/worker/node_modules/body-parser/lib/types/json.js:157:10)
              at parse (/worker/node_modules/body-parser/lib/types/json.js:83:15)
              at /worker/node_modules/body-parser/lib/read.js:121:18
              at invokeCallback (/worker/node_modules/raw-body/index.js:224:16)
              at done (/worker/node_modules/raw-body/index.js:213:7)
              at IncomingMessage.onEnd (/worker/node_modules/raw-body/index.js:273:7)
              at emitNone (events.js:106:13)
              at IncomingMessage.emit (events.js:208:7)
              at endReadableNT (_stream_readable.js:1064:12)
          

          推荐答案

          当您使用 GsonConverterFactory 时,我认为当使用 @Body 批注.当您传递原始的String值时,我认为这是错误所在.

          As you are using the GsonConverterFactory, I think it's expecting json (or to serialize to JSON) when you use the @Body annotation. As you are passing a raw String value I think this is where it errors.

          请忽略上面的答案.GsonConverterFactory会将您自己的Type序列化为JSON,但是您要发送的是原始String值.这将不会被序列化,因此ID为3的帖子的正文将为"3"-我认为您正在调用deleteUser的api期望的是未发送的正文中的JSON,这就是为什么收到错误消息的原因.我将检查您正在制作的Firebase API调用的文档,以查看其期望帖子正文采用的格式.它更有可能是这样的:

          Please disregard the answer above. The GsonConverterFactory will serialise your own Type to JSON, however you are sending in a raw String value. This will not be serialized so the body of the post for an id of 3 will be "3" - I think the api you are calling for deleteUser is expecting JSON in the body which you are not sending which is why you are getting the error. I would check the docs of the Firebase API call you are making to see what format it expects the post body to be in. It is more likely to be something like:

          {
              "userId": "3"
          }
          

          如果是这种情况,那么您将需要一个类似的类:

          If this is the case then you would need a class like:

          public class User {
              private String userId;
          
              public User(String userId){
                  this.userId  = userId;
              }
          }
          

          当您可能期望{表示开始一个JSON对象时,您当前正在发送一个字符作为第一个字符

          You are currently sending a " character as the first character when it's probably expecting a { to signify starting a JSON object

          这篇关于使用Retrofit2的Android Studio上位置0的JSON中的意外令牌#错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆