【问题标题】:doInBackground method must return a result of type JSONObjectdoInBackground 方法必须返回 JSONObject 类型的结果
【发布时间】:2015-12-15 01:05:56
【问题描述】:

我正在尝试在子类中结合GCM JSONObject 来实现Asynctask 方法。我在 doinBackground 方法中应用 GCM 条件时遇到问题。

下面是doInBackground method的标题

 protected JSONObject doInBackground(String... args) {

    GCMRegistrar.checkDevice(context);
     GCMRegistrar.checkManifest(context);
     final String regId = GCMRegistrar.getRegistrationId(context);
     if (regId.equals("")) {        
        GCMRegistrar.register(context,SENDER_ID);
    }else {
        if (GCMRegistrar.isRegisteredOnServer(context)) {               
            Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
        } else {
                UserFunctions userFunction = new UserFunctions();
                JSONObject json =userFunction.registerUser(context,fname, lname, email, password,regId);
                return json;
        }
    }
 }

我的 IDE 不允许执行它。这是它给出的错误信息。

此方法必须返回 JSONObject 类型的结果

现在,当从条件中取出返回值时,上面的代码可以正常工作,但是假设只有在条件为真时才执行返回值。

更新

这还有另一个问题。 json 在 doInBackground 方法中总是返回 null,因为当 GCM registrationID 为空时,GCM Registrar 会注册它。一旦注册,GCM Intent 服务就会接管服务器注册,这意味着 doInBackground 中的json 将向onPostExecute method 发送空值。

我还在 onPostExecute 方法中检查成功和验证。检查验证后,会向 UI 发送一条消息。

如果jsononPostExecute 方法发送空值,我将无法进行任何验证并将消息发布到用户界面

请问有没有办法让上面的方法起作用,我会的 如果有人可以提供帮助,将不胜感激。谢谢。

【问题讨论】:

  • 该方法必须在所有条件下都返回一个值。您至少应该在方法结束时返回一个空值。
  • 你应该把return null放在最后一行或者if (...)里面
  • 关于你的更新:IMO,你可以参考my answer here

标签: android


【解决方案1】:

因为你定义了 JSONObject。试试这个 -

protected JSONObject doInBackground(String... args) {

    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context);
    final String regId = GCMRegistrar.getRegistrationId(context);
    JSONObject jsonObject = null; //Declaration
    if (regId.equals("")) {
        GCMRegistrar.register(context,SENDER_ID);
    }else {
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
        } else {
            UserFunctions userFunction = new UserFunctions();
            jsonObject =userFunction.registerUser(context,fname, lname, email, password,regId);

        }
    }
    return jsonObject;
}

现在在您的 onPostExecute() 中,只需在开始访问数据之前检查您的 JSONObject 是否为空 -

@Override
protected void onPostExecute(JSONObject jsonObject) {
    if(jsonObject != null) {
        //Do something i.e. access data
    } else {
        //Handle null JSON
    }
}

【讨论】:

  • 感谢您的帮助,请对此有问题,请检查我的问题,我已更新。谢谢
  • 我可以看到您正在使用已弃用的 GCMRegister。您应该将 GoogleCloudMessaging 用作 GCMRegister 块,并且必须放入 AsyncTask。尝试移植到 GoogleCloudMessaging。它会让你的生活更轻松。
  • 确实 GoogleCloudMessaging 让我的生活变得非常轻松,非常感谢@Varundroid
【解决方案2】:

只要在外面声明一个变量,然后返回它。

protected JSONObject doInBackground(String... args) {

    JSONObject json = null;

    GCMRegistrar.checkDevice(context);
    GCMRegistrar.checkManifest(context);
    final String regId = GCMRegistrar.getRegistrationId(context);

    if (regId.equals("")) {        
       GCMRegistrar.register(context,SENDER_ID);
    } else {
       if (GCMRegistrar.isRegisteredOnServer(context)) {               
           Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
       } else {
            UserFunctions userFunction = new UserFunctions();
            json = userFunction.registerUser(context,fname, lname, email, password,regId);
       }
    }

    return json;
}

【讨论】:

    【解决方案3】:

    如果没有什么可以传回的,你可以简单地return null

    protected JSONObject doInBackground(String... args) {
    
        // declare it here
        JSONObject json = null;
        GCMRegistrar.checkDevice(context);
        GCMRegistrar.checkManifest(context);
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {        
            GCMRegistrar.register(context,SENDER_ID);
        }else {
            if (GCMRegistrar.isRegisteredOnServer(context)) {               
                Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
            } else {
                UserFunctions userFunction = new UserFunctions();
                json =userFunction.registerUser(context,fname, lname, email, password,regId);
            }
        }
    
        // if the criteria isn't met then return the null object reference
        return json;
    }
    

    然后只需在onPostExecute() 中进行空引用检查。

    如果你告诉方法返回某种类型的东西,那么你需要这样做。您只需要始终提前考虑对象是否可以是null

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-16
      • 2013-12-18
      • 2015-09-25
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多