First HTTP async client connecting to my RESTful Web Service
authorgumartinm <gu.martinm@gmail.com>
Fri, 25 Nov 2011 01:11:18 +0000 (02:11 +0100)
committergumartinm <gu.martinm@gmail.com>
Fri, 25 Nov 2011 01:11:18 +0000 (02:11 +0100)
Android/Testing/Test1/AndroidManifest.xml
Android/Testing/Test1/src/de/android/test1/MobieAdHttpClient.java [new file with mode: 0644]
Android/Testing/Test1/src/de/android/test1/Test1Activity.java

index 2ff5f86..0907f84 100644 (file)
@@ -5,6 +5,7 @@
     android:versionName="1.0" >
 
     <uses-sdk android:minSdkVersion="13" />
+    <uses-permission android:name="android.permission.INTERNET"/>
 
     <application
         android:icon="@drawable/ic_launcher"
@@ -15,7 +16,7 @@
             android:screenOrientation="portrait" 
             android:configChanges="touchscreen|keyboard"
             android:theme="@android:style/Theme.Black" 
-            android:permission="android.permission.ACCESS_FINE_LOCATION
+            android:permission="android.permission.INTERNET
             android:launchMode="standard">
             <intent-filter >
                 <action android:name="android.intent.action.MAIN" />
diff --git a/Android/Testing/Test1/src/de/android/test1/MobieAdHttpClient.java b/Android/Testing/Test1/src/de/android/test1/MobieAdHttpClient.java
new file mode 100644 (file)
index 0000000..7645e15
--- /dev/null
@@ -0,0 +1,38 @@
+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 d1a5768..32cd223 100644 (file)
@@ -1,36 +1,20 @@
 package de.android.test1;
 
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
-import java.util.List;
+import java.net.MalformedURLException;
+import java.net.URL;
 
-import org.apache.http.HttpResponse;
-import org.apache.http.NameValuePair;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.message.BasicNameValuePair;
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
-import android.os.StrictMode;
 import android.view.View;
-import android.widget.EditText;
 
 public class Test1Activity extends Activity {
+       
+       
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        ///JUSTE DEVELOPER MODE!!!!! GOOGLE "RECOMENDS" TO USE A DIFFERENT THREAD FOR NETWORK CONNECTIONS
-        // THE STRICTMODE WILL STOP ME USING THE NETWORK DIRECTLY IN THIS THREAD  :S (for someone coming
-        // from programming drivers this is a bit stupid... but anyway the most of the developers are idiot "word of Linus")
-        StrictMode.enableDefaults();
-        // 
-        
+        super.onCreate(savedInstanceState);       
         setContentView(R.layout.main);
     }
     
@@ -40,34 +24,17 @@ public class Test1Activity extends Activity {
     }
     
     public void onClickLogin(View v) {
-       HttpClient httpclient = new DefaultHttpClient();
        
-       //RESTful WebService
-       HttpPost httppost = new HttpPost("http://192.168.1.34/userfront.php/api/51,32/0,5/gpsads.xml");
        
-       EditText uname = (EditText)findViewById(R.id.username);
-        String username = uname.getText().toString();
-        
-        EditText pword = (EditText)findViewById(R.id.password);
-        String password = pword.getText().toString();
-        
-        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
-        nameValuePairs.add(new BasicNameValuePair("username", username));
-        nameValuePairs.add(new BasicNameValuePair("password", password));
-        try {
-                       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
-                       // Execute HTTP Post Request
-                       HttpResponse response = httpclient.execute(httppost);
-               } catch (UnsupportedEncodingException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               } catch (ClientProtocolException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               } catch (IOException e) {
+       URL url = null;
+               try {
+                       //RESTful WebService
+                       url = new URL("http://192.168.1.34/userfront.php/api/51,32/0,5/gpsads.xml");
+               } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
+               new MobieAdHttpClient().execute(url);
 
        //Intent i = new Intent(Test1Activity.this, NextActivity.class);
        //this.startActivity(i);