This is featured post 1 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 2 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 3 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 4 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.

This is featured post 5 title

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation test link ullamco laboris nisi ut aliquip ex ea commodo consequat.


Custom Search
Showing posts with label Network. Show all posts
Showing posts with label Network. Show all posts

Checking network connection and performing operation in Thread

0 comments

My this post is related to checking network connection  and how to handle if network connection is not available to make our application more responsive. Later on we will learn how to perform time taking operation in separate thread if connection is present.

First of all we two permission if your are checking wifi connection then you need one extra permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
If you want to check connection on click of particular button you can  call this function inside click listener of button

public void ahmadNetworkHanlder() {
 
ConnectivityManager connMgr = (ConnectivityManager)
        getSystemService
(Context.CONNECTIVITY_SERVICE);
   
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
   
if (networkInfo != null && networkInfo.isConnected()) {
       
// fetch data here as network is availble
   
} else {
       
// display error dialog to notify user ,connection is not present
   
}
}
Now consider case, Network is available. Use one asynchronous task to perform network operation like downloading data



public class AhmadActivity extends Activity {
   
private static final String DEBUG_TAG = "AhmadHttp";
   
private EditText urlText;
   
private TextView textView;
   
   
@Override
   
public void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView
(R.layout.main);  
        urlText
= (EditText) findViewById(R.id.myUrl);
        textView
= (TextView) findViewById(R.id.myText);
   
}
// Call this method on Click of A button
  public void ahmadNetworkHandler() {
       
// Gets the URL from the UI's text field.
       
String stringUrl = urlText.getText().toString();
       
ConnectivityManager connMgr = (ConnectivityManager)
            getSystemService
(Context.CONNECTIVITY_SERVICE);
       
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
       
if (networkInfo != null && networkInfo.isConnected()) {
           
new DownloadWebpageText().execute(stringUrl);
       
} else {
            textView
.setText("No network connection available.");
       
}
   
} /* Here we take thread to down load image etc

     
private class DownloadWebpageText extends AsyncTask {
       
@Override
       
protected String doInBackground(String... urls) {
             
           
// params comes from the execute() call: params[0] is the url.
           
try {
               
return downloadUrl(urls[0]);
           
} catch (IOException e) {
               
return "Unable to retrieve web page. URL may be invalid.";
           
}
       
}
       
// onPostExecute displays the results of the AsyncTask.
       
@Override
       
protected void onPostExecute(String result) {
            textView
.setText(result);
       
}
   
}
}
Now we almost complete our task. Just we need to create a function that will handle download from url. So here is the final function


/* we pass the url from asynchronous task */

private String downloadUrl(String myurl) throws IOException {
   
InputStream is = null;
 
  int len = 500;
   
try {
        URL url
= new URL(myurl);
       
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn
.setReadTimeout(10000 /* milliseconds */);
        conn
.setConnectTimeout(15000 /* milliseconds */);
        conn
.setRequestMethod("GET");
        conn
.setDoInput(true);
       
// Starts the query
        conn
.connect();
       
int response = conn.getResponseCode();
       
Log.d(DEBUG_TAG, "The response is: " + response);
       
is = conn.getInputStream();

       
// Convert the InputStream into a string See this Link
       
String contentAsString = readIt(is, len);
       
return contentAsString;
       
   
// Makes sure that the InputStream is closed after the app is
   
// finished using it.
   
} finally {
       
if (is != null) {
           
is.close();
       
}
   
}
}

So its done. Now you have string response you can do anything what your requirement tell you to do. 

converting InputStream to string

0 comments

We are very much aware about InputStream. InputStream is to  read to content from file or URL. If we handle file then we used FileInputStream and while reading from a URL, we used InputStream.


InputStream is a readable source of bytes. Once you get an InputStream it's common to decode or convert it into a target data type. For example, if you were downloading image data, you might decode and display it directly Read this post to know how to convert input stream to bitmap See How to convert InputStream to bitmap. But if we want to convert InputStream to string then we need to read it line by line like given code


// Reads an InputStream and converts it to a String.
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
   
Reader reader = null;
    reader
= new InputStreamReader(stream, "UTF-8");        
   
char[] buffer = new char[len];
    reader
.read(buffer);
   
return new String(buffer);
}


Industry Leaders Announce Open Platform for Mobile Devices

0 comments

Group Pledges to Unleash Innovation for Mobile Users Worldwide

MOUNTAIN VIEW, Calif.; BONN, Germany; TAOYUAN, Taiwan; SAN DIEGO, Calif.; SCHAUMBERG, Ill., November 5, 2007 -- A broad alliance of leading technology and wireless companies today joined forces to announce the development of Android, the first truly open and comprehensive platform for mobile devices. Google Inc., T-Mobile, HTC, Qualcomm, Motorola and others have collaborated on the development of Android through the Open Handset Alliance, a multinational alliance of technology and mobile industry leaders.

This alliance shares a common goal of fostering innovation on mobile devices and giving consumers a far better user experience than much of what is available on today's mobile platforms. By providing developers a new level of openness that enables them to work more collaboratively, Android will accelerate the pace at which new and compelling mobile services are made available to consumers.

With nearly 3 billion users worldwide, the mobile phone has become the most personal and ubiquitous communications device. However, the lack of a collaborative effort has made it a challenge for developers, wireless operators and handset manufacturers to respond as quickly as possible to the ever-changing needs of savvy mobile consumers. Through Android, developers, wireless operators and handset manufacturers will be better positioned to bring to market innovative new products faster and at a much lower cost. The end result will be an unprecedented mobile platform that will enable wireless operators and manufacturers to give their customers better, more personal and more flexible mobile experiences.

