我一直在尝试遵循本教程:
https://www.youtube.com/watch?v=SBzfsCFgGdo&list=PLDGYn1fFRgEN1zsbOLRCdapIZlb0Y9Bas&index=3
尝试在模拟器上运行应用程序时遇到以下错误:
2021-07-22 02:14:47.311 442-442/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2021-07-22 02:14:47.311 442-442/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2021-07-22 02:14:49.051 444-444/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2021-07-22 02:14:49.051 444-444/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
互联网上的所有解决方案都围绕着将“android:usesCleartextTraffic="true"”包含到清单文件中,但我已经这样做了,但没有更好的结果。
我的代码:
MainActivity.java:
package com.example.testapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(" http://192.168.1.246:5000/").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Toast.makeText(MainActivity.this, "network not found", Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
TextView textView = findViewById(R.id.textView1);
textView.setText(response.body().string());
}
});
}
}
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestApp"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity"
android:exported = "false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Flask 服务器:
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def hello_world():
print("request")
return 'hello honey'
if __name__ == "__main__":
app.run()
此错误可能由两个原因造成:
首先,将当前的 minSdkVersion 更改为其他版本,最好低于 23。 然后检查你的 logcat 是否在你的代码中给出了一些错误。这很可能是导入错误。
就我而言,我将 minSdkVersion 从 26 更改为 17。 这对我有用。希望它也适合您。