Thứ Hai, 28 tháng 12, 2009

Android: Http Connection

This time, I'm going to building a service in Android. There are a lot of types of services with Android included: IPC (communication between applications on the same device), get resources outside the devices by various type of connections. I'll focus on Http connection first.
Although Android included web-browser application in the basic applications, but there are several benefits to creating thick and thin native application rather than relying on entirely web-base:
- Bandwidth
- Caching
- Native features.
With currently connection methods:
- Wi-Fi.
more details could collected from reference books below.

Connection permission for Android application
By Eclipse ADT plug-in:
- Open AndroidManifest.xml file (in design mode)
- Select "Permissions" tab ==> click "Add" and select "Uses permission"
- At the "Name" property, select "android.permission.INTERNET"
- Save setting.

By XML editor:
- Open AndroidManifest.xml file (in editor mode)
- Add below code:
Open an URL connection and get content:

public static String getHttpContent(String prURL) {
String strResult = "";
StringBuffer sbfResult = new StringBuffer();
String newline = System.getProperty("line.seperator");

try {
URL url = new URL(prURL);
URLConnection urlcon = url.openConnection();
HttpURLConnection httpurlcon = (HttpURLConnection) urlcon;

int responseCode = httpurlcon.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpurlcon.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = br.readLine()) != null) {
} catch (Exception e) {
if (sbfResult != null) {
strResult = sbfResult.toString();
strResult = "StringBuffer has failed.";
return strResult;

- Pro Android June 2009
- Profession Android Application Development.

Không có nhận xét nào:

Đăng nhận xét