--- /dev/null
+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?
+ }
+ }
+ }
+}
+++ /dev/null
-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();
- }
-
-}
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 {
}
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);