Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.samir;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.app.Activity;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- public class Utils {
- public static String getJSONString(String url) {
- String jsonString = null;
- HttpURLConnection linkConnection = null;
- try {
- URL linkurl = new URL(url);
- linkConnection = (HttpURLConnection) linkurl.openConnection();
- int responseCode = linkConnection.getResponseCode();
- if (responseCode == HttpURLConnection.HTTP_OK) {
- InputStream linkinStream = linkConnection.getInputStream();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int j = 0;
- while ((j = linkinStream.read()) != -1) {
- baos.write(j);
- }
- byte[] data = baos.toByteArray();
- jsonString = new String(data);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (linkConnection != null) {
- linkConnection.disconnect();
- }
- }
- return jsonString;
- }
- public static void CopyStream(InputStream is, OutputStream os)
- {
- final int buffer_size=1024;
- try
- {
- byte[] bytes=new byte[buffer_size];
- for(;;)
- {
- int count=is.read(bytes, 0, buffer_size);
- if(count==-1)
- break;
- os.write(bytes, 0, count);
- }
- }
- catch(Exception ex){}
- }
- public static boolean isNetworkAvailable(Activity activity) {
- ConnectivityManager connectivity = (ConnectivityManager) activity
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- if (connectivity == null) {
- return false;
- } else {
- NetworkInfo[] info = connectivity.getAllNetworkInfo();
- if (info != null) {
- for (int i = 0; i < info.length; i++) {
- if (info[i].getState() == NetworkInfo.State.CONNECTED) {
- return true;
- }
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement