Bubble ZOO Escape Anouncement

  Hi folks! I am proud to present: 
Bubble Zoo Escape, my first Android game built using AndEngine 
If you have a time, please download and give feedback!

Android application and the Internet

Nov 30

Written by: Goran Siric
11/30/2011 9:14 AM  RssIcon

To allow your application to connect to the internet first you need to do is  to modify AndroidManifest.xml file to request INTERNET permission for your application.

To do this open AndroidManifest.xml file and add following line if not exists:

<uses-permission android:name="android.permission.INTERNET"/>


It is good to know that if you want to connect to your local web server (localhost), you must use IP address of  your computer instead of  localhost keyword or 127.0.0.1 IP address. That's it because 127.0.0.1 IP address is internally used by android emulator or your android device. 

Somewhere I found that you can use 10.0.0.2 IP adress to connect to  your local web server. I tried it but this not work for me. I think that this depends of type of web server you have installed.

Here is code of  test.aspx web page which I created for testing purposes. Page returns  information about which HTTP method is used (POST or GET), what are Query String parameters and what are form data values.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String result = "";
 
        result += "HTTP METHOD:" + Request.HttpMethod;
        result += "<BR>";
 
 
        if (Request.Form.Count == 0)
        {
            result += "There is no form data<BR>";
        }
        else
        {
            result += "FORM DATA:<BR>";
 
            foreach (String key in Request.Form.AllKeys)
            {
                result += key + ": " + Request.Form[key] + "<BR>";
            }
        }
 
        if (Request.QueryString.Count == 0)
        {
            result += "There is no parameters in query string<BR>";
        }
        else
        {
            result += "QUERY STRING PARAMETERS:<BR>";
 
            foreach (String key in Request.QueryString.AllKeys)
            {
                result += key + ": " + Request.QueryString[key] + "<BR>";
            }
        }
 
 
        Response.Write(result);
        Response.End();
    }
}

Execeuted in web browser without any parameters page:

http://localhost/dotnetnuke/test.aspx

will show following result:

HTTP METHOD:GET
There is no form data
There is no parameters in query string

Notice that the page  test.aspx is on my webserver installed in dotnetnuke web application. On your system it will probably be somewhere else.

Here is the code you can use to send GET and POST http requests to the page along with some QueryString parameters and with some form data (when you use POST http request). Comments are in code so I will not explain code in more details.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
 
public class UploadDataToUrlActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
         
        setContentView(R.layout.l1_upload_data_to_url);
     
         
        // Create URI with some QueryString parameters
         
        Uri uri = new Uri.Builder()
            .scheme("http")
            .authority("192.168.0.13")
            .path("dotnetnuke/test.aspx")
            .appendQueryParameter("param1", "value1")
            .appendQueryParameter("param2", "value2")
            .build();
         
         
        try
        {
            // execute HTTP GET request
            OutputStream response = HttpRequestGet(uri);
             
            // Write output to screen
            TextView txtResponseGet = (TextView)findViewById(R.id.l1_txtResponseGet);
            txtResponseGet.setText(response.toString().replace("<BR>", "\n"));
 
        }
        catch (IOException e)
        {
            // do error handling here
        }
         
        try
        {
            // Create array list of form data
            ArrayList formData = new ArrayList();
            formData.add(new BasicNameValuePair("formData1", "formValue1"));
            formData.add(new BasicNameValuePair("formData2", "formValue2"));
             
            // execute HTTP POST request
            OutputStream response = HttpRequestPost(uri,formData);
 
            // Write output to screen
            TextView txtResponsePost = (TextView)findViewById(R.id.l1_txtResponsePost);
            txtResponsePost.setText(response.toString().replace("<BR>", "\n"));
             
        }
        catch (IOException e)
        {
            // do error handling here
        }
             
         
    }
 
    // Method for sending HTTP GET request to a web page
    public OutputStream HttpRequestGet(Uri uri) throws IOException
    {
                 
        HttpGet httpget = new HttpGet(uri.toString());
         
        HttpClient httpclient = new DefaultHttpClient();
                         
        HttpResponse response = httpclient.execute(httpget);
         
        HttpEntity httpentity = response.getEntity();
         
        if (null == httpentity)
        {
            throw(new IOException("HttpEntity is null"));
        }
         
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        httpentity.writeTo(outstream);
             
        return outstream;
    }
 
    // Method for sending HTTP POST request to a web page
    public OutputStream HttpRequestPost(Uri uri, ArrayList formData) throws IOException
    {
                 
        HttpPost httppost = new HttpPost(uri.toString());
         
        httppost.setEntity(new UrlEncodedFormEntity(formData));
     
        HttpClient httpclient = new DefaultHttpClient();
     
        HttpResponse response = httpclient.execute(httppost);
         
        HttpEntity httpentity = response.getEntity();
         
        if (null == httpentity)
        {
            throw(new IOException("HttpEntity is null"));
        }
         
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        httpentity.writeTo(outstream);
             
        return outstream;
    }
     
}

If everything is fine you should get following result on your device:




Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

Bubble Zoo Escape

  Hi folks! I am proud to present: 
Bubble Zoo Escape, my first Android game built using AndEngine 
If you have a time, please download and give feedback!

Search Blog

If you found this useful,
you can buy me a coffe :)
By me a coffe through PayPal :)

Featured articles

AndEngine - Textures tips and tricks

What you should know abot textures before get started programming your first game.

GIMP script for creating Android icons at once

Script for creating Android icons for different screen resolutions at once. Icons can be saved using standard Android icons naming conventions and saved in appropriate folders.

Creating Android button with image and text using relative layout

Source code with examples how to use relative layout to create nice buttons with text and images in Android

Android application and the Internet

Tutorial about connecting to the web pages on the Internet from Android application, using both POST and GET web requests.

Recent Entries

Unity start guide
AndEngine - Textures tips and tricks
Calculated variables
Comments about Parking SMS Scheduler
Comments about cities and parking zones
Android "DataEntry ListView" Design Pattern
Android application and the Internet

Recent Comments

Re: Creating Android button with image and text using relative layout
What if, using the drawable left method, i want to increase the distance of the icon from the left limit of the button, making it a bit more central?
Re: Creating Android button with image and text using relative layout
Hi Shameel,try to use ImageView android:scaleType property in xml button layout:

android:scaleType="centerInside"

For more info see: developer.android.com/reference/android/widget/ImageView.ScaleType.html
Re: Creating Android button with image and text using relative layout
Hi bro,

Can u tell me how to set the size of icon according to our requirement in button ?

Actually my icon size is 256x256 and i want 20x20 ???
Re: Creating Android button with image and text using relative layout
Hi dude, i have also find out one good example

How To Create Button

Re: Creating Android button with image and text using relative layout
Hello, I am new for android.I want to move text on image view. how its possible?
Plz help me.Thanks in advance