0

I'm about to code an Android app (using A.Studio 3.5.1) that should connect to a back-end using https. I'm quite new to the techniques so I looked at https://developer.android.com/training/articles/security-ssl.html#HttpsExample

I has four lines of code:

URL url = new URL("https://wikipedia.org");
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
copyInputStreamToOutputStream(in, System.out);

When I try it I get a red text;

new URL gives MalformedURLException url.openConnection() gives IOException,

urlConnection.getInputStream() also

copyInputStreamToOutputStream is not found.

I have read about copyInputStreamToOutputStream, that it can be solved

copyInputStreamToOutputStream(in, System.out)

and

Easy way to write contents of a Java InputStream to an OutputStream

I tried the first way, using apache commons, with no success.

I'm mostly curious about the exceptions.

I will try another walk-through on

https://codelabs.developers.google.com/codelabs/android-network-security-config/

But it would be nice to learn about this...

MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
Valter Ekholm
  • 173
  • 2
  • 17

1 Answers1

0

I have faced similar situations many times. This occur because the webpage you are trying to reach doesn't return anything as a response after connection is established. So the parsing returns IOException as there is nothing to read in the response of URL after connection. newURL is returning MalformedURLException because your url contains -- " -- which should be escaped as these are special characters. Hope this helps.