简体   繁体   English

如何使用httpUrlConnection获取url的查询字符串

[英]How to get a query string for a url using httpUrlConnection , android

I am using HttpUrlConnection to post a query to a webpage. 我正在使用HttpUrlConnection将查询发布到网页。 The web page take my request and does another posting. 该网页接受了我的请求,并再次发布。 For example: www.unknown.com 例如:www.unknown.com

URL url = new URL("http://www.unknown.com"); //$NON-NLS-1$
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(15*1000);
urlConnection.setDoInput(true);
urlConnection.setDoOutput (true);
urlConnection.setUseCaches (false);
urlConnection.setAllowUserInteraction(true);
out = new OutputStreamWriter(urlConnection.getOutputStream());
string content = "var1=5&var2=0&var3=6"; //$NON-NLS-1$
out.write(content);
out.flush();
out.close();

The code is working without problem and I am getting the html code. 该代码可以正常工作,并且我正在获取html代码。 The problem is that the webpage is proceeding another HTTP "POST" method when it is taking my request. 问题在于,网页在接受我的请求时正在处理另一个HTTP“ POST”方法。 For example after my request the URI is: http://www.unknown.com?var1=5&var2=0&var3=6&var4=1500 例如,在我请求之后,URI是: http : //www.unknown.com? var1=5& var2=0& var3=6& var4=1500

I need to get the value of "var4" in my code and I can not find any solution for this. 我需要在代码中获取“ var4”的值,但找不到任何解决方案。 HttpUrlConnection.getUrl() returns just the address http://www.unknown.com ! HttpUrlConnection.getUrl()仅返回地址http://www.unknown.com Have anybody a suggestion? 有任何建议吗? Thanks. 谢谢。

How about using "getQuery()" to retrieve the Query String. 如何使用“ getQuery()”来检索查询字符串。

String query = url.getQuery();

This will give you the the query part of this URL. 这将为您提供该URL的查询部分。

Use StringTokenizer to separate the parameters. 使用StringTokenizer分隔参数。 (You will have to apply StringTokenizer twice.) (您将必须两次应用StringTokenizer。)

First get tokens from query string which are separated by "&". 首先从查询字符串中获取用“&”分隔的令牌。 This will return "val1=5" , "val4=1500", etc. 这将返回“ val1 = 5”,“ val4 = 1500”等。

To the above tokens apply StrinTokenizer once again. 对上述令牌再次应用StrinTokenizer。 This time retrieve tokens separated by the "=". 这次检索以“ =”分隔的令牌。 Now iterate through this, the first token will be the parameter name "val4", the second token will be the value "1500". 现在,通过它进行迭代,第一个标记将是参数名称“ val4”,第二个标记将是值“ 1500”。

StringTokenizer st = new StringTokenizer(query,"&",false); //query  is from getQuery()
while (st.hasMoreElements()) 
{   // First Pass to retrive the "parametername=value" combo

    String paramValueToken = st.nextElement().toString();

    StringTokenizer stParamVal = new StringTokenizer(paramValueToken, "=", false );

     int i = 0;
     while (stParamVal.hasMoreElements()) 
     {
        //Second pass to separate the "paramname" and "value".
        // 1st token is param name
        // 2nd token is param value


        String separatedToken = stParamVal.nextElement().toString();

        if( i== 0)
        {
            //This indicates that it is the param name : ex val4,val5 etc
            String paramName = separatedToken;
        }
        else 
        {
            // This will hold value of the parameter
            String paramValue = separatedToken;
        }
        i++;
     }
}

URL getQuery() API Documentation URL getQuery()API文档

http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM