要有这个权限:
cpp展开代码<uses-permission android:name="android.permission.INTERNET" />
从 Android 9(API 级别 28)开始,默认情况下不支持通过 HTTP 访问网络,而要求使用 HTTPS。这是出于安全考虑,因为 HTTPS 可以提供数据传输的加密和身份验证。因此,如果你的应用目标设备的 Android 版本为 9 或更高,确实需要使用 HTTPS 进行网络请求。
为了解决这个问题,你有几个选项:
使用 HTTPS:将你的服务器配置为支持 HTTPS,并相应地更新 Android 代码中的基础 URL,将 http 修改为 https。
在 AndroidManifest.xml 中添加网络安全配置:如果你的服务器尚未配置 HTTPS,你可以在 AndroidManifest.xml 中添加网络安全配置,允许应用访问不安全的 HTTP 资源。这样做会降低安全性,因此不建议在生产环境中使用。配置如下:
xml展开代码<application
    android:usesCleartextTraffic="true"
    ... >
    ...
</application>
xml展开代码<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your.insecure.domain</domain>
    </domain-config>
</network-security-config>
然后在 AndroidManifest.xml 中引用这个文件:
xml展开代码<application
    android:networkSecurityConfig="@xml/network_security_config"
    ... >
    ...
</application>
使用 HTTPS 是推荐的做法,因为它提供了更好的安全性。

gradle加上:
cpp展开代码    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
实现请求:
cpp展开代码    private static final String BASE_URL = "http://120.46.128.4:8001/";
    interface ApiService {
        @POST("check_in")
        Call<ResponseBody> checkIn(@Body RequestBody body);
    }
    private void checkIn(String name, String studentID) throws JSONException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService = retrofit.create(ApiService.class);
        JSONObject json = new JSONObject();
        json.put("name", name);
        json.put("student_id", studentID);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json.toString());
        Call<ResponseBody> call = apiService.checkIn(body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        JSONObject jsonResponse = new JSONObject(response.body().string());
                        String message = jsonResponse.getString("message");
                        runOnUiThread(() -> Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show());
                    } catch (Exception e) {
                        e.printStackTrace();
                        runOnUiThread(() -> Toast.makeText(MainActivity.this, "响应解析失败", Toast.LENGTH_SHORT).show());
                    }
                } else {
                    runOnUiThread(() -> Toast.makeText(MainActivity.this, "签到失败", Toast.LENGTH_SHORT).show());
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                runOnUiThread(() -> Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show());
            }
        });
    }
python是这么请求:
cpp展开代码import requests
# 接口地址
url = "http://120.46.128.4:8001/insert_student_record"  # 根据实际的地址进行修改
# 要插入的学生记录
data = {
    "name": "张三",
    "student_id": "123456xx"
}
# 发送 POST 请求
response = requests.post(url, json=data)
# 输出响应结果
print(response.json())
你如果需要帮助,请看这里:
csharp展开代码https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2


本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!