简体   繁体   English

如何从独立的Java应用程序进行HTTP调用

[英]How to make http call from standalone java application

I'm making a small dictionary kind of app using java swings. 我正在使用Java swing创建一个小型字典类的应用程序。 I'm using oxford dictionary api for that. 我为此使用牛津字典API。 Is there any way to make a simple ajax request in java without using servelets and all advanced java concepts. 有没有办法在不使用servelet和所有高级java概念的情况下用java发出简单的ajax请求。 As in android we use http url connection to do this job.I googled a lot for finding this but I could't find a solution as every page is showing results using servelets. 就像在android中一样,我们使用http url连接来完成这项工作。我在Google上进行了大量的搜索,但是我找不到解决方案,因为每个页面都使用servelet显示结果。 But I know core java alone.If it is possible to make ajax call without servelts please help me...Thanks in advance... 但是我只了解核心Java,如果可以在不使用Servlts的情况下进行Ajax调用,请帮助我...在此先感谢...

Use HttpURLConnection class to make http call. 使用HttpURLConnection类进行http调用。

If you need more help for that then go for offical documentation site of java Here Example 如果您需要更多的帮助,了解,然后去的java的官方文档站点这里

public class JavaHttpUrlConnectionReader {
    public static void main(String[] args) throws IOException{
        String results = doHttpUrlConnectionAction("https://your.url.com/", "GET");
        System.out.println(results);
    }
    public static String doHttpUrlConnectionAction(String desiredUrl, String requestType) throws IOException {
        BufferedReader reader = null;
        StringBuilder stringBuilder;
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(desiredUrl).openConnection();
            connection.setRequestMethod(requestType);// Can be "GET","POST","DELETE",etc
            connection.setReadTimeout(3 * 1000);
            connection.connect();// Make call
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));// Reading Responce
            stringBuilder = new StringBuilder();

            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            throw new IOException("Problam in connection : ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ioe) {
                    throw new IOException("Problam in closing reader : ", ioe);
                }
            }
        }
    }
}

It will make a call and give response as return string. 它将进行呼叫并以返回字符串形式给出响应。 If you want to make POST call the need to do some extra for that : 如果您想进行POST呼叫,则需要为此做一些额外的工作:

try{
   DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
   wr.write(postParam.getBytes());
} catch(IOException e){
}

Note : Here postParam is String type with value somthing like "someId=156422&someAnotherId=32651" 注意:这里的postParamString类型,其值类似于"someId=156422&someAnotherId=32651"

And put this porson befor connection.connect() statement. 并将此porson放在connection.connect()语句之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使Java应用程序独立? - How to make Java application standalone? 从Java Applet调用系统功能或独立应用程序的解决方案 - Solution to call a system function or standalone application from Java Applet 如何从Java独立应用程序在前台打开Windows应用程序 - How to open windows application in foreground from Java standalone application 如何创建Java独立应用程序 - How to create a Java Standalone Application Birt:如何在独立的Java应用程序中从rtpdesign文件生成PDF - Birt : How to generate PDF from rtpdesign file in standalone Java application 当您从插件内的Eclipse工作台调用Java独立应用程序时,这表明进程间通信吗? - When you call a java standalone application from an eclipse workbench inside a plugin, this indicates interprocess communication? 为什么从Java应用程序连接到Spark Standalone时,“无法调用已停止的SparkContext上的方法”? - Why is “Cannot call methods on a stopped SparkContext” thrown when connecting to Spark Standalone from Java application? 如何使RCP应用程序真正独立? - How to make RCP application really standalone? 如何使简化的RCP应用程序真正独立? - How to make simplistic RCP application really standalone? 如何为独立的Java应用程序存储简单数据 - How to store simple data for standalone java application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM