【问题标题】:How to pass get two id from url & pass it to other class using shared preference如何从 url 获取两个 id 并使用 sharedpreferences 将其传递给另一个类
【发布时间】:2016-04-05 02:52:12
【问题描述】:

http://example/map.php?cust_id=1&store_id=2 ,在链接中的 onclick 期间,我从 url 获取 cust_id 和 store_id 并将其保存在另一个共享首选项中。我正在从其他类调用两个 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);
        }
    }

下一堂课

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);
    }

【问题讨论】:

  • 尝试更改 editor.putString("Customer", strcust_id).putString("Store", strstore_id);到 editor.putString("Customer", strcust_id); editor.putString("Store", strstore_id);
  • 如果只传递一个 id,则完全在同一代​​码中意味着 cust_id 存储在共享首选项中,并且在下一个类中,它可以正确上传到下一个 url,但对于两个字符串不能正常工作。我有这个结构,请帮助我
  • 在使用这些值之前尝试做日志。
  • 字符串 strcust_id = strdetails[1];字符串 strstore_id=strdetails[1];您正在为这些变量分配相同的值。这就是你想要的吗?
  • @ajantha 我按照你说的方式试过了,但它不起作用。

标签: android string url sharedpreferences parameter-passing


【解决方案1】:

大概在这里:

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 中。 并确保android.net.Uri 包被导入到类中。

【讨论】:

    【解决方案2】:

    您的问题在于以下几行:

    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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 2021-03-10
      • 2021-02-05
      相关资源
      最近更新 更多