Android HttpsUrlConnection

Hi *,

does anyone know why my code runs as Java code, but not for Android. I try to get a simple reader to run but the only effort i got is in Java. No luck with Android…

import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.Certificate;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;


 
 
public class HttpsClient{
 
   public static void main(String[] args)
   {
        new HttpsClient().getTheContent();
   }
 
   private void getTheContent(){
 
      String https_url = "https://api.spark.io/v1/devices/MYCOREID/events/?access_token=MYTOKEN";
      URL url;
      try {
 
	     url = new URL(https_url);
	     HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
 

	     //dump all the content
	     print_content(con);
 
      } catch (MalformedURLException e) {
	     e.printStackTrace();
      } catch (IOException e) {
	     e.printStackTrace();
      }
 
   }
 

 
   private void print_content(HttpsURLConnection con){
	if(con!=null){
 
	try {
 
	   System.out.println("****** Spark Core - Result ********");			
	   BufferedReader br = 
		new BufferedReader(
			new InputStreamReader(con.getInputStream()));
 
	   String input;
 
	   while ((input = br.readLine()) != null){
	      System.out.println(input);
		   
	   }
	   br.close();
 
	} catch (IOException e) {
	   e.printStackTrace();
	}
 
       }
 
   }
 
}

When I try to use that in Android Studio I got an “Error: illegal start of expression” and “Error: ; expected” for the lines: private void testIt() { … }, private void print_https_cert(HttpsURLConnection con){ … } and private void print_content(HttpsURLConnection con){ … }.

Please do have merci with my Java skills, absolute new in that but willing to learn.

I’m going to take a stab in the dark and suggest converting your private methods to public as a test. Perhaps the way you are accessing your private members is causing the error?

1 Like

Hi @BDub

Thanks for you help, but that was not the problem. I just managed it (after fiddling around a day) to get a class up and running to catch the stream.

Note to myself: Run StreamReader in background :wink: with AsyncTask. If I manage it to get only one complete output than I solemnly swear I put this into this thread! But for now I´am glad to get it to this point!