【发布时间】:2020-10-07 01:45:06
【问题描述】:
我正在尝试实现基于 curl 的代码,该代码通过 Android 应用程序与基于 RQLite 的服务器进行通信。一般来说,命令与服务器进行 SQL 查询/执行以返回 JSON 数据/响应。
卷曲命令: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。
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)
【问题讨论】:
-
你的 logcat 中没有收到 NetworkOnMainThread 异常吗?
-
没有这样的
-
你能把你的logcat贴一次吗?
-
您是否尝试从服务器获取数据?
-
可以使用 curl 命令,但想在 java 中实现
标签: java android sql curl httpurlconnection