在android中传递带有URL链接的两个参数

时间:2012-08-31 13:50:27

标签: android url parameter-passing

我有两个参数传递URL链接的问题。任何人都可以帮助我吗?

private void FillDetails(String _userid,int _sporttype) {
    al_TeamName=new ArrayList<String>();

    try{
        spf=SAXParserFactory.newInstance();
        sp=spf.newSAXParser();
        xr=sp.getXMLReader();
        URL sourceUrl = new URL(
        "http://10.0.2.2:2291/acd.asmx/Get_Teams?_userid ="+_userid & "_sporttype="+ _sporttype);
        MyHandler mh=new MyHandler();
        xr.setContentHandler(mh);

        xr.parse(new InputSource(sourceUrl.openStream()));
        setListAdapter(new MyAdapter());


    }
    catch(Exception ex)
    {

    }
}

当我使用此代码时,我得到null。如果我发送单个参数,那么它工作正常。 这是传递两个参数的URL的正确过程吗?

提前致谢..........

4 个答案:

答案 0 :(得分:3)

更新的答案:

现在您的网址中有多处错误:

URL sourceUrl = new URL("http://10.0.2.2:2291/acd.asmx/Get_Teams?_userid =" + 
    _userid & "_sporttype="+ _sporttype); 
  1. 在第一个=标志
  2. 之前仍有空格
  3. +变量与字符串其余部分之间没有_userid
  4. &符号位于第二个字符串
  5. 之外

    它应该是这样的:

    URL sourceUrl = new URL("http://10.0.2.2:2291/acd.asmx/Get_Teams?_userid=" 
        + _userid + "&_sporttype=" + _sporttype);
    

    原始答案:

    您的第一个参数后,您目前有一个空格而不是=个符号:

    ?_userid "+_userid
    

    应该是

    ?_userid="+_userid
    

答案 1 :(得分:1)

解决。

URL sourceUrl = new URL("http://0.0.0.0/acd.asmx/GetList?Value1="+Value1+"&ID="+ID);

答案 2 :(得分:0)

"http://10.0.2.2:2291/acd.asmx/Get_Teams?_userid ="+_userid & "_sporttype="+ _sporttype);

你有一个&amp;在_userid之后,这可能是谁知道_userid上的内容。通常单身&amp;做二进制操作,所以你可能正在改变_userid的内容。另外,如果您还没有这样做,我建议您对REST标记进行URLE编码

我建议在开发过程中记录REST参数,并仔细检查它是否正确形成

更新:&amp;在引用之外,您需要使用+

 "http://10.0.2.2:2291/acd.asmx/Get_Teams?_userid ="+_userid + "&_sporttype="+ _sporttype);

答案 3 :(得分:0)

如果你来到这里是因为你搜索了一个在 Kotlin 中工作的版本(像我一样),你可以使用这个函数来构建你的 URL:

import java.net.URL

// Your URL you want to append the query on
val url: String = "http://10.0.2.2:2291/acd.asmx/Get_Teams"

// The parameters you want to pass
val params: Map<String, String> = mapOf(
        "_userid"      to _user_id
        , "_sporttype" to _sporttype
)

// The final build url. Standard encoding for URL is already utf-8
val final_url: URL = URL(
        "$url?" // Don't forget the question-mark!
        + params.map {
            "${it.key}=${it.value}"
        }.joinToString("&")
)
相关问题