我如何传递多个参数的网址是什么? [英] How do I pass multiple parameter in URL?
问题描述
我试图找出如何在URL中传递多个参数。我想通过经度和纬度,从我的Android类一个Java servlet。我该怎么办呢?
I am trying to figure out how to pass multiple parameters in a URL. I want to pass latitude and longitude from my android class to a java servlet. How can I do that?
URL url;
double lat=touchedPoint.getLatitudeE6() / 1E6;
double lon=touchedPoint.getLongitudeE6() / 1E6;
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+lon);
在这种情况下,输出(写入文件)是28.53438677.472097。 这是工作,但我想通过经度和纬度在两个独立的参数,使我在服务器端的工作是减少。如果它是不可能的我怎么能至少增加纬度和放大器之间的空间; LON这样我就可以使用标记生成器类,让我的经度和纬度。我想下面的行,但无济于事。
In this case output (written to file) is 28.53438677.472097. This is working but I want to pass latitude and longitude in two separate parameters so that my work at server side is reduced. If it is not possible how can I at least add a space between lat & lon so that I can use tokenizer class to get my latitude and longitude. I tried following line but to no avail.
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon);
output- Nothing is written to file
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon);
output- 28.534386 (Only Latitude)
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon);
output- 28.532577?param2=77.502996
我的servlet code是如下:
My servlet code is as follows:
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
final String par1 = req.getParameter("param1");
final String par2 = req.getParameter("param2");
FileWriter fstream = new FileWriter("C:\\Users\\Hitchhiker\\Desktop\\out2.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write(par1);
out.append(par2);
out.close();
此外,我想在知道这是从Android设备将数据传递给服务器的最安全和最安全的方式。
Also I wanted to the know is this the most safe and secured way to pass the data from android device to server.
推荐答案
这
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"¶m2="+lon);
必须工作。不管出于什么奇怪的原因,你需要
第一个参数前,&安培;
之前,下面的那些
must work. For whatever strange reason, you need ?
before the first parameter and &
before the following ones.
使用像
url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"_"+lon);
会的工作,太多,但肯定不是很好。不能使用空格那里,因为它是禁止的网址,但你可以连接code为%20
(但是这是更糟糕的风格)。
would work, too, but is surely not nice. You can't use a space there as it's prohibited in an URL, but you could encode it as %20
(but this is even worse style).
这篇关于我如何传递多个参数的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!