Connection to RESTful Web Service authentication
authorgumartinm <gu.martinm@gmail.com>
Mon, 12 Dec 2011 06:59:14 +0000 (07:59 +0100)
committergumartinm <gu.martinm@gmail.com>
Mon, 12 Dec 2011 06:59:14 +0000 (07:59 +0100)
Android/Testing/Test1/src/de/android/test1/MobieAdHttpAuthClient.java [new file with mode: 0644]
Android/Testing/Test1/src/de/android/test1/MobieAdHttpClient.java [deleted file]
Android/Testing/Test1/src/de/android/test1/Test1Activity.java

diff --git a/Android/Testing/Test1/src/de/android/test1/MobieAdHttpAuthClient.java b/Android/Testing/Test1/src/de/android/test1/MobieAdHttpAuthClient.java
new file mode 100644 (file)
index 0000000..ebcdb6f
--- /dev/null
@@ -0,0 +1,92 @@
+package de.android.test1;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+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.net.http.AndroidHttpClient;
+import android.os.AsyncTask;
+import android.util.Log;
+
+public class MobieAdHttpAuthClient extends AsyncTask<URL, Integer, HttpResponse> {
+       private static final String TAG = "MobieAdHttpAuthClient";
+       private AndroidHttpClient httpClient;
+       private final String username;
+       private final String password;
+       
+       public MobieAdHttpAuthClient(final String username, final String password)
+       {
+               this.username = username;
+               this.password = password;
+       }
+       
+       @Override
+       protected HttpResponse doInBackground(final URL... urls) {
+               final String USERAGENT ="MobieAds/1.0";
+               final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
+               final HttpPost httpPost = new HttpPost();
+               HttpResponse httpResponse = null;
+               HttpEntity httpEntity = null;      
+        
+        //TODO: RESTful Web Service must use JSON instead of signin array :(
+        nameValuePairs.add(new BasicNameValuePair("signin[username]", this.username));
+        nameValuePairs.add(new BasicNameValuePair("signin[password]", this.password));
+        try {
+                       httpEntity = new UrlEncodedFormEntity(nameValuePairs);
+               } catch (UnsupportedEncodingException e2) {
+                       Log.e(TAG, "Error while encoding POST parameters.");
+               }
+
+               for(URL url : urls)
+        {
+                       try {
+                               httpPost.setURI(url.toURI());
+                       } catch (URISyntaxException e) {
+                               Log.e(TAG, "Error while creating URI from URL.");
+                       }
+                       httpPost.setEntity(httpEntity);       
+               this.httpClient = AndroidHttpClient.newInstance(USERAGENT);
+               try {
+                       httpResponse = httpClient.execute(httpPost);
+               } catch (ClientProtocolException e1) {
+                       Log.e(TAG, "Error while executing HTTP client connection.");
+               } catch (IOException e1) {
+                       Log.e(TAG, "Error while executing HTTP client connection.");
+               }
+         }
+               
+               return httpResponse;
+       }
+       
+       @Override
+       protected void onPostExecute(final HttpResponse result)
+       {
+               this.httpClient.close();
+               //Never should be null but this check should be not harmful
+               if (result != null)
+               {
+                       if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
+                       {
+                               //OK GO TO THE NEXT ACTIVITY
+                       }
+                       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/MobieAdHttpClient.java b/Android/Testing/Test1/src/de/android/test1/MobieAdHttpClient.java
deleted file mode 100644 (file)
index 7645e15..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-package de.android.test1;
-
-import java.io.IOException;
-import java.net.URL;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.methods.HttpGet;
-import android.net.http.AndroidHttpClient;
-import android.os.AsyncTask;
-
-public class MobieAdHttpClient extends AsyncTask<URL, Integer, Integer> {
-       private static final String USERAGENT ="MobieAds/1.0";
-       private AndroidHttpClient httpClient;
-       
-       @Override
-       protected Integer doInBackground(URL... urls) {
-               for(URL url : urls)
-        {              
-               this.httpClient = AndroidHttpClient.newInstance(USERAGENT);
-               try {
-                       httpClient.execute(new HttpGet(url.toString()));
-               } catch (ClientProtocolException e1) {
-                       // TODO Auto-generated catch block
-                       e1.printStackTrace();
-               } catch (IOException e1) {
-                       // TODO Auto-generated catch block
-                       e1.printStackTrace();
-               }
-         }
-               
-               return null;
-       }
-       
-       protected void onPostExecute(Integer result)
-       {
-               this.httpClient.close();
-       }
-
-}
index 32cd223..4533d5f 100644 (file)
@@ -2,11 +2,11 @@ package de.android.test1;
 
 import java.net.MalformedURLException;
 import java.net.URL;
-
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
+import android.widget.EditText;
 
 public class Test1Activity extends Activity {
        
@@ -24,17 +24,19 @@ public class Test1Activity extends Activity {
     }
     
     public void onClickLogin(View v) {
-       
-       
+       final String URLAuth = "http://192.168.1.34/userfront.php/api/login/auth.json";
+       final EditText password = (EditText) findViewById(R.id.password);
+       final EditText username = (EditText) findViewById(R.id.username);
        URL url = null;
+       
                try {
                        //RESTful WebService
-                       url = new URL("http://192.168.1.34/userfront.php/api/51,32/0,5/gpsads.xml");
+                       url = new URL(URLAuth);
                } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
-               new MobieAdHttpClient().execute(url);
+               new MobieAdHttpAuthClient(username.getText().toString(), password.getText().toString()).execute(url);
 
        //Intent i = new Intent(Test1Activity.this, NextActivity.class);
        //this.startActivity(i);