Advertisement
Guest User

Untitled

a guest
Apr 28th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. package com.samir;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8.  
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.net.ConnectivityManager;
  12. import android.net.NetworkInfo;
  13.  
  14. public class Utils {
  15. public static String getJSONString(String url) {
  16. String jsonString = null;
  17. HttpURLConnection linkConnection = null;
  18. try {
  19. URL linkurl = new URL(url);
  20. linkConnection = (HttpURLConnection) linkurl.openConnection();
  21. int responseCode = linkConnection.getResponseCode();
  22. if (responseCode == HttpURLConnection.HTTP_OK) {
  23. InputStream linkinStream = linkConnection.getInputStream();
  24. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  25. int j = 0;
  26. while ((j = linkinStream.read()) != -1) {
  27. baos.write(j);
  28. }
  29. byte[] data = baos.toByteArray();
  30. jsonString = new String(data);
  31. }
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. } finally {
  35. if (linkConnection != null) {
  36. linkConnection.disconnect();
  37. }
  38. }
  39. return jsonString;
  40. }
  41. public static void CopyStream(InputStream is, OutputStream os)
  42. {
  43. final int buffer_size=1024;
  44. try
  45. {
  46. byte[] bytes=new byte[buffer_size];
  47. for(;;)
  48. {
  49. int count=is.read(bytes, 0, buffer_size);
  50. if(count==-1)
  51. break;
  52. os.write(bytes, 0, count);
  53. }
  54. }
  55. catch(Exception ex){}
  56. }
  57.  
  58. public static boolean isNetworkAvailable(Activity activity) {
  59. ConnectivityManager connectivity = (ConnectivityManager) activity
  60. .getSystemService(Context.CONNECTIVITY_SERVICE);
  61. if (connectivity == null) {
  62. return false;
  63. } else {
  64. NetworkInfo[] info = connectivity.getAllNetworkInfo();
  65. if (info != null) {
  66. for (int i = 0; i < info.length; i++) {
  67. if (info[i].getState() == NetworkInfo.State.CONNECTED) {
  68. return true;
  69. }
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement