im new in android development, i have problem with my program. my android program get data from API HTTP client. if i set targetSdkVersion=23
my program work, but if i set targetSdkVersion=30
it doesnt work
here my androidManifest.xml, connection to API, and helper for 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>
Connection to 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";
}
My Request Handler
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();
}
}
Maybe i missing something because the different of api android level
If you target android SDK above 28 and getting issue with Api calls then
Add this line to your manifest
android:usesCleartextTraffic="true"
eg
<application
android:name=".MyApplication"
android:allowBackup="false"
android:theme="@style/MyMaterialTheme"
android:usesCleartextTraffic="true"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.