您好我正在编写一个本地托管的Web服务器,它将在Android设备的URL / URI中发布经度和经度,这将用作SQL Select查询中的搜索条件,以检索5个关闭列车站
我已经使用硬编码的经度和纬度使代码工作但现在需要添加它的功能,使用Post / Get功能从adnroid设备动态添加,不幸的是我从未使用过get / post所以不要知道从哪里开始。
下面是我在Web服务器中所有类的代码,因为它说所有工作都是硬编码但现在需要接受来自Android设备的输入并返回相同的预期结果。感谢
public class WebServer {
static String jArray = "";
public static void main(String[] args) {
try{
HttpServer server = HttpServer.create(new InetSocketAddress(8080),0);
server.createContext("/",new HttpHandler(){
public void handle(HttpExchange he) throws IOException{
try {
jArray = sqlConnector.train(jArray);
} catch (Throwable e) {
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(he.getResponseBody()));
System.out.println("Processing Request");
he.sendResponseHeaders(200, 0);
String output = "<html><head></head><body><p>" + jArray + "</p></body></html>";
bw.write(output);
bw.close();
}
});
server.start();
System.out.println("Started up . . .");
}
catch (IOException ioe){
System.err.println("problems Starting Webserver: " + ioe);
}
}
}
public class sqlConnector {
public static String train(String jArray) throws Exception{
PreparedStatement s = null;
try
{
Connection c = DriverManager.getConnection("jdbc:sqlite:C:/Users/Colin/trainstations.db");
s = c.prepareStatement("SELECT Latitude, Longitude, StationName,( 3959 * acos(cos(radians(53.4355)) * cos(radians(Latitude)) * cos(radians(Longitude) - radians(-3.0508)) + sin(radians(53.4355)) * sin(radians(Latitude )))) AS distance FROM stations ORDER BY distance ASC LIMIT 0,5;");
ResultSet rs = s.executeQuery();
jArray = jsonConverter.convertResultSetIntoJSON(rs, jArray);
}
catch (SQLException se)
{
se.printStackTrace();
}
return jArray;
}
}
public class jsonConverter {
public static String convertResultSetIntoJSON(ResultSet rs, String jArray) throws Exception {
JSONArray jsonArray = new JSONArray();
while (rs.next()) {
int total_rows = rs.getMetaData().getColumnCount();
JSONObject obj = new JSONObject();
for (int i = 0; i < total_rows; i++) {
String columnName = rs.getMetaData().getColumnLabel(i + 1).toString();
Object columnValue = rs.getObject(i + 1);
obj.put(columnName, columnValue);
}
jsonArray.put(obj);
}
jArray = jsonArray.toString();
return jArray;
}
}
我目前连接到另一个托管相同数据的网络服务器,并且完全是功能性的,在端口号之后,其格式如下
/stations?lat=" + lat + "&lng=" + lng);
其中lat和lng是我使用GPS获取的变量
答案 0 :(得分:0)
过程如下:
1)从查询字符串中解析参数字符串he.getRequestURI()。getQuery()
/**
* returns the url parameters in a map
* @param query
* @return map
*/
public static Map<String, String> queryToMap(String query){
Map<String, String> result = new HashMap<String, String>();
for (String param : query.split("&")) {
String pair[] = param.split("=");
if (pair.length>1) {
result.put(pair[0], pair[1]);
}else{
result.put(pair[0], "");
}
}
return result;
}
2)将这些参数传递给select方法
public static String train(String jArray, double lat, double long) throws Exception{
3)在你的陈述中使用它
s = c.prepareStatement("SELECT Latitude, Longitude, StationName,
( 3959 * acos(cos(radians(?)) * cos(radians(Latitude))
* cos(radians(Longitude) - radians(?)) + sin(radians(?))
* sin(radians(Latitude ))))
AS distance FROM stations ORDER BY distance ASC LIMIT 0,5;");
s.setDouble(1, lat);
s.setDouble(2, long);
s.setDouble(3, lat);
最后
4)修复你的代码。
希望这有助于作为一个粗略的指导方针: - )