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

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

我正在尝试实现一个基于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代码:

private void SendRequest() {
        HttpsURLConnection httpsURLConnection = null;
        try {
            URL url = new URL("192.168.9.4:401/db/query");
            httpsURLConnection = (HttpsURLConnection) (url.openConnection());

            String data1 = "[\n\"SELECT * FROM ABCTable\"\n]";
            httpsURLConnection.setDoOutput(true);
            httpsURLConnection.setRequestMethod("POST");
            httpsURLConnection.addRequestProperty("Content-Length", Integer.toString(data1.length()));
            httpsURLConnection.addRequestProperty("Content-Type", "application/json");
            httpsURLConnection.setUseCaches( false );

            int responseCode = httpsURLConnection.getResponseCode();

            if (responseCode == httpsURLConnection.HTTP_OK) {
                BufferedReader data = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                while ((line = data.readLine()) != null) {
                    sb.append(line);
                }

                Log.d("Result",sb.toString());
            }
        } catch (IOException e){
            e.printStackTrace();

        } finally {
            if (httpsURLConnection != null){
                httpsURLConnection.disconnect();
            }
        }

    }

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

06-17 15:03:23.075 28254-28254/? I/art: Late-enabling -Xcheck:jni
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
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
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>
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>
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err: java.net.MalformedURLException: Unknown protocol: user
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at java.net.URL.<init>(URL.java:182)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at java.net.URL.<init>(URL.java:125)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at com.base.httppost.MainActivity.SendRequest(MainActivity.java:30)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at com.base.httppost.MainActivity.onCreate(MainActivity.java:24)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at android.app.Activity.performCreate(Activity.java:6259)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
06-17 15:03:23.320 28254-28254/com.base.httppost W/System.err:     at android.os.Looper.loop(Looper.java:148)
06-17 15:03:23.321 28254-28254/com.base.httppost W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5443)
06-17 15:03:23.321 28254-28254/com.base.httppost W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
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)
06-17 15:03:23.321 28254-28254/com.base.httppost W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-17 15:03:23.376 28254-28288/com.base.httppost D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
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对象创建行替换为:

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

相关问题