I have a problem trying to read a JSON by OpenHttpConnection on a Public IP.
I tried the JSON link and it is perfect, but when I try to read the JSON I have the following message by Log:
D / NetWorkSecurityConfig: No NetWork Security Config specified, using platform dafault
My code is as follows:
public class Koneksi {
public String call(String url) {
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(url);
} catch (IOException e) {
e.printStackTrace();
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer)) > 0) {
String readString = String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return "";
}
return str;
}
private InputStream OpenHttpConnection(String url) throws IOException {
InputStream in = null;
int response = -1;
URL url1 = new URL(url);
URLConnection conn = url1.openConnection();
//if (!(conn instanceof HttpURLConnection))
// throw new IOException("Not An Http Connection");
//Log.d("ERROR: ", ": ERROR DE HTTP CONNECTION");
try {
HttpURLConnection httpconn = (HttpURLConnection) conn;
httpconn.setAllowUserInteraction(false);
httpconn.setInstanceFollowRedirects(true);
httpconn.setRequestMethod("GET");
httpconn.connect();
response = httpconn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpconn.getInputStream();
}
} catch (Exception e) {
Log.d("ERROR: ", e.getMessage() + ": ERROR DE CONEXION");
throw new IOException("Error connecting2");
}
return in;
}
}
Another Class
public class Biodata extends Koneksi {
String URL = "http://MIIPPUBLICA:8000/Android/server.php";
String url = "";
String response = "";
public String vista() {
try {
url = URL + "?operasi=view";
System.out.println("URL Tampil Biodata: " + url);
response = call(url);
} catch (Exception e) {
Log.d("ERROR APP: ", e.getMessage() + ": MENSAJE PREDETERMINADO DE ERROR");
}
return response;
}
public String insertar(String nama, String alamat) {
try {
url = URL + "?operasi=insert&nama=" + nama + "&alamat=" + alamat;
System.out.println("URL Insert Biodata : " + url);
response = call(url);
} catch (Exception e) {
Log.d("ERROR APP: ", e.getMessage() + ": MENSAJE PREDETERMINADO DE ERROR");
}
return response;
}
public String getData(int id) {
try {
url = URL + "?operasi=get_biodata_by_id&id=" + id;
System.out.println("URL Insert Biodata : " + url);
response = call(url);
} catch (Exception e) {
Log.d("ERROR APP: ", e.getMessage() + ": MENSAJE PREDETERMINADO DE ERROR");
}
return response;
}
public String actualizar(String id, String nama, String alamat) {
try {
url = URL + "?operasi=update&id=" + id + "&nama=" + nama + "&alamat=" + alamat;
System.out.println("URL Insert Biodata : " + url);
response = call(url);
} catch (Exception e) {
}
return response;
}
public String borrar(int id) {
try {
url = URL + "?operasi=delete&id=" + id;
System.out.println("URL Insert Biodata : " + url);
response = call(url);
} catch (Exception e) {
Log.d("ERROR APP: ", e.getMessage() + ": MENSAJE PREDETERMINADO DE ERROR");
}
return response;
}
}
My AdroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tecnologia.crud_android">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>