如何在URL中传递多个参数?
我正在尝试弄清楚如何在URL中传递多个参数。我想将纬度和经度从我的Android类传递到java servlet。我该怎么做?
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
lat
lon
tokenizer
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 代码如下:
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设备传递到服务器的最安全可靠的方法。