有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将asynchttpclient作为maven项目发送异步post请求并运行

我在这里完全疯了。这是我第一次使用java,它让我非常害怕

无论如何,我需要做的是创建一个小型java maven项目,在其中我监听一个特定的UDP端口(python脚本atm,模拟我应该使用的设备),然后在发送一个post请求到一个带有两个post参数的特定url之前做一些事情

我发现这个{a1}图书馆似乎得到了良好的评价和声誉

到目前为止我完成了什么

  1. 我已经成功创建了一个maven项目(显然)
  2. 编写了receiver类(工作非常完美)——捕获UDP消息并存储稍后发送POST消息时需要存储的内容的类
  3. 编写了负责使用库发送POST消息的代码(我不确定它是否有效,因为我无法运行它):
    // To make post request to send data
  AsyncHttpClient client = Dsl.asyncHttpClient();

  BoundRequestBuilder postRequest = client
          .preparePost("http://localhost:8080/my_custom_url/")
          .addFormParam("id", myid)
          .addFormParam("stream", inData);

  // Asynchronous execution
  postRequest.execute(new AsyncHandler<Object>() {
      public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
          System.out.print("\nstatus received");
          return null;
      }

      public State onHeadersReceived(HttpHeaders headers) throws Exception {
          System.out.print("\nheaders received");
          return null;
      }

      public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
          System.out.print("\nbody part received");
          return null;
      }

      public void onThrowable(Throwable t) {

      }

      public Object onCompleted() throws Exception {
          System.out.print("\nexecute completed");
          return null;
      }
  });

  // Close to stop memory leaks after we are done
  try {           client.close();         } catch (IOException e) {           infoBox("Could not close AsyncHttpClient.", "Error");
      System.exit(0);         }
  1. 通过执行:pom构建maven项目(从eclipse中)。xml>;右键单击>;运行方式>;Maven Build&燃气轮机;目标设定为“套餐”
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MyProject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MyProject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\me\eclipse-workspace\MyProject\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MyProject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ MyProject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\me\eclipse-workspace\MyProject\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ MyProject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MyProject ---
[INFO] Surefire report directory: C:\Users\me\eclipse-workspace\MyProject\target\surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running me.company.MyProject.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ MyProject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.044 s
[INFO] Finished at: 2018-05-21T13:23:49+03:00
[INFO] Final Memory: 10M/245M
[INFO] ------------------------------------------------------------------------
  1. 尝试通过powershell运行以下操作:
PS C:\Users\me\eclipse-workspace\MyProject\target> java -cp .\MyProject-0.0.1-SNAPSHOT.jar me.company.MyProject.App
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/asynchttpclient/AsyncHandler
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.asynchttpclient.AsyncHandler
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more

如果您能在这方面给予帮助,我们将不胜感激。乐意在需要时提供更多信息


共 (0) 个答案