无法在 android 中通过 OkHttp 发送 post 请求

Not able to send post request through OkHttp in android

我正在尝试向我的服务器发送多部分表单 post 请求。我正在发送图像文件,但收到以下文件

W/System.err: java.net.UnknownServiceException: CLEARTEXT communication to 35.226.157.128 not permitted by network security policy
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:176)
        at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:233)
        at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:107)
        at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:75)
        at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:245)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:74)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:100)
        at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:197)
        at okhttp3.internal.connection.RealCall.execute(RealCall.kt:148)
        at com.example.plantai.CropImage.run(CropImage.java:228)
        at java.lang.Thread.run(Thread.java:764)

发送请求代码如下

public void scanClick(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                isConnected();
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                Log.d("req" , "Resquesting ...");
                //MediaType mediaType = MediaType.parse("text/plain");

                RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                        .addFormDataPart("photo",Environment.getExternalStorageDirectory()
                                        +"/Android/PlantAi/crop.jpg",
                                RequestBody.create(MediaType.parse("application/octet-stream"),
                                        new File(Environment.getExternalStorageDirectory()
                                                +"/Android/PlantAi/crop.jpg")))
                        .build();
                Log.d("req" , "Body created");


                Request request = new Request.Builder()
                        .url("http://35.226.157.128:80/PlantAi/upload-manager.php")
                        .method("POST", body)
                        .addHeader("Content-Transfer-Encoding", "multipart/form-data")
                        .build();
                Log.d("req" , "Request Build.");


                try {
                    Log.d("req" , "Sending request");

                    Response response = client.newCall(request).execute();
                    System.out.println("Response ====>  " + response);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }).start();

    }

根据我添加

res/xml/network_security_config.xml

android:usesCleartextTraffic="true"

在 AndroidManifest 中,但它不工作。使用“https://”而不是“http://”对我的服务器不起作用,它会给出类似

的错误

W/System.err: javax.net.ssl.SSLHandshakeException: Handshake failed

我做错了什么?

您忘记在 network_security_config.xml.

中添加内容

res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config xmlns:android="http://schemas.android.com/apk/res/android">
    <base-config cleartextTrafficPermitted="false" />
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">35.226.157.128</domain>
    </domain-config>
</network-security-config>

AndroidManifest.xml

<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/AppTheme"
  android:networkSecurityConfig="@xml/network_security_config">

  ...

</application>