onPostExecute将使用2个参数, 来自doInBackground的GCMID 和来自调用AsyncTask的主函数的UserID
我试图更改AsyncTask声明中的参数,但是我收到错误“尝试使用不兼容的返回类型”
String UserID = "Admin";
new GCM().execute(null,null,null);
private class GCM extends AsyncTask<String, String, String> {
@Override
protected String[] doInBackground(String... params) {
String gcmRegID = GHelper.GCMRegister("123456");
return gcmRegID;
}
@Override
protected void onPostExecute(String RegID) {
UTask ut = new UTask();
// ****** I managed to send RegID
// ****** I can not send UserID !! How to send it?
ut.execute(UserID, RegID);
}
}
答案 0 :(得分:2)
试试这个:
String UserID = "Admin";
new GCM().execute(null,null,null);
并且
private class GCM extends AsyncTask<String, String, String[]> {
@Override
protected String[] doInBackground(String... params) {
String regId = somethingToGetRegId();
String userId = somethingToGetUserId();
return new String[]{regId, userId};
}
@Override
protected void onPostExecute(String[] values) {
UTask ut = new UTask();
String regId = values[0];
String userId = values[1];
ut.execute(UserID, RegID);
}
}
答案 1 :(得分:1)
您可以通过GCM构造函数发送UserId。
private class GCM extends AsyncTask<String, String, String>{
private String userId;
public GCM(String UserId){
this.userId=UserId;
}
而不是onPostExecute()&#39;你可以这样使用它:
@Override
protected void onPostExecute(String RegID) {
UTask ut = new UTask();
ut.execute(this.userId, RegID);
}
并像这样使用
new GCM(UserId).execute();
答案 2 :(得分:0)
更改,
@Override
protected String[] doInBackground(String... params) {
String gcmRegID = GHelper.GCMRegister("123456");
return gcmRegID;
}
到
@Override
protected String doInBackground(String... params) {
String gcmRegID = GHelper.GCMRegister("123456");
return gcmRegID;
}
你返回的类型是String [],你在doInBackground()中返回一个String。