MultipartUploadRequest 無法從Android傳遞信息到服務器的一種情況

MultipartUploadRequest 的標準代碼大概是這樣寫的:

String UPLOAD_URL = "http://XX.XX.XX.XX/AndroidUpload/upload.php";

new MultipartUploadRequest(this, uploadId, UPLOAD_URL)
                    .addFileToUpload(path, "image") // 一張圖片
                    .addParameter("name", name) // 一個text
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .setMethod("POST")
                    .startUpload(); 

這時候注意到圖片和text都沒有上傳上去,一時間無法判斷是什麼問題。

通過閱讀官網代碼發現屬性setDelegate可以監控MultipartUploadRequest 的傳輸過程,於是設置屬性setDelegate進行排查:

String UPLOAD_URL = "http://XX.XX.XX.XX/AndroidUpload/upload.php";

new MultipartUploadRequest(this, uploadId, UPLOAD_URL)
                    .addFileToUpload(path, "image") // 一張圖片
                    .addParameter("name", name) // 一個text
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .setMethod("POST")
                    .setDelegate(new UploadStatusDelegate() {
                        @Override
                        public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse,
                                            Exception exception) {
                            Toast.makeText(context, "信息傳遞錯誤", Toast.LENGTH_SHORT).show();
                            exception.printStackTrace(); // 輸出錯誤信息
                        }

                        @Override
                        public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
                            Toast.makeText(context, "信息傳遞完成", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancelled(Context context, UploadInfo uploadInfo) {
                            Toast.makeText(context, "信息傳遞取消", Toast.LENGTH_SHORT).show();
                        }
                    }).startUpload(); 

重新允許程序,在run輸出區中,可以看到一個錯誤信息:

Cleartext HTTP traffic to XX not permitted

這個錯誤出現的原因是,在Android 9及更新的安卓版本,明確規定禁止通過http協議傳輸信息,應該通過https進行傳輸。

知道了症狀就好解決了,這裏提供兩種解決辦法:

1、允許http協議傳輸
首先打開Android項目,在res下新建一個Directory,命名爲xml,在xml中新建一個network_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

然後轉到AndroidManifest.xml,加入屬性android:networkSecurityConfig="@xml/network_config"
如下:

<application
        android:icon="@mipmap/ic_launcher"
        android:networkSecurityConfig="@xml/network_config" <!--這一行-->
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning">

重新運行程序即可。

2、在服務器部署https
參考這兩篇博客完成:(1)wampserver3.0.6部署https、(2)wamp3 本地配置https

發佈了213 篇原創文章 · 獲贊 861 · 訪問量 125萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章