SNetwork.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package net.indivia.hacklabbo.statusreader;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.ProtocolException;
  9. import java.net.URL;
  10. import android.annotation.TargetApi;
  11. import android.net.ConnectivityManager;
  12. import android.net.NetworkInfo;
  13. import android.os.Build;
  14. import android.os.StrictMode;
  15. import android.util.Log;
  16. public class SNetwork {
  17. public static final String LOG_TAG = "SNetwork";
  18. public static boolean isConnected(ConnectivityManager cm) {
  19. boolean connected = false;
  20. if (cm != null) {
  21. NetworkInfo[] netInfo = cm.getAllNetworkInfo();
  22. for (NetworkInfo ni : netInfo) {
  23. Log.d(LOG_TAG, "type:"+ni.getTypeName()+" conn:"+ni.isConnected()
  24. +" avv:"+ni.isAvailable() );
  25. if ((ni.getTypeName().equalsIgnoreCase("WIFI") ||
  26. ni.getTypeName().equalsIgnoreCase("MOBILE"))
  27. && ni.isConnected() && ni.isAvailable()){
  28. connected = true;
  29. break;
  30. }
  31. }
  32. }
  33. return connected;
  34. }
  35. @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  36. public static String GET(String uri){
  37. String result = "";
  38. HttpURLConnection con;
  39. try {
  40. con = (HttpURLConnection) ( new URL(uri)).openConnection();
  41. try {
  42. con.setRequestMethod("GET");
  43. con.setDoOutput(false);
  44. try {
  45. if (android.os.Build.VERSION.SDK_INT > 9) {
  46. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  47. StrictMode.setThreadPolicy(policy);
  48. }
  49. con.connect();
  50. InputStream is = con.getInputStream();
  51. if(is != null)
  52. result = convertInputStreamToString(is);
  53. else
  54. result = "Did not work!";
  55. con.disconnect();
  56. } catch (IOException e) {
  57. Log.d(LOG_TAG,"no json");
  58. result = "Il file json non esiste";
  59. e.printStackTrace();
  60. }
  61. } catch (ProtocolException e1) {
  62. Log.d(LOG_TAG,"protocoll");
  63. e1.printStackTrace();
  64. }
  65. } catch (MalformedURLException e2) {
  66. Log.d(LOG_TAG,"Url non idoneo:"+ uri);
  67. result = "Manca il protocollo";
  68. e2.printStackTrace();
  69. } catch (IOException e2) {
  70. Log.d(LOG_TAG,"IO 2");
  71. e2.printStackTrace();
  72. }
  73. return result;
  74. }
  75. private static String convertInputStreamToString(InputStream inputStream) throws IOException{
  76. String line = "", result = "";
  77. BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
  78. while((line = bufferedReader.readLine()) != null)
  79. result += line;
  80. inputStream.close();
  81. return result;
  82. }
  83. }