(android)使用httpurlconnection编写curl post命令

wlsrxk51  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(504)

我正在尝试实现一个基于curl的代码,它通过android应用程序与基于rqlite的服务器通信。通常,该命令与服务器进行sql查询/执行,以返回json数据/响应。
curl命令: curl -XPOST -k 'https://192.168.9.4:401/db/query?pretty' -H "Content-Type:application/json" -d '[" SELECT * FROM ABCTable "]' 上面命令中的url是 'https://192.168.9.4:401/db/query?pretty' 数据: [" SELECT * FROM ABCTable "] android的java代码:

  1. private void SendRequest() {
  2. HttpsURLConnection httpsURLConnection = null;
  3. try {
  4. URL url = new URL("192.168.9.4:401/db/query");
  5. httpsURLConnection = (HttpsURLConnection) (url.openConnection());
  6. String data1 = "[\n\"SELECT * FROM ABCTable\"\n]";
  7. httpsURLConnection.setDoOutput(true);
  8. httpsURLConnection.setRequestMethod("POST");
  9. httpsURLConnection.addRequestProperty("Content-Length", Integer.toString(data1.length()));
  10. httpsURLConnection.addRequestProperty("Content-Type", "application/json");
  11. httpsURLConnection.setUseCaches( false );
  12. int responseCode = httpsURLConnection.getResponseCode();
  13. if (responseCode == httpsURLConnection.HTTP_OK) {
  14. BufferedReader data = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
  15. StringBuffer sb = new StringBuffer("");
  16. String line = "";
  17. while ((line = data.readLine()) != null) {
  18. sb.append(line);
  19. }
  20. Log.d("Result",sb.toString());
  21. }
  22. } catch (IOException e){
  23. e.printStackTrace();
  24. } finally {
  25. if (httpsURLConnection != null){
  26. httpsURLConnection.disconnect();
  27. }
  28. }
  29. }

问题是我没有从代码中得到任何类型的响应/错误。有没有人能指出我在表格文件中遗漏的或一般性的错误?
注意:我在互联网上看到过很多例子,但大多数都是基于在查询中发送基于json的数据,但我必须发送一个简单的数据字符串来给出响应。连接是基于https的连接,因此httpsurlconnection。
日志:

  1. 06-17 15:03:23.075 28254-28254/? I/art: Late-enabling -Xcheck:jni
  2. 06-17 15:03:23.147 28254-28254/com.base.httppost W/System: ClassLoader referenced unknown path: /data/app/com.base.httppost-2/lib/arm
  3. 06-17 15:03:23.188 28254-28254/com.base.httppost W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
  4. 06-17 15:03:23.229 28254-28254/com.base.httppost I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
  5. 06-17 15:03:23.229 28254-28254/com.base.httppost I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
  6. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: java.net.MalformedURLException: Unknown protocol: user
  7. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at java.net.URL.<init>(URL.java:182)
  8. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at java.net.URL.<init>(URL.java:125)
  9. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at com.base.httppost.MainActivity.SendRequest(MainActivity.java:30)
  10. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at com.base.httppost.MainActivity.onCreate(MainActivity.java:24)
  11. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at android.app.Activity.performCreate(Activity.java:6259)
  12. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
  13. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
  14. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
  15. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java)
  16. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
  17. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
  18. 06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: at android.os.Looper.loop(Looper.java:148)
  19. 06-17 15:03:23.321 28254-28254/com.base.httppost W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5443)
  20. 06-17 15:03:23.321 28254-28254/com.base.httppost W/System.err: at java.lang.reflect.Method.invoke(Native Method)
  21. 06-17 15:03:23.321 28254-28254/com.base.httppost W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
  22. 06-17 15:03:23.321 28254-28254/com.base.httppost W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  23. 06-17 15:03:23.376 28254-28288/com.base.httppost D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
  24. 06-17 15:03:23.437 28254-28288/com.base.httppost I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: (Ifd751822f5)
yrwegjxp

yrwegjxp1#

错误信息非常清楚:“未知协议”。协议是url中的主要部分。尝试将url对象创建行替换为:

  1. URL url = new URL("https://192.168.9.4:401/db/query");

相关问题