gpt4 book ai didi

java - (Android) 使用 HttpUrlConnection 编写 curl POST 命令

转载 作者:行者123 更新时间:2023-12-01 16:14:57 26 4
gpt4 key购买 nike

我试图实现一个基于 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();
}
}

}


问题是我没有从代码中得到任何类型的响应/错误。谁能指出我在文档中遗漏的任何内容或一般错误?

注意:我在 Internet 上看到了很多示例,但大多数示例都是基于在查询中发送基于 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)

最佳答案

错误消息很清楚:“未知协议(protocol)”。协议(protocol)是 URL 中的前导部分。尝试将 URL 对象创建行替换为:

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

关于java - (Android) 使用 HttpUrlConnection 编写 curl POST 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62425627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com