title: Android Application that connects to remote server
body: <p>I am trying to build an Android App on Platform 2.2 Froyo. The app is supposed to connect to a remote server, fetch data from it and display to user in a different language.</p>
  
  <ul>
  <li><strong>So my question - What technologies I need to learn so that I can
  build the above app.</strong></li>
  </ul>
  
  <p>Note - I have already installed the Android platform and have built simple apps like Hello, world. I know Java. Also I am using Eclipse.</p>
  
  <p>Thank you for your answers. No rude comment please...</p>
  
  <p>//--------------Code for connecting to web using HTTP protocol-----------------//</p>
  
  <pre><code>package in.androidbook.Networking;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.net.URLConnection;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  import android.app.Activity;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.widget.ImageView;
  import android.widget.Toast;
  
  public class MainActivity extends Activity 
  {
      ImageView img;
      /* This is for making asynchronous calls to ensure that connection to server will not return until data is received */
  
      private class BackgroundTask extends AsyncTask&lt;String, Void, Bitmap&gt;
      {
          protected Bitmap doInBackground(String...url)
          {
              Bitmap bitmap = DownloadImage(url[0]);
              return bitmap;
          }
  
          protected void onPostExecute(Bitmap bitmap)
          {
              ImageView img = (ImageView) findViewById(R.id.img);
              img.setImageBitmap(bitmap);         
          }
      }
      // Code for making HTTP connection
      private InputStream OpenHttpConnection(String urlString) throws IOException 
      {
          InputStream in = null;
          int response = -1;
  
          URL url = new URL(urlString);//We take an object of class URL
          URLConnection conn = url.openConnection(); //Create a connection object and open the connection
  
          if(!(conn instanceof HttpURLConnection)) throw new IOException("Not an Http connection");
          try
          {
              HttpURLConnection httpConn = (HttpURLConnection) conn; //httpConn object is assigned the value of conn. Typecasting is done to avoid conflict.
              httpConn.setAllowUserInteraction(false);
              httpConn.setInstanceFollowRedirects(true);
              httpConn.setRequestMethod("GET");
              httpConn.connect();
              response = httpConn.getResponseCode();
  
              if(response == HttpURLConnection.HTTP_OK)
                  in = httpConn.getInputStream();
          }
          catch (Exception ex)
          {
              throw new IOException("Error connecting");
          }
          return in;
      }   
          //------------------------------------------ OpenHttpConnection method completed----------------------------------------------------------//
      //----------------------------------------------------------------------------------------------------------------------------------------------------------------//
      //-------------------------------Method to download an image--------------------------------------------------------------------------------------//
      private Bitmap DownloadImage(String URL)
      {
          Bitmap bitmap = null;
          InputStream in = null;
          try
          {
              in = OpenHttpConnection(URL);
              bitmap = BitmapFactory.decodeStream(in);
              in.close();
          }
          catch(IOException e1)
          {
              Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();
              //Toast displays a short msg to user. this refers to current object.
              e1.printStackTrace();
          }
          return bitmap;
      }  
  
  
  /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) 
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
         Bitmap bitmap = DownloadImage("http://i.zdnet.com/blogs/3-29-androids.jpg");
          img = (ImageView) findViewById(R.id.img);
          img.setImageBitmap(bitmap);
      }
  }
  </code></pre>