From e8eb7d2f9e2707f0d82466a8d45a49d6a9526ea2 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Wed, 14 Dec 2011 01:01:48 +0100 Subject: [PATCH] AsyncTask working: launching a new Task and storing the cookie Test2: DefaultHttpClient, for the new Android version it is a must to chage the default policy about threads using sockets (the UI must be as fast as possible) --- Android/Testing/Test1/res/layout/main.xml | 11 +-- .../de/android/test1/MobieAdHttpAuthClient.java | 46 ++++++---- .../Test1/src/de/android/test1/NextActivity.java | 4 + .../Test1/src/de/android/test1/Test1Activity.java | 26 ++++-- Android/Testing/Test2/AndroidManifest.xml | 34 ++++++++ Android/Testing/Test2/gen/de/android/test2/R.java | 36 ++++++++ Android/Testing/Test2/proguard.cfg | 40 +++++++++ Android/Testing/Test2/project.properties | 11 +++ .../Test2/res/drawable-hdpi/ic_launcher.png | Bin 0 -> 4147 bytes .../Test2/res/drawable-ldpi/ic_launcher.png | Bin 0 -> 1723 bytes .../Test2/res/drawable-mdpi/ic_launcher.png | Bin 0 -> 2574 bytes Android/Testing/Test2/res/layout/main.xml | 49 +++++++++++ Android/Testing/Test2/res/values/strings.xml | 11 +++ .../Test2/src/de/android/test2/Test2Activity.java | 96 +++++++++++++++++++++ 14 files changed, 337 insertions(+), 27 deletions(-) create mode 100644 Android/Testing/Test2/AndroidManifest.xml create mode 100644 Android/Testing/Test2/gen/de/android/test2/R.java create mode 100644 Android/Testing/Test2/proguard.cfg create mode 100644 Android/Testing/Test2/project.properties create mode 100644 Android/Testing/Test2/res/drawable-hdpi/ic_launcher.png create mode 100644 Android/Testing/Test2/res/drawable-ldpi/ic_launcher.png create mode 100644 Android/Testing/Test2/res/drawable-mdpi/ic_launcher.png create mode 100644 Android/Testing/Test2/res/layout/main.xml create mode 100644 Android/Testing/Test2/res/values/strings.xml create mode 100644 Android/Testing/Test2/src/de/android/test2/Test2Activity.java diff --git a/Android/Testing/Test1/res/layout/main.xml b/Android/Testing/Test1/res/layout/main.xml index 15264ad..cf49719 100644 --- a/Android/Testing/Test1/res/layout/main.xml +++ b/Android/Testing/Test1/res/layout/main.xml @@ -15,19 +15,16 @@ android:id="@+id/username" android:singleLine="true" android:layout_width="fill_parent" - android:layout_height="wrap_content" android:text="@string/username"/> - + android:layout_height="wrap_content" + android:hint="@string/username"/> + + android:hint="@string/password"/> { private static final String TAG = "MobieAdHttpAuthClient"; private AndroidHttpClient httpClient; private final String username; private final String password; + private final Context context; - public MobieAdHttpAuthClient(final String username, final String password) + public MobieAdHttpAuthClient(final String username, final String password, Context context) { this.username = username; this.password = password; + this.context = context; } @Override @@ -36,7 +44,8 @@ public class MobieAdHttpAuthClient extends AsyncTask final List nameValuePairs = new ArrayList(2); final HttpPost httpPost = new HttpPost(); HttpResponse httpResponse = null; - HttpEntity httpEntity = null; + HttpEntity httpEntity = null; + //TODO: RESTful Web Service must use JSON instead of signin array :( nameValuePairs.add(new BasicNameValuePair("signin[username]", this.username)); @@ -72,21 +81,28 @@ public class MobieAdHttpAuthClient extends AsyncTask protected void onPostExecute(final HttpResponse result) { this.httpClient.close(); - //Never should be null but this check should be not harmful + //It should not be null anyway this check is not harmful if (result != null) { - if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) - { - //OK GO TO THE NEXT ACTIVITY + switch (result.getStatusLine().getStatusCode()) { + case HttpStatus.SC_OK: + String cookie = result.getLastHeader("Set-Cookie").getValue(); + CookieManager.getInstance().setCookie("192.168.1.34/userfront.php",cookie); + CookieSyncManager.getInstance().sync(); + //OK GO TO THE NEXT ACTIVITY + Intent i = new Intent(this.context, NextActivity.class); + this.context.startActivity(i); + break; + case HttpStatus.SC_UNAUTHORIZED: + //ERROR IN USERNAME OR PASSWORD + break; + case HttpStatus.SC_BAD_REQUEST: + //WHAT THE HECK ARE YOU DOING? + break; + default: + Log.e(TAG, "Error while retrieving the HTTP status line."); + break; } - if (result.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) - { - //ERROR IN USERNAME OR PASSWORD - } - if (result.getStatusLine().getStatusCode() == HttpStatus.SC_BAD_REQUEST) - { - //WHAT THE HECK ARE YOU DOING? - } - } + } } } diff --git a/Android/Testing/Test1/src/de/android/test1/NextActivity.java b/Android/Testing/Test1/src/de/android/test1/NextActivity.java index 2242fe3..5e769db 100644 --- a/Android/Testing/Test1/src/de/android/test1/NextActivity.java +++ b/Android/Testing/Test1/src/de/android/test1/NextActivity.java @@ -2,12 +2,16 @@ package de.android.test1; import android.app.Activity; import android.os.Bundle; +import android.webkit.CookieManager; +import android.webkit.CookieSyncManager; public class NextActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + CookieSyncManager.createInstance(this); + String myCookie = CookieManager.getInstance().getCookie("192.168.1.34/userfront.php"); setContentView(R.layout.main2); } } diff --git a/Android/Testing/Test1/src/de/android/test1/Test1Activity.java b/Android/Testing/Test1/src/de/android/test1/Test1Activity.java index 4533d5f..56a2dde 100644 --- a/Android/Testing/Test1/src/de/android/test1/Test1Activity.java +++ b/Android/Testing/Test1/src/de/android/test1/Test1Activity.java @@ -1,11 +1,29 @@ package de.android.test1; +import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; +import java.net.URISyntaxException; import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.content.Intent; +import android.net.http.AndroidHttpClient; +import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; import android.view.View; +import android.webkit.CookieManager; +import android.webkit.CookieSyncManager; import android.widget.EditText; public class Test1Activity extends Activity { @@ -14,7 +32,8 @@ public class Test1Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + super.onCreate(savedInstanceState); + CookieSyncManager.createInstance(this); setContentView(R.layout.main); } @@ -36,10 +55,7 @@ public class Test1Activity extends Activity { // TODO Auto-generated catch block e.printStackTrace(); } - new MobieAdHttpAuthClient(username.getText().toString(), password.getText().toString()).execute(url); - - //Intent i = new Intent(Test1Activity.this, NextActivity.class); - //this.startActivity(i); + new MobieAdHttpAuthClient(username.getText().toString(), password.getText().toString(), this).execute(url); } public void onClickCancel(View v) { diff --git a/Android/Testing/Test2/AndroidManifest.xml b/Android/Testing/Test2/AndroidManifest.xml new file mode 100644 index 0000000..57577ad --- /dev/null +++ b/Android/Testing/Test2/AndroidManifest.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Android/Testing/Test2/gen/de/android/test2/R.java b/Android/Testing/Test2/gen/de/android/test2/R.java new file mode 100644 index 0000000..37ca302 --- /dev/null +++ b/Android/Testing/Test2/gen/de/android/test2/R.java @@ -0,0 +1,36 @@ +/* AUTO-GENERATED FILE. DO NOT MODIFY. + * + * This class was automatically generated by the + * aapt tool from the resource data it found. It + * should not be modified by hand. + */ + +package de.android.test2; + +public final class R { + public static final class attr { + } + public static final class drawable { + public static final int ic_launcher=0x7f020000; + } + public static final class id { + public static final int cancel_button=0x7f050005; + public static final int frameLayout1=0x7f050000; + public static final int frameLayout2=0x7f050003; + public static final int login_button=0x7f050004; + public static final int password=0x7f050002; + public static final int username=0x7f050001; + } + public static final class layout { + public static final int main=0x7f030000; + } + public static final class string { + public static final int app_name=0x7f040001; + public static final int button_cancel=0x7f040006; + public static final int button_login=0x7f040004; + public static final int button_ok=0x7f040005; + public static final int hello=0x7f040000; + public static final int password=0x7f040003; + public static final int username=0x7f040002; + } +} diff --git a/Android/Testing/Test2/proguard.cfg b/Android/Testing/Test2/proguard.cfg new file mode 100644 index 0000000..b1cdf17 --- /dev/null +++ b/Android/Testing/Test2/proguard.cfg @@ -0,0 +1,40 @@ +-optimizationpasses 5 +-dontusemixedcaseclassnames +-dontskipnonpubliclibraryclasses +-dontpreverify +-verbose +-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* + +-keep public class * extends android.app.Activity +-keep public class * extends android.app.Application +-keep public class * extends android.app.Service +-keep public class * extends android.content.BroadcastReceiver +-keep public class * extends android.content.ContentProvider +-keep public class * extends android.app.backup.BackupAgentHelper +-keep public class * extends android.preference.Preference +-keep public class com.android.vending.licensing.ILicensingService + +-keepclasseswithmembernames class * { + native ; +} + +-keepclasseswithmembers class * { + public (android.content.Context, android.util.AttributeSet); +} + +-keepclasseswithmembers class * { + public (android.content.Context, android.util.AttributeSet, int); +} + +-keepclassmembers class * extends android.app.Activity { + public void *(android.view.View); +} + +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); +} + +-keep class * implements android.os.Parcelable { + public static final android.os.Parcelable$Creator *; +} diff --git a/Android/Testing/Test2/project.properties b/Android/Testing/Test2/project.properties new file mode 100644 index 0000000..730e911 --- /dev/null +++ b/Android/Testing/Test2/project.properties @@ -0,0 +1,11 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system use, +# "ant.properties", and override values to adapt the script to your +# project structure. + +# Project target. +target=android-14 diff --git a/Android/Testing/Test2/res/drawable-hdpi/ic_launcher.png b/Android/Testing/Test2/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000000000000000000000000000000000000..8074c4c571b8cd19e27f4ee5545df367420686d7 GIT binary patch literal 4147 zcmV-35X|q1P)OwvMs$Q8_8nISM!^>PxsujeDCl4&hPxrxkp%Qc^^|l zp6LqAcf3zf1H4aA1Gv-O6ha)ktct9Y+VA@N^9i;p0H%6v>ZJZYQ`zEa396z-gi{r_ zDz)D=vgRv62GCVeRjK{15j7V@v6|2nafFX6W7z2j1_T0a zLyT3pGTubf1lB5)32>bl0*BflrA!$|_(WD2)iJIfV}37=ZKAC zSe3boYtQ=;o0i>)Rt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c(nR$rSAaFQ$(Wh{vkG1AlutDMw=mM`C`T=X&|Ad9fb5Od}ROt1z zOpczHqrb4Jo^rSCiW#&o(m7jFamnrsTpQb;*h4o8r#$aZ}2RaT-x2u^^ z%u@YyIv$U^u~@9(XGbSwU@fk6SikH>j+D1jQrYTKGJpW%vUT{!d}7THI5&Sa?~MKy zS0-mvMl+BOcroEJ@hN!2H_?coTEJ5Q<;Nd?yx;eIj4{$$E2?YUO|NtNPJ-PdDf;s} zab;}Mz0kbOI}5*w@3gROcnl#5)wQnEhDBfn!Xhy`u>C}*E~vWpO^HS)FC>8^umI=+ z&H;LW6w#;EF`}vQd_9Muru`KnQVPI9U?(sD)&Dg-0j3#(!fNKVZ_GoYH{la~d*1Yh$TI-TL>mI4vpNb@sU2=IZ8vL%AXUx0 zz{K0|nK(yizLHaeW#ZhRfQXoK^}1$=$#1{Yn002ovPDHLkV1n#w+^+xt literal 0 HcmV?d00001 diff --git a/Android/Testing/Test2/res/drawable-ldpi/ic_launcher.png b/Android/Testing/Test2/res/drawable-ldpi/ic_launcher.png new file mode 100644 index 0000000000000000000000000000000000000000..1095584ec21f71cd0afc9e0993aa2209671b590c GIT binary patch literal 1723 zcmV;s21NOZP)AReP91Tc8>~sHP8V>Ys(CF=aT`Sk=;|pS}XrJPb~T1dys{sdO&0YpQBSz*~us zcN*3-J_EnE1cxrXiq*F~jZje~rkAe3vf3>;eR)3?Ox=jK*jEU7Do|T`2NqP{56w(* zBAf)rvPB_7rsfeKd0^!CaR%BHUC$tsP9m8a!i@4&TxxzagzsYHJvblx4rRUu#0Jlz zclZJwdC}7S3BvwaIMTiwb!98zRf|zoya>NudJkDGgEYs=q*HmC)>GExofw=92}s;l z_YgKLUT5`<1RBwq{f)K~I%M=gRE6d)b5BP`8{u9x0-wsG%H)w^ zRU7n9FwtlfsZSjiSB(k8~Y5+O>dyoSI477Ly?|FR?m))C!ci%BtY!2Sst8Uri#|SFX&)8{_Ou2 z9r5p3Vz9_GY#%D>%huqp_>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