3

你好我想问一些问题我想制作与我制作的网络服务连接的应用程序

我的应用程序有 2 个名为 app_id 和令牌的唯一 ID,app_id 仅在应用程序首次启动时生成一次,令牌由 Web 服务生成

每个请求,我必须检查令牌是否已过期,如果令牌已过期,它将调用单独的 Web 服务并生成新令牌

问题是应用程序必须访问 2 个不同的 Web 服务:请求新令牌并获取另一个所需数据

我使用 asynctask,但是 Web 服务对请求令牌的响应总是相同的每个请求,我不知道为什么

protected Boolean doInBackground(Void... params) {
        int status = 0;
        int token_expired=0;
        String token_val = token.getToken(getBaseContext());
        for(int i=0;i<5 && status==0;i++) {
            try {
                Thread.sleep(1000);
                //function to check if token already expired or not and request new token using http post
                token_expired = token.checkToken(getBaseContext());
                System.out.println("token expired: " +token_expired);
                if (token_expired==1 || token_expired==2) {
                     //function to call another web service and get a data from it
                     status =  rclient.Execute("POST");
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (status==0) {
            return false;
        }else{
            return true;
        }
    }

之前谢谢!

4

3 回答 3

0

哦,是的,这是从类令牌处理程序检查令牌的功能

public Integer checkToken(Context context) {
        int status = 0; //0 = failed to request token , 1 = successfully request new token, 2 = token has not expired yet
        String token_id = getToken(context); 
        System.out.println("token_id: " +token_id);
        //if (token_id!=null) {
            Long time = getTime(context);
            Long curr_time = System.currentTimeMillis()/1000;
            System.out.println("time before: " +time);
            System.out.println("time current: " +curr_time);
            Long interval = curr_time - time;
            System.out.println("interval: " +interval);
            if (interval>10) {
                status = TokenGenerator(context);
            }else {
                status = 2;
            }       
        //}
        return status;      
    }

}

这是一个从同一个类请求新令牌的函数

    public synchronized Integer TokenGenerator(Context context) {
     int status = 0;
     SharedPreferences sharedPrefs = context.getSharedPreferences(TOKEN_STORAGE, Context.MODE_PRIVATE);
     uniqueID = sharedPrefs.getString(PREF_UNIQUE_ID, null);
     try {
        rclient.AddJSON("app_id", uniqueID);
        rclient.CompileJSON();
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
     try {
        status =  rclient.Execute("POST");
     } catch (Exception e) {
         e.printStackTrace();
     } 
     if (status==1) {
        String response = rclient.getResponse();
        String token = null;
        System.out.println("uuid_response: " +response);
        try {
            JSONObject json = new JSONObject(response);
            token = json.getString("result");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Long tsLong = System.currentTimeMillis()/1000;
        String ts = tsLong.toString();
        System.out.println("time: " +ts);
        Editor editor = sharedPrefs.edit();
        editor.putString(TIMESTAMP, ts);
        editor.putString(TOKEN_ID, token);
        editor.commit();
        }
return status;

}

所以基本上其余客户端类调用了两次,第一次在类令牌处理程序中请求新令牌,第二次来自活动本身

于 2013-04-12T07:28:58.797 回答
0

根据您发布的代码,我认为 rclient.Execute("POST") 用于获取数据。但是下面的代码

if (token_expired==1 || token_expired==2) {
     //function to call another web service and get a data from it
     status =  rclient.Execute("POST");
}

表示如果令牌仍然存在,您正在尝试再次获取新令牌。我认为行 status = rclient.Execute("POST"); 应替换为从服务器获取数据的代码。

于 2013-04-12T07:39:19.617 回答
0

将休息客户端类的构造函数放入函数后问题解决

于 2013-04-12T08:36:45.830 回答