我怎样才能得到 url 字符串编码

how can I get url string encoded

我想要一个看起来像这样的url

"http://example.com/get_item_data.php?uid="inv_no"

我尝试了 URL 编码器,但无法弄清楚,

我第一次试了这个,没用

try {
   json_url = "http://example.com/get_item_data.php?uid="+  URLEncoder.encode(inv_no, "UTF-8");
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

然后这个

try {
   json_url = "http://example.com/get_item_data.php?uid="+ URLEncoder.encode("inv_no", "UTF-8") + "=" + URLEncoder.encode(inv_no, "UTF-8");
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

还没有结果,求助

在添加“inv_no”变量后对整个字符串进行编码。

try { json_url = URLEncoder.encode('http://example.com/get_item_data.php?uid='+inv_no, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }

通常你不必引用你的参数。所以对我来说你的第一次审判是正确的:

   json_url = "http://example.com/get_item_data.php?uid="+ 
                  URLEncoder.encode( inv_no, "UTF-8");

不确定您要做什么,但考虑到您的问题,我建议使用以下代码:

try {
   json_url = "http://example.com/get_item_data.php?uid=\""+ 
                  URLEncoder.encode( inv_no, "UTF-8") + "\"";
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

在这种情况下,您在编码参数 inv_no 之外引用了引号。

甚至:

try {
   json_url = "http://example.com/get_item_data.php?uid="+ 
                  URLEncoder.encode("\"" + inv_no + "\"", "UTF-8");
} catch (UnsupportedEncodingException e) {
   e.printStackTrace();
}

在这种情况下,参数 inv_no 和引号将被编码。

try {
       String    json_url = "\"http://example.com/get_item_data.php?uid=\"inv_no\"";
     System.out.println(json_url);  
     } catch (Exception e) {
           e.printStackTrace();
        }