0

I try my first steps in Android Studio. I currently try to fetch data from an external website. Now I am in a strange situation. As far as I know, Android Studio has a great debugger, but I get no notice or error for this problem. So I tried to make it step by step and let the script write numbers into a string after each step, which are then outputted. The first code-block is the Script I try to get run. But the output is always 1234 and not 123456. When I delete the lines between 4 and 6 the output is 123456. So I guess there is an error that the Android Studio debugger does not recognize. Does anyone have a clue, what could be wrong here? I doublechecked the variablenames and everything.

        data = data + "2";
        URL url = new URL("http://asdfasdfasdf.net/json.php");
        data = data + "3";
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        data = data + "4";
        InputStream inputStream = httpURLConnection.getInputStream();
        data = data + "5";
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        data = data + "6";

This code returns 1234 in in my emulator.

        data = data + "2";
        URL url = new URL("http://asdfasdfasdf.net/json.php");
        data = data + "3";
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        data = data + "4";
        //InputStream inputStream = httpURLConnection.getInputStream();
        data = data + "5";
        //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        data = data + "6";

This code returns 123456 in my emulator.

This is the try/catch-block:

protected Void doInBackground(Void... voids) {

    try {
        data = data + "2";
        URL url = new URL("http://asdfasdfasdf.net/json.php");
        data = data + "3";
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        data = data + "4";
        InputStream inputStream = httpURLConnection.getInputStream();
        data = data + "5";
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        data = data + "6";
        /*
        String line = "";
        while(line != null) {
            line = bufferedReader.readLine();
            data = data + line;
        }
        */

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

This is what LogCat returns

2019-01-06 17:15:44.868 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err: java.io.IOException: Cleartext HTTP traffic to asdfasdfasdf.net not permitted
2019-01-06 17:15:44.868 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
2019-01-06 17:15:44.869 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
2019-01-06 17:15:44.869 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
2019-01-06 17:15:44.869 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
2019-01-06 17:15:44.869 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at eu.asdfasdfasdf.www.cst3.fetchData.doInBackground(fetchData.java:26)
2019-01-06 17:15:44.869 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at eu.asdfasdfasdf.www.cst3.fetchData.doInBackground(fetchData.java:14)
2019-01-06 17:15:44.870 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
2019-01-06 17:15:44.870 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2019-01-06 17:15:44.871 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2019-01-06 17:15:44.871 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2019-01-06 17:15:44.871 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2019-01-06 17:15:44.871 19419-19442/eu.asdfasdfasdf.www.cst3 W/System.err:     at java.lang.Thread.run(Thread.java:764)
Bernhard
  • 1,852
  • 11
  • 19
  • "I get no notice or error for this problem" -- where is your `try`/`catch` block that is wrapping this code? What is it doing in `catch`? – CommonsWare Jan 06 '19 at 16:07
  • I added the try/catch-block – Bernhard Jan 06 '19 at 16:11
  • Your exception should be showing up in Logcat. – CommonsWare Jan 06 '19 at 16:13
  • Thanks for the hint. So I get "java.io.IOException: Cleartext HTTP traffic to asdfasdfasdf.net not permitted". And I think this answer should help me https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted I will try it. Thank you! – Bernhard Jan 06 '19 at 16:19
  • Switch from `http` to `https`. – CommonsWare Jan 06 '19 at 16:20
  • 1
    @Bernhard The exception that the Logcat shows, tells you that your application is not authorized to communicate in simple HTTP, as a safety mechanism. You can enable this via adding a permission in the AndroidManifest.xml – Daniel B. Jan 06 '19 at 16:20
  • Ok, the solution was to add "android:usesCleartextTraffic="true"" in the AndroidManifest.xml within the -block. It works now! Of course I will try to get it set up with https in the future! Thank you for your help! – Bernhard Jan 06 '19 at 16:25

0 Answers0