【发布时间】: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 发送一条消息。
如果json 向onPostExecute 方法发送空值,我将无法进行任何验证并将消息发布到用户界面
请问有没有办法让上面的方法起作用,我会的 如果有人可以提供帮助,将不胜感激。谢谢。
【问题讨论】:
-
该方法必须在所有条件下都返回一个值。您至少应该在方法结束时返回一个空值。
-
你应该把
return null放在最后一行或者if (...)里面 -
关于你的更新:IMO,你可以参考my answer here
标签: android