连接错误,Android.java与Servlet.java之间通过HttpURLConnection。

huangapple 未分类评论44阅读模式
英文:

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连接和客户端-服务器连接之间的区别是什么?

我查看了以下信息。
https://stackoverflow.com/questions/39990752/source-code-does-not-match-the-bytecode-when-debugging-on-a-device

我检查了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(&quot;http://localhost:8080/AndroidProject/Servlet/&quot;);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod(&quot;GET&quot;);
            connection.setRequestProperty(&quot;Accept&quot;,&quot;text/plain&quot;);
            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&lt;br&gt;&lt;br>

I checked build.gradle. The API information is

android {
    compileSdkVersion 29
    buildToolsVersion &quot;29.0.3&quot;
    defaultConfig {
        applicationId &quot;com.example.androidapp&quot;
        minSdkVersion 29
        targetSdkVersion 29
        versionCode 1
        versionName &quot;1.0&quot;
        testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
    }

So based on the information, I added the following at AndroidManifest.xml.
But still, the same error happens.

&lt;uses-sdk android:minSdkVersion=&quot;29&quot; android:targetSdkVersion=&quot;29&quot; /&gt;

huangapple
  • 本文由 发表于 2020年3月16日 02:32:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/60696286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定