0
您好我正在編寫一個本地託管的Web服務器,它將在Android設備的URL/URI中發佈緯度和經度,並將用作SQL中的搜索條件選擇查詢以檢索5個關閉的火車站。從數據庫獲取/發佈web服務
我與硬編碼的經度和緯度的代碼工作,但現在需要在它的功能添加動態地添加形式使用Post /獲取功能格蘭Adnroid內設備,不幸的是我從來沒有使用GET/POST等等不知道從哪裏開始。
下面是我在網絡服務器的所有類的代碼,正如所說的所有工作硬編碼,但現在需要接受來自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;
}
}
我目前連接到主機相同的數據並完全functinal和端口號後,其格式如下
/stations?lat=" + lat + "&lng=" + lng);
其中緯度和經度另一個網站服務器是我的變量採用GPS