我的网络服务无法连接到 android api 30

Posted

技术标签:

【中文标题】我的网络服务无法连接到 android api 30【英文标题】:My Web Service cannot connect to android api 30 【发布时间】:2022-01-19 08:03:12 【问题描述】:

我是 android 开发的新手,我的程序有问题。我的 android 程序从 API HTTP 客户端获取数据。如果我设置targetSdkVersion=23 我的程序工作,但如果我设置targetSdkVersion=30 它不起作用

这里是我的 androidManifest.xml、API 连接和 API 助手 androidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.abb.akda_extra">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


        <application
        android:allowBackup="true"
        android:icon="@drawable/logo_akda_fix"
        android:debuggable="false"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:ignore="HardcodedDebugMode">
        <uses-library android:name="org.Apache.http.legacy" android:required="false"/>
        <activity android:name=".cek_kepesertaan"></activity>
        <activity android:name=".aktivasi_kartu" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

与 API 的连接

package com.abb.akda_extra;

public class konfigurasi 
    //Alamat tujuan API
    /*public static final String URL_ADD = "http://192.168.1.227/smsdevelopment/api/tambahPeserta.php";
    public static final String URL_GET = "http://192.168.1.227/smsdevelopment/api/cekPeserta.php?nopin=";
    public static final String URL_GET_KARTU = "http://192.168.1.227/smsdevelopment/api/cekKartu.php?nopin=";
    public static final String URL_GET_STATUS = "http://192.168.1.227/smsdevelopment/api/cekKepesertaan.php?noktp=";*/

    public static final String URL_ADD = "http://apisms.abb.co.id/tambahPeserta.php";
    public static final String URL_GET = "http://apisms.abb.co.id/cekPeserta.php?nopin=";
    public static final String URL_GET_KARTU = "http://apisms.abb.co.id/cekKartu.php?nopin=";
    public static final String URL_GET_STATUS = "http://apisms.abb.co.id/cekKepesertaan.php?noktp=";

    //Kunci untuk mengirim permintaan ke script php dan untuk POST Request
    public static final String KEY_PST_ID = "id";
    public static final String KEY_PST_NOPIN = "nopin";
    public static final String KEY_PST_NAMA = "nama";
    public static final String KEY_PST_TANGGAL = "tanggal";
    public static final String KEY_PST_ALAMAT = "alamat";
    public static final String KEY_PST_NOMORHP = "nomorhp";
    public static final String KEY_PST_NIK = "noktp";
    public static final String KEY_PST_NOREG = "noreg";
    public static final String KEY_PST_TGLREG = "tglregister";
    public static final String KEY_PST_TGLAKTIF = "tglaktif";
    public static final String KEY_PST_TGLEXPIRED = "tglexpired";


    //JSON TAGS untuk GET Request
    public static final String TAG_JSON_ARRAY = "result";
    public static final String TAG_ID = "id";
    public static final String TAG_NOREG = "noreg";
    public static final String TAG_NOPIN = "nopin";
    public static final String TAG_NAMA = "nama";
    public static final String TAG_TANGGAL_LAHIR = "tgllahir";
    public static final String TAG_ALAMAT = "alamat";
    public static final String TAG_NOMORHP = "nomorhp";
    public static final String TAG_NIK = "nonik";
    public static final String TAG_TANGGAL_REGISTER = "tglregister";
    public static final String TAG_TANGGAL_AKTIF="tglaktif";
    public static final String TAG_TANGGAL_EXPIRED="tglexpired";

    //Noreg dan Nopin untuk cek kepesertaan
    public static final String PST_NOPIN = "pst_nopin";
    public static final String PST_NOREG = "pst_noreg";

我的请求处理程序

package com.abb.akda_extra;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

public class RequestHandler 
    public String sendPostRequest(String requestURL,HashMap<String,String> postDataParams)
        URL url;

        StringBuilder sb = new StringBuilder();
        try
            url = new URL(requestURL);

            //Membuat Koneksi HttpURLConnection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

            //Konfigurasi koneksi
            conn.setReadTimeout(5000);
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            //Membuat keluaran Stream
            OutputStream os = conn.getOutputStream();

            //Menulis parameter permintaan
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
            writer.write(getPostDataString(postDataParams));

            writer.flush();
            writer.close();
            os.close();
            int responseCode = conn.getResponseCode();

            if (responseCode == HttpsURLConnection.HTTP_OK)
            
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                sb = new StringBuilder();
                String response;
                //Reading server response
                while ((response = br.readLine()) != null)
                    sb.append(response);
                
            
        catch (Exception e)
            e.printStackTrace();
        
        return sb.toString();
    

    public String sendGetRequest(String requestURL)
        StringBuilder sb =new StringBuilder();
        try 
            URL url = new URL(requestURL);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            //HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String s;
            while((s=bufferedReader.readLine())!=null)
                sb.append(s+"\n");
            
        catch(Exception e)
        
        return sb.toString();
    

    public String sendGetRequestParam(String requestURL,String nopin,String noreg)
        StringBuilder sb =new StringBuilder();
        try 
            URL url = new URL(requestURL+nopin+"&noreg="+noreg);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            //HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String s;
            while((s=bufferedReader.readLine())!=null)
                sb.append(s+"\n");
            
        catch(Exception e)
            e.printStackTrace();
        
        return sb.toString();
    

    public String sendGetRequestKartu(String requestURL,String nopin)
        StringBuilder sb =new StringBuilder();
        try 
            URL url = new URL(requestURL+nopin);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            //HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String s;
            while((s=bufferedReader.readLine())!=null)
                sb.append(s+"\n");
            
        catch(Exception e)
            e.printStackTrace();
        
        return sb.toString();
    
    public String sendGetRequestStatus(String requestURL,String noktp)
        StringBuilder sb =new StringBuilder();
        try 
            URL url = new URL(requestURL+noktp);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            //HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

            String s;
            while((s=bufferedReader.readLine())!=null)
                sb.append(s+"\n");
            
        catch(Exception e)
            e.printStackTrace();
        
        return sb.toString();
    

    private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException 
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for (Map.Entry<String, String> entry : params.entrySet()) 
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        

        return result.toString();
    

也许我错过了什么,因为 api android 级别不同

【问题讨论】:

android:usesCleartextTraffic="true" 你能把它添加到清单文件中的 【参考方案1】:

如果你的目标是 28 以上的 android SDK 并遇到 Api 调用问题,那么

将此行添加到您的清单中

android:usesCleartextTraffic="true"

e-g​​

 <application
    android:name=".MyApplication"
    android:allowBackup="false"
    android:theme="@style/MyMaterialTheme"
    android:usesCleartextTraffic="true"

【讨论】:

以上是关于我的网络服务无法连接到 android api 30的主要内容,如果未能解决你的问题,请参考以下文章

Tensorflow android demo出错了。无法连接到相机服务

无法连接到 MariaDB

从 android studio API 28 连接到解析服务器的 i/o 失败

android:无法在 targetSdkVersion 更改时连接到相机服务

Docker compose api无法连接到主机MongoDB数据库

如何修复 Android 模拟器中的“无法连接到相机服务”异常