Thirty-four companies have formed the Open Handset Alliance, which aims to develop technologies that will significantly lower the cost of developing and distributing mobile devices and services. The Android platform is the first step in this direction -- a fully integrated mobile "software stack" that consists of an operating system, middleware, user-friendly interface and applications. Consumers should expect the first phones based on Android to be available in the second half of 2008.

The Android platform will be made available under one of the most progressive, developer-friendly open-source licenses, which gives mobile operators and device manufacturers significant freedom and flexibility to design products. Next week the Alliance will release an early access software development kit to provide developers with the tools necessary to create innovative and compelling applications for the platform.

Android holds the promise of unprecedented benefits for consumers, developers and manufacturers of mobile services and devices. Handset manufacturers and wireless operators will be free to customize Android in order to bring to market innovative new products faster and at a much lower cost. Developers will have complete access to handset capabilities and tools that will enable them to build more compelling and user-friendly services, bringing the Internet developer model to the mobile space. And consumers worldwide will have access to less expensive mobile devices that feature more compelling services, rich Internet applications and easier-to-use interfaces -- ultimately creating a superior mobile experience.

Open Software, Open Device, Open Ecosystem

"This partnership will help unleash the potential of mobile technology for billions of users around the world. A fresh approach to fostering innovation in the mobile industry will help shape a new computing environment that will change the way people access and share information in the future," said Google Chairman and CEO Eric Schmidt. "Today's announcement is more ambitious than any single 'Google Phone' that the press has been speculating about over the past few weeks. Our vision is that the powerful platform we're unveiling will power thousands of different phone models."

"As a founding member of the Open Handset Alliance, T-Mobile is committed to innovation and fostering an open platform for wireless services to meet the rapidly evolving and emerging needs of wireless customers," said René Obermann, Chief Executive Officer, Deutsche Telekom, parent company of T-Mobile. "Google has been an established partner for T-Mobile's groundbreaking approach to bring the mobile open Internet to the mass market. We see the Android platform as an exciting opportunity to launch robust wireless Internet and Web 2.0 services for T-Mobile customers in the US and Europe in 2008."

"HTC's trademark on the mobile industry has been its ability to drive cutting-edge innovation into a wide variety of mobile devices to create the perfect match for individuals," said Peter Chou, Chief Executive Officer, HTC Corp. "Our participation in the Open Handset Alliance and integration of the Android platform in the second half of 2008 enables us to expand our device portfolio into a new category of connected mobile phones that will change the complexion of the mobile industry and re-create user expectations of the mobile phone experience."

"The convergence of the wireless and Internet industries is creating new partnerships, evolving business models and driving innovation," said Dr. Paul E. Jacobs, Chief Executive Officer of Qualcomm. "We are extremely pleased to be participating in the Open Handset Alliance, whose mission is to help build the leading open-source application platform for 3G networks. The proliferation of open-standards-based handsets will provide an exciting new opportunity to create compelling services and devices. As a result, we are committing research and development resources to enable the Android platform and to create the best always-connected consumer experience on our chipsets."

"Motorola has long been an advocate of open software for mobile platforms. Today, we're excited to continue this support by joining Google and others in the announcement of the Open Handset Alliance and Android platform. Motorola plans to leverage the Android platform to enable seamless, connected services and rich consumer experiences in future Motorola products," said Ed Zander, Chairman and CEO of Motorola, Inc.

Open Handset Alliance Founding Members

Aplix (www.aplixcorp.com), Ascender Corporation (www.ascendercorp.com), Audience (www.audience.com), Broadcom (www.broadcom.com), China Mobile (www.chinamobile.com), eBay (www.ebay.com), Esmertec (www.esmertec.com), Google (www.google.com), HTC (www.htc.com), Intel (www.intel.com), KDDI (www.kddi.com), Living Image (www.livingimage.jp), LG (www.lge.com), Marvell (www.marvell.com), Motorola (www.motorola.com), NMS Communications (www.nmscommunications.com), Noser (www.noser.com), NTT DoCoMo, Inc. (www.nttdocomo.com), Nuance (www.nuance.com), Nvidia (www.nvidia.com), PacketVideo (www.packetvideo.com), Qualcomm (www.qualcomm.com), Samsung (www.samsung.com), SiRF (www.sirf.com), SkyPop (www.skypop.com), SONiVOX (www.sonivoxrocks.com), Sprint Nextel (www.sprint.com), Synaptics (www.synaptics.com), TAT - The Astonishing Tribe (www.tat.se), Telecom Italia (www.telecomitalia.com), Telefónica (www.telefonica.es), Texas Instruments (www.ti.com), T-Mobile (www.t-mobile.com), Wind River (www.windriver.com)

For more information about the Open Handset Alliance, visit the website at www.openhandsetalliance.com.

Verizon Opens Network in 2008

0 comments

Verizon Wireless announced that it plans to open it's wireless network in 2008. It seem like Verizon finally "Gets" it. I'm sure that saw what Google is doing with Android and didn't want to be the odd one out.

In an interview Chief Executive Officer Lowell McAdam says it now makes sense to get behind Android. "We're planning on using Android, Android is an enabler of what we do."

Now the only thing to do is wait. Will Verizon really open their network or will it just be a half assed attempt just to fit in with the rest of the crowd?

We will see soon. Good Luck Verizon.