我在下面的循环中获取URL字符串
referrer = intent.getStringExtra("referrer");
以下是Log
action: 'com.android.vending.INSTALL_REFERRER' referrer string: 'utm_source=tooyoou&utm_medium=banner&utm_term=foursquare&utm_content=foursquare-tooyoou&utm_campaign=foursquare'
我想解析此URL字符串并获取字符串 “utm_source” “utm_medium” “utm_term” 换成 “的utm_content” “utm_campaign”
我尝试了以下代码,但获得了值
Uri referrerUri = Uri.parse(referrer);
String utmsource= referrerUri.getQueryParameter("utm_source");
String utmmedium= referrerUri.getQueryParameter("utm_medium");
String utmterm= referrerUri.getQueryParameter("utm_term");
String utmcontent= referrerUri.getQueryParameter("utm_content");
String utmcampaign= referrerUri.getQueryParameter("utm_campaign");
Log.d("utmsource===" , utmsource);
Log.d("utmmedium===" , utmmedium);
Log.d("utmterm===" , utmterm);
Log.d("utmcontent===" , utmcontent);
Log.d("utmcampaign===" , utmcampaign);
可能是什么问题?
答案 0 :(得分:4)
您可以使用split
..
String[] referrerList = referrer.split('&');
String utmsource= referrerList[0].substring(referrerList[0].indexOf("=") + 1);
String utmmedium= referrerList[1].substring(referrerList[1].indexOf("=") + 1);
String utmterm= referrerList[2].substring(referrerList[2].indexOf("=") + 1);
....
答案 1 :(得分:2)
您可以执行以下操作,而不是将String
解析为URI
。
String[] uriTokens = referrer.split("&");
for(int i=0;i<uriTokens.length;i++){
String[] valTokens = uriTokens[i].split("=");
switch(valTokens[0]){
case "utm_source":
utmsource = valTokens[1];
break;
case "utm_medium":
utmmedium = valTokens[1];
break;
case "utm_term":
utmterm = valTokens[1];
break;
case "utm_content":
utmcontent = valTokens[1];
break;
case "utm_campaign":
utmcampaign = valTokens[1];
break;
}
}
答案 2 :(得分:0)
或者与番石榴:
private static final String REFERRER = "referrer";
private static final String EQUALS = "%3D";
private static final String AMPERSAND = "%26";
Map<String, String> map = Splitter.on(AMPERSAND).withKeyValueSeparator(EQUALS)
.split(intent.getStringExtra(REFERRER));