Skip to main content

Android Networking

Android Networking


  • Introduction to android networking in Hindi 
  • Networks status in android in Hindi 
  • HTTP request handling with android networking in Hindi 
  • Example 

Introduction to Android Networking

Mobile phones दिनों दिन computer ही बनते जा रहे है। Android phones में networking की वो क्षमता होती है जो किसी computer में पाई जाती है। जैसे की  emails, web surfing और watching online videos आदि। ये सभी services android बेहतर networking के द्वारा provide करता है। आप भी यदि user को web के द्वारा services provide करना चाहते है तो अपनी application में networking implement कर सकते है। 

Android में networking 3 तरह से implement की जाती है। 
  • IP (Internet Protocol) 
  • WiFi 
  • Bluetooth 

Android आपको java.net और org.apache.http-client packages provide करता है। इन packages के द्वारा आप अपनी application में basic networking implement कर सकते है। यदि आप detail में networking को implement करना चाहते है तो android.net package use  कर सकते है। ये उन applications के लिए होता है जो पूरी तरह से web based होती है।             

Network Status

आपका device network से connected है या नहीं ये check करने के लिए आप ConnectivityManager class को use  करते है। इसके लिए आप सबसे पहले ConnectivityManager class का object create करते है। इसके बाद आप current object पर getSystemService() method call करेंगे। इस method में आप CONNECTIVITY_SERVICE context pass करते है। ये method एक object return करता है जिसे convert करके ConnectivityManager class के object को assign किया जाता है। इसके बाद आप इस object पर isConnected() method call करके ये पता लगा सकते है की आपका device किसी network से connected है या नहीं। ये method true और false return करता है। 


ConnectivityManager obj;

obj = (ConnectivityManager) this.getSystemService(context.CONNECTIVITY_SERVICE);

if(obj.isConnected())
{
   Toast t1 = new Toast();
   t1.makeText(context, "you are connected to internet",Toast.LENGTH_SHORT);
   t1.show();
}
else
   Toast t2 = new Toast();
   t2.makeText(context, "you are not connected to internet",Toast.LENGTH_SHORT);
   t2.show();
}




Handling HTTP Requests    

आपका device network से connected है या नहीं ये पता लगाने के बाद आप कुछ operations perform कर सकते है। Server को HTTP requests भेजना और HTTP response पाना सबसे basic networking operations होते है। जब आप कोई URL को open करने के लिए request करते है तो ये ही operations perform होते है।

HTTP operations perform करने के लिए android आपको URL और httpURLConnection classes provide करता है। सबसे पहले आप URL class का object create करते है। इस object में आप एक valid URL pass करते है। इसके बाद आप इस object पर openConnection() method call करते है। इसके result को आप httpURLConnection class के reference variable को assign करते है। इसके बाद httpURLConnection class के उस object पर connect() method call किया जाता है।

इसके बाद आप httpURLConnection class के object पर getInputStream() method call करते है। और इसे आप InputStreamReader class के object को pass करते है। और InputStreamReader class के object को BufferReader class के object को pass किया जाता है। इसके बाद आप BufferReader class के object पर readLine() method call करके data read करते है। ऐसा आप किसी loop के साथ करते है। इसके बाद आप BufferReader class के object पर close() method call करके input stream close करते है। और httpURLConnection class के object पर disconnect() method call करके connection को disconnect करते है। सबसे आखिर में आप result को return करते है। इसका उदाहरण नीचे दिया जा रहा है। एक बात आपको हमेशा याद रखनी चाहिए की ये सभी काम आप Activity class के getHttpResponse() method में करते है।


String result; 

URL myurl = new URL("http://www.google.com");

httpURLConnection obj = (httpURLConnection) myurl.openConnection();

obj.connect();

InputStreamReader  isr = new InputStreamReader(obj.getInputStream);

BufferReader  br = new BufferReader(isr);

String input;
int count=0;

while(input = (br.readLine() != null))  // reading data 
{
    result = input;
    count++;
}

br.close();

obj.disconnect();

return result;

Comments

Popular posts from this blog

Android Services

Android Services Introduction to  android services in Hindi  Declaring  services in Hindi Creating a  android service in Hindi Introduction to Android Services इससे पहले की आप जाने की service क्या होती है आइये जानते है की service क्या नहीं होती है - एक service separate प्रोसेस नहीं होती है। ये आपकी एप्लीकेशन का ही पार्ट होती है।  एक service thread नहीं होती है।  आइये अब देखते है service क्या होती है - Service एक facility होती है जिससे आप android system को बता सकते है की आप कोई काम background में करते रहना चाहते है।  Service एक facility होती है जिससे एक एप्लीकेशन अपनी कुछ functionality दूसरी applications को expose कर सकती है।    हर service क्लास का <service> declaration AndroidManifest.xml file में होना चाहिए। Services को आप context.startService() और context.bindService() मेथड्स से स्टार्ट कर सकते है।            जब service component क्रिएट होता है तो android syst...

Android Activity

Android Activity Activity life cycle in Hindi Starting an  android activity in Hindi Creating a new activity in Hindi Destroying an  android activity in Hindi Introduction to Activity आप की application में एक से ज्यादा activity हो सकती है। जब आप इन activities के through navigate करते है, यानि एक activity से दूसरी activity में जाते है तो activity अपनी life cycle की different stages में जाती है। उदाहरण के लिए जब आपकी activity start होती है तो ये foreground में होती है और user का पूरा focus इस पर होता है। इस process के दौरान  android  system कुछ life cycle methods को call करता है। यदि user कोई दूसरी activity start करता है तो android system उस activity के life cycle methods को call करता है और आपकी पहले वाली activity background में चली जाती है। life cycle methods में आप decide कर सकते है की जब user जाता है या वापस आता है तो आपकी activity कैसे behave करेगी।  उदाहरण के लिए यदि आप एक वीडियो player बना रहे है तो जब user किसी दूसरी activity पर जाये तो...

Android Spinners

Android Spinners  Introduction to  android spinners in Hindi Defining  android spinners in Hindi Creating spinner items array in Hindi Responding to spinner selections in Hindi    Introduction to Android Spinners Android spinners आपकी एप्लीकेशन में एक drop down menu होती है। इस drop down menu में बहुत से items होते है जिन्हे user choices कहते है। जब user spinner पर क्लिक करता है तो ये items शो होते है। यूज़र अपने लिए appropriate choice choose करता है। By default spinner में सबसे top वाला element शो होता है। Spinners को XML layout file में क्रिएट किया जाता है। और items की लिस्ट string resource file में क्रिएट की जाती है। जिसमे आप एक string array क्रिएट करते है।  Spinner और items को जोड़ने के लिए एक array adapter क्रिएट किया जाता है। इसके बाद selection events को respond करने के लिए OnItemSelectedListener interface को  इम्प्लीमेंट किया जाता है।  Defining Android Spinners Android spinners को आप XML l...