英文:
Connection error between Android.java to Servlet.java via HttpURLConnection
问题
以下是您要翻译的内容:
我正在IntelliJ上创建Android项目。现在我正在尝试做的是从MainActivity.java(即Android.java)连接位于Android项目相同包中的servlet.java。Android端的代码如下:
URL url = new URL("http://localhost:8080/AndroidProject/Servlet/");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept","text/plain");
status = connection.getResponseCode();
但是当我运行程序时,在"status = connection.getResponseCode();"之后,在Looper.java中出现了"Source code does not match the bytecode"的错误。同时,当我在没有Android的客户端servlet java上做相同的事情时,它可以正常工作。那么我需要做什么?Android连接和客户端-服务器连接之间的区别是什么?
我检查了build.gradle。API信息如下:
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.androidapp"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
因此,根据这些信息,我在AndroidManifest.xml中添加了以下内容。
但是仍然出现相同的错误。
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="29" />
英文:
I am making Android project on IntelliJ. Now what I'm trying to do is connecting servlet.java which is within the same package of the Android project from MainActivity.java which is Android.java. The code of Android side is below.
URL url = new URL("http://localhost:8080/AndroidProject/Servlet/");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept","text/plain");
status = connection.getResponseCode();
But when I ran the program, After "status = connection.getResponseCode();", "Source code does not match the bytecode" came up at Looper.java. Meanwhile, when I did the same thing at client-servlet java without Android, it worked normally. So what do I need to do? what is the difference between the Android connection and client-server connection?
I checked the following info.<br>
https://stackoverflow.com/questions/39990752/source-code-does-not-match-the-bytecode-when-debugging-on-a-device<br><br>
I checked build.gradle. The API information is
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.androidapp"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
So based on the information, I added the following at AndroidManifest.xml.
But still, the same error happens.
<uses-sdk android:minSdkVersion="29" android:targetSdkVersion="29" />
专注分享java语言的经验与见解,让所有开发者获益!
评论