将令牌和 ID 发送到服务器 [英] Sending token and Id to Server

查看:52
本文介绍了将令牌和 ID 发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个使用 JSinterface 获取 user_id 的 webview 应用程序.我想要做的是向我的服务器发送两个值,user_id 和 FCM 令牌,onpause().我像这样在 onCreate() 中获取并保存 fcm 令牌.

I am trying to develop a webview app that uses JSinterface to get the user_id. what I am trying to do is send two values to my server, user_id and FCM token,onpause(). i am getting and saving the fcm token inside my onCreate() like that.

FirebaseInstanceId.getInstance().getInstanceId()
            .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                @Override
                public void onComplete(@NonNull Task<InstanceIdResult> task) {
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "Failed", task.getException());

                        return;
                    }

                    String token = task.getResult().getToken();
                    SharedPreferences sharedPreferences
                            = getSharedPreferences("info",
                            MODE_PRIVATE);
                    SharedPreferences.Editor myEdit
                            = sharedPreferences.edit();
                    myEdit.putString("token",
                            token);
                    myEdit.commit();
                    Log.i("FCM", "Current token=" + token);

                }
            });

我正在通过这个类获取用户 ID.

i am getting the user id with this class.

public class JsInterface {
private Context mcontext;


public JsInterface(Context c) {
    this.mcontext = c;
}

@JavascriptInterface
public void getid(String userid){
    int id;
    try {
        id = Integer.parseInt(userid);
    }
    catch (NumberFormatException e)
    {
        id = -1;
    }
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mcontext);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt("userid", id);
    editor.commit();
}

public int LoadId(){
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mcontext);
    int userid = sharedPreferences.getInt("userid", -1);
    return userid;

}}

我尝试将值发送到服务器:

What i've tried regarding sending the values to the server:

 class AsyncT extends AsyncTask<Void,Void,Void> {

    @Override
    protected Void doInBackground(Void...params) {

        try {
            URL url = new URL("Myurl"); //Enter URL here
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            SharedPreferences sh
                    = getSharedPreferences("info",MODE_APPEND);

            String token = sh.getString("token", "");
            int id = sh.getInt("userid", -1);

            JSONObject jsonObject = new JSONObject();


            DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
            wr.writeBytes(jsonObject.toString());
            wr.flush();
            wr.close();
            Log.i("Token", "Current token=" + token); //the logs dont have the correct values
            Log.i("id", "Current id=" + id);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

 @Override
protected void onPause()
{
    AsyncT asyncT = new AsyncT();
    asyncT.execute();
    super.onPause();
}

我完全没有使用 sharedPrefs 的经验,我无法理解 JSinteface 上的上下文部分,所以这就是为什么我认为这不起作用.

i am not experienced with sharedPrefs at all and i cant understand the context part on the JSinteface so thats why i think this doesnt work.

推荐答案

尝试创建一个类来更新您的默认共享首选项并从中获取价值,这将帮助您避免混淆:

try to make one class for updating and getting value from your default sharedpreference, this will help you avoid confusion:

    public class sharedpref {
    
        private SharedPreferences mysharedpref;
    
        public sharedpref(Context cntx) { // pass context of activity you are using this
          
            mysharedpref = PreferenceManager.getDefaultSharedPreferences(cntx);
        }
    
        public void setuserid(String userid) {
            prefs.edit().putString("userid", userid).commit();
        }
     public  String getuserid(){
            String userid = prefs.getString("userid","");
            return  userid;
        }
      public void settoken(String token) {
            prefs.edit().putString("token", token).commit();
        }
     public  String gettoken(){
            String token = prefs.getString("token","");
            return  token;
        }
}

现在在任何活动中只需调用:

Now in any activity just call:

private sharedpref sp = new sharedpref(this.getApplicationContext());
// then simply perform your operations

sp.settoken(value);
sp.getuserid();

这篇关于将令牌和 ID 发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