Read JSON by OpenHttpConnection in a Public IP

0

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>
    
asked by Diego Martinez 13.03.2018 в 16:58
source

2 answers

1

It was a problem of permissions, I was finally able to solve it with this within the onCreate

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    
answered by 18.03.2018 / 15:11
source
-1

Update:

This message can also be shown when a request is made to the Internet but the permission is not defined within AndroidManifest.xml :

   <uses-permission android:name="android.permission.INTERNET"/>

It's not a mistake, it's a debug message:

  

D / NetworkSecurityConfig: No Network Security Config specified, using   platform default

If you can not read the json, the problem should be another, check the LogCat for messages defined with the E/ tag and the "Caused by:" message.

I advise you to define the error messages in the logCat as Errors, using Log.e () instead of Log.d ():

catch (Exception e) {
            //Log.d("ERROR APP: ", e.getMessage() + ": MENSAJE PREDETERMINADO DE ERROR");
            Log.e("ERROR APP: ", e.getMessage() + ": MENSAJE PREDETERMINADO DE ERROR");
        }
    
answered by 13.03.2018 в 18:08