如何使用共享首选项从 url 获取两个 ID 并将其传递给其他 class

How to pass get two id from url & pass it to other class using shared preference

http://example/map.php?cust_id=1&store_id=2 ,在 link 的 onclick 期间,我从 url 获得了 cust_id 和 store_id 并将其保存在另一个共享首选项中。我正在从其他 class 调用两个 id 并想将两个 id 发送到其他 url 但 id 的值没有上传到那个 url ,如果我单独传递 cust_id 意味着传递两个不起作用的值时一切正常。我是这个概念的新手,请帮助我。

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        CharSequence urlstring1="cust_id";
        CharSequence urlstring2="store_id";
        String[] details = url.split("[?]");
        String[] strdetails =details[1].split("&");
        String strcust_id = strdetails[1];
        String strstore_id= strdetails[1];

        if (url.contains(urlstring1)& url.contains(urlstring2)){
            SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("Customer", strcust_id).putString("Store", strstore_id);
            editor.commit();

            AboutusFragment fragment2 = new AboutusFragment();
            FragmentManager fragmentManager = getFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, fragment2);
            fragmentTransaction.commit();
            return false;
        }else{
            return super.shouldOverrideUrlLoading(view, url);
        }
    }

接下来class

if(CheckNetwork.isInternetAvailable(getContext().getApplicationContext())) //returns true if internet available
    {
        SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String strquerystring=settings.getString("Customer","")+settings.getString("Store","");
        String strs = "http://example/bookappointment.php?" + strquerystring;
        web.setWebViewClient(new myWebClient());
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl(strs);
    }

可能在这里:

String strcust_id = strdetails[1];
String strstore_id= strdetails[1];

行导致问题,因为将 strdetails[1] 相同的值分配给两个变量。

而不是使用 String.split 从 url 获取参数。使用 android.net.Uri 从 URL 获取两个参数,使用如下名称:

Uri uriURL=Uri.parse(url);
String strcust_id=uriURL.getQueryParameter("cust_id");
String strstore_id =uriURL.getQueryParameter("store_id");

现在将 strcust_idstrstore_id 值保存在 SharedPreferences 中。 并确保在 class.

中导入了 android.net.Uri

您的问题出在这些行上:

String strcust_id = strdetails[1];
String strstore_id= strdetails[1];

两者不应该相同,试试这个

String strcust_id = strdetails[0];
String strstore_id= strdetails[1];

split() 有时可能会产生问题,最好使用 getQueryParameter()

Uri uri=Uri.parse(url);
String cust_id=uri.getQueryParameter("cust_id");
String store_id =uri.getQueryParameter("store_id");