From: gumartinm Date: Wed, 22 Feb 2012 06:46:02 +0000 (+0100) Subject: New Android API (version 15) and firs steps downloading X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=3818d306330961d4fba20f7cc118c35bac8fb6cf;p=JavaForFun New Android API (version 15) and firs steps downloading Ads from the server using HttpClient --- diff --git a/Android/AndroidTetris/project.properties b/Android/AndroidTetris/project.properties index 730e911..8da376a 100644 --- a/Android/AndroidTetris/project.properties +++ b/Android/AndroidTetris/project.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-14 +target=android-15 diff --git a/Android/Testing/Test2/project.properties b/Android/Testing/Test2/project.properties index 730e911..8da376a 100644 --- a/Android/Testing/Test2/project.properties +++ b/Android/Testing/Test2/project.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-14 +target=android-15 diff --git a/Android/Testing/Test3/AndroidManifest.xml b/Android/Testing/Test3/AndroidManifest.xml index 4fcd010..e7208c7 100644 --- a/Android/Testing/Test3/AndroidManifest.xml +++ b/Android/Testing/Test3/AndroidManifest.xml @@ -4,7 +4,7 @@ android:versionCode="1" android:versionName="1.0" > - + diff --git a/Android/Testing/Test3/project.properties b/Android/Testing/Test3/project.properties index 730e911..8da376a 100644 --- a/Android/Testing/Test3/project.properties +++ b/Android/Testing/Test3/project.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-14 +target=android-15 diff --git a/Android/Testing/Test3/src/de/android/test3/MobieAdHttpClient.java b/Android/Testing/Test3/src/de/android/test3/MobieAdHttpClient.java index 1dadd73..57c9049 100644 --- a/Android/Testing/Test3/src/de/android/test3/MobieAdHttpClient.java +++ b/Android/Testing/Test3/src/de/android/test3/MobieAdHttpClient.java @@ -15,6 +15,7 @@ import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; +import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -159,9 +160,41 @@ public class MobieAdHttpClient implements Runnable return builder; } - public void downloadAds(String domain, String link, String path) { - //if the id is not on the data base, download the ad, otherwise do nothing. USE synchronize + public byte[] sortDownloadAd(HttpResponse httpResponse) throws UnsupportedEncodingException, IllegalStateException, IOException { + byte[] file = null; + + if (httpResponse != null) { + switch (httpResponse.getStatusLine().getStatusCode()) { + case HttpStatus.SC_OK: + //OK + HttpEntity entity = httpResponse.getEntity(); + if (entity != null) { + file = EntityUtils.toByteArray(entity); + } + break; + case HttpStatus.SC_UNAUTHORIZED: + //ERROR IN USERNAME OR PASSWORD + throw new SecurityException("Unauthorized access: error in username or password."); + case HttpStatus.SC_BAD_REQUEST: + //WHAT THE HECK ARE YOU DOING? + throw new IllegalArgumentException("Bad request."); + default: + throw new IllegalArgumentException("Error while retrieving the HTTP status line."); + } + } + return file; + } + + public void downloadAds(String domain, String link, String path) { + ResponseHandler handler = new ResponseHandler() { + public byte[] handleResponse(HttpResponse response) + throws ClientProtocolException, UnsupportedEncodingException, IllegalStateException, IOException { + //There could be a null as return value in case of not receiving any data from the server, + //although the HTTP connection was OK. + return sortDownloadAd(response); + } + }; final HttpGet httpGet = new HttpGet(); final String URLAd = "http://" + domain + "/" + link; HttpResponse httpResponse = null;