0

我对通过 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)
    {

    }
}

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

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

4

4 回答 4

3

更新答案:

现在您的 URL 中有多个错误:

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

它应该是这样的:

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

原始答案:

您当前在第一个参数后有一个空格而不是=符号:

?_userid "+_userid

应该

?_userid="+_userid
于 2012-08-31T13:53:29.683 回答
1

解决了。

URL sourceUrl = new URL("http://0.0.0.0/acd.asmx/GetList?Value1="+Value1+"&ID="+ID);
于 2012-10-25T11:18:54.710 回答
0
"http://10.0.2.2:2291/acd.asmx/Get_Teams?_userid ="+_userid & "_sporttype="+ _sporttype);

你在 _userid 之后有一个 &,它可能知道 _userid 上有什么。通常单个 & 进行二进制操作,因此您可能正在转换来自 _userid 的内容。另外,如果您还没有这样做,我建议您对 REST 标签进行 URLEncoding

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

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

 "http://10.0.2.2:2291/acd.asmx/Get_Teams?_userid ="+_userid + "&_sporttype="+ _sporttype);
于 2012-08-31T14:06:41.557 回答
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("&")
)
于 2021-04-02T21:29:57.267 回答