Android doInBackground方法必须返回JSONObject类型的结果
我试图在子类中结合Android doInBackground方法必须返回JSONObject类型的结果,android,Android,我试图在子类中结合GCMJSONObject实现Asynctask方法。我在doinBackground方法中应用GCM条件时遇到问题 下面是doInBackground方法的标题 protected JSONObject doInBackground(String... args) { GCMRegistrar.checkDevice(context); GCMRegistrar.checkManifest(context); final String regI
GCM
JSONObject
实现Asynctask
方法。我在doinBackground
方法中应用GCM条件时遇到问题
下面是doInBackground方法的标题
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 registrator会将其注册。一旦注册,GCM intent服务就会接管服务器注册,这意味着doInBackground中的json
将向onPostExecute方法发送空值
我还检查onPostExecute方法是否成功和有效。检查Validation后,将向UI发送一条消息
如果json
向onPostExecute
方法发送空值,则我无法进行任何验证,也无法向UI发送消息
请问有没有办法让上述方法奏效,我会的
如果有人能帮忙,我将不胜感激。谢谢。只需在外部声明一个变量,然后返回它
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;
}
如果没有要传回的内容,您可以简单地返回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
,因为您定义了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是否为null-
@Override
protected void onPostExecute(JSONObject jsonObject) {
if(jsonObject != null) {
//Do something i.e. access data
} else {
//Handle null JSON
}
}
该方法必须在所有条件下返回一个值。你至少应该在方法的末尾返回一个空值。你应该把return null
放在最后一行或里面,如果(…)你的更新:IMO,你可以参考感谢帮助,请对此有问题,请检查我的问题,我已经更新了。谢谢,因为我可以看到您正在使用已弃用的GCMRegister。您应该使用GoogleCloudMessaging作为GCMRegister阻止内容,并且必须将其放入AsyncTask中。尝试移植到GoogleCloudMessaging。这会让你的生活变得更轻松。事实上,谷歌云信息让我的生活变得非常轻松,非常感谢@Varundroid