dimanche 6 septembre 2020

Android reading web content, nothing returned WHY?

i'm trying to read a web content from url saved in SOURCE_URL variable, but nothing returned, everything should coerrect and in place ,So what is the problem?!!!!!! and internet permission is added to android manifest

MainActivity.java


    private static final String SOURCE_URL = "https://www.imdb.com/list/ls052283250/";
    private ImageView img;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Pattern pattern;
    private Matcher matcher;

    private class DownloadTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... strings) {
            String result = "";
            URL url;
            HttpURLConnection httpURLConnection;

            try {
                url = new URL(strings[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();
                InputStream is = httpURLConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(is);
                int data = reader.read();
                Log.v("char", String.valueOf(data));
                while (reader.read() != -1){
                    char current = (char) data;
                    result += current;
                    data = reader.read();
                }
                return result;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = findViewById(R.id.imageView);
        btn1 = findViewById(R.id.button1);
        btn2 = findViewById(R.id.button2);
        btn3 = findViewById(R.id.button3);
        btn4 = findViewById(R.id.button4);

        DownloadTask task = new DownloadTask();
        task.execute(SOURCE_URL);
        try {
            String result = task.get();
            Log.v("Result",result);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

After running the app no content returned

LOG Verbose

09-07 02:11:52.538 5167-5186/com.example.guessthecelebrity D/libc-netbsd: getaddrinfo: www.imdb.com NO result from proxy 
09-07 02:11:52.539 5167-5186/com.example.guessthecelebrity I/System.out: [CDS][DNS]Unable to resolve host "www.imdb.com": No address associated with hostname
09-07 02:11:52.539 5167-5186/com.example.guessthecelebrity W/System.err: java.net.UnknownHostException: Unable to resolve host "www.imdb.com": No address associated with hostname
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] java.lang.String.split(java.lang.String)' on a null object reference




Aucun commentaire:

Enregistrer un commentaire