在安卓中进行多个HTTP POST请求

3

你好,我想问一些问题。我想制作一个应用程序,与我创建的Web服务连接。

我的应用程序有两个唯一标识符,称为app_id和token。app_id在应用程序第一次启动时仅生成一次,而token由Web服务生成。

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

问题是应用程序必须访问2个不同的Web服务:请求新的token和获取其他所需数据。我使用asynctask,但是请求token的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;
        }
    }

谢谢您的信任!


你的过期令牌功能代码在哪里? - Kameswari
我在下面发布了我的另一个函数。 - Rico Harisin
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;

}

大致上,Rest客户端类会被调用两次,第一次是在Token处理器类中请求新令牌,第二次是从活动本身调用。


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");应替换为从服务器获取数据的代码。


0
问题解决了,我把 REST 客户端类的构造函数放在函数里面。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接