简体   繁体   English

(Android) 使用HttpUrlConnection写一个curl POST命令

[英](Android) Using HttpUrlConnection to write a curl POST command

Im trying to implement a curl based Code which communicates with an RQLite based Server through Android Application.我试图实现一个基于 curl 的代码,它通过 Android 应用程序与基于 RQLite 的服务器通信。 In General, the Command talks with server for a SQL query/execute for in return of JSON data/response.通常,该命令与服务器进行对话以获取 SQL 查询/执行以返回 JSON 数据/响应。

Curl Command: curl -XPOST -k 'https://192.168.9.4:401/db/query?pretty' -H "Content-Type:application/json" -d '[" SELECT * FROM ABCTable "]' Curl Command: curl -XPOST -k 'https://192.168.9.4:401/db/query?pretty' -H "Content-Type:application/json" -d '[" SELECT * FROM ABCTable "]'

url in above command is 'https://192.168.9.4:401/db/query?pretty'上面命令中的 url 是'https://192.168.9.4:401/db/query?pretty'

data: [" SELECT * FROM ABCTable "]数据: [" SELECT * FROM ABCTable "]

Java Code for Android: Java Android 的代码:

 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();
            }
        }

    }

The problem is I'm not getting any kind of response/error from the Code.问题是我没有从代码中得到任何类型的响应/错误。 Could anyone point me anything that I'm missing form Docs or in general mistakes?谁能指出我在文档中遗漏的任何内容或一般错误?

Note: I've seen many examples on the Internet but most of them are based on sending JSON-based data in queries but I have to send a simple data string that gives a response.注意:我在 Internet 上看到了很多示例,但大多数示例都是基于在查询中发送基于 JSON 的数据,但我必须发送一个给出响应的简单数据字符串。 The connection is HTTPS based connection so hence HttpsUrlConnection.该连接是基于 HTTPS 的连接,因此是 HttpsUrlConnection。

Logcat:日志猫:

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)

The error message is clear enough: "unknown protocol".错误消息很清楚:“未知协议”。 The protocol is the leading portion in the URL.该协议是 URL 中的主要部分。 Try replacing the URL object creation line with:尝试将 URL object 创建行替换为:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM