我需要输入开始时间和结束时间,在 swing java 中,我将进一步发送到 URL,以使用 GET REST
调用获取此时创建的一些选定节点。
URL
是:
http://wisekar.iitd.ernet.in/active/api_resources.php/method/mynode
?key=YOUR_API_KEY_HERE
&startTime=START_TIME[Optional]
&endTime=END_TIME[Optional]en
网站将采用图像中给出的输入(时间戳)。
我的窗口的屏幕截图
现在我的代码在这里:
class Algorithm extends JFrame implements ActionListener {
private static String ENDPOINT =
"http://wisekar.iitd.ernet.in/active/api_"
+ "resources.php/method/mynode.json?key=api_key";
Algorithm() {
// label1 = new JLabel();.....
panel = new JPanel(new GridLayout(5, 2));
//adding in panel label1,text1 ...
add(panel, BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("Optimal Travel Route");
}
public void actionPerformed(ActionEvent ae) {
try {
//String value1 = text1.getText();..
URL url = new URL(ENDPOINT + "&datasetId=" + value3
+ "&startTime=" + value1 + "&endTime=" + value2);
System.out.println(url);
HttpURLConnection httpCon;
httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoInput(true);
System.out.println(httpCon.getResponseCode());
System.out.println(httpCon.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(
httpCon.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException ex) {
Logger.getLogger(Algorithm.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class AlgorithmDemo {
public static void main(String arg[]) {
try {
Algorithm frame = new Algorithm();
frame.setSize(450, 200);
frame.setVisible(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
我已经尝试了一切;我已经评论过了。使用此代码,当我在浏览器中输入生成的 URL 时,我会获取节点,但当我打印时,它不会在控制台上给出结果。我的代码有什么问题?有人可以告诉我如何将日期和时间传递给 GET。请帮忙。当我只输入数据集 ID 时,它会为我提供该数据集 ID 的所有节点。据我所知,传递时间戳存在一些问题。
最佳答案
不幸的是,当我进行快速测试时,我得到了未经授权的响应。 但问题似乎是该字符串不是 url 安全的。 您需要确保您的空间转换为 %20。 这也是它使用浏览器工作的原因,因为浏览器地址栏会在幕后为您执行此操作。 如果您使用:
String urlSafeValue1 = URLEncoder.encode(value1, "UTF-8");
String urlSafeValue2 = URLEncoder.encode(value2, "UTF-8");
String urlSafeValue3 = URLEncoder.encode(value3, "UTF-8");
参数将被设为 url 安全。
关于java - 在 Swing 中输入格式化的日期和时间以使用 GET REST 获取节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11326848/