Thanks for the help, I've got it working now, both pulling back data
from a database using JSON and putting data on the server using
HttpGet. I've also found out I can still use session type
authorisation by keeping a HttpClient open rather then creating a new
one for each request (which the example I was working off did). That
will stop the need of submitting auth data with each request which was
something I was a little concerned about doing.
Also does anyone have a small example of something that uses HttpPost
rather then HttpGet to submit data, otherwise I'll post the code I
have so far and I would appreciate any advice about better ways of
doing it, don't worry to much about the blank exception catching,
they're more there for my reference for where they should be when I
decide how to handle them.

package net.skid_inc.webprototype;

import android.os.Handler;
import android.util.Log;
import android.telephony.TelephonyManager;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;

/**
 *
 * @author Skid
 */
public class NetComms
{
        private static final String LOGTAG = "ServiceLog";

        // Delay between start of the timer and the first time it runs the
task
        private static final long TIMERDELAY = 0;
        // Inverval between each time the task is run
        private static final long TIMERPERIOD = 60000;

        // Link to the activity that called this class
        private final MainActivity parent;
        // Stores the phones device id
        private String imei;
        private String android_id;

        // HttpClient is created now to test if it can both hold a session ID
and keep it
        public HttpClient client = new DefaultHttpClient();

        // Timer for regualer updates
        private Timer updatetimer;
        // Task to run when timer is tiggered
        private UpdateTask updatetask;

        // Result of a manual update
        private String update;
        // Allows the getUpdate thread to return the data it retrives
        private final Handler updateHandler;
        // Called after getUpdate has finished and sends the data back to the
activity
        private final Runnable updateRunnable = new Runnable ()
        {
                public void run()
                {
                        String output = "";
                        try
                        {
                                JSONArray json = new JSONArray(update);
                                for (int t = 0; t < json.length(); t++)
                                {
                                        JSONArray json2 = json.getJSONArray(t);
                                        for (int tt = 0; tt < json2.length(); 
tt++)
                                        {
                                                if (tt != json2.length() - 1)
                                                {
                                                        output += 
json2.getString(tt) + ", ";
                                                }
                                                else
                                                {
                                                        output += 
json2.getString(tt);
                                                }
                                        }
                                        output += "\n";
                                }
                        }
                        catch (Exception ex)
                        {
                        }
                        finally
                        {
                                parent.content.setText(output);
                        }
                }
        };

        /**
         * Creates a new Net Communications class and links it to the
MainActivity
         * @param parent MainActivity that created this instance.
         */
        public NetComms (MainActivity parent)
        {
                this.parent = parent;
                Log.i(LOGTAG, "Created");
                TelephonyManager mTelephonyMgr = (TelephonyManager)
parent.getSystemService(MainActivity.TELEPHONY_SERVICE);
                imei = mTelephonyMgr.getDeviceId();
                android_id = android.provider.Settings.System.getString
(parent.getContentResolver(),
android.provider.Settings.System.ANDROID_ID);

                updatetimer = new Timer();
                updatetask = new UpdateTask();

                update = "";
                updateHandler = new Handler();
        }

        /**
         * Starts the Update Task with the preset delays
         */
        public void startUpdater()
        {
                Log.i(LOGTAG, "Started");
                updatetimer.scheduleAtFixedRate(updatetask, TIMERDELAY,
TIMERPERIOD);
        }

        /**
         *
         */
        public void getUpdate ()
        {
                Thread t = new Thread() {
                        @Override
                        public void run() {
                                String url = 
"http://www.skid-inc.net/send.php?id="; + imei +
"&time=" + System.currentTimeMillis();
                                HttpGet request = new HttpGet(url);
                                try
                                {
                                        HttpResponse response = 
client.execute(request);
                                        update = getText(response);
                                        Log.i(LOGTAG, "URL:" + url);
                                        Log.i(LOGTAG, "IMEI:" + imei + " ID:" + 
android_id);
                                        Log.i(LOGTAG, update);
                                        //updateHandler.post(updateRunnable);
                                }
                                catch(Exception ex)
                                {
                                        Log.i(LOGTAG, "Update Fail!");
                                }
                        }
                };
                t.start();
        }

        /**
         *
         */
        private class UpdateTask extends TimerTask
        {
                public void run()
                {
                        String url = "http://www.skid-inc.net/update.php";;
                        HttpGet request = new HttpGet(url);
                        try
                        {
                                HttpResponse response = client.execute(request);
                                update = getText(response);
                                Log.i(LOGTAG, update);
                                updateHandler.post(updateRunnable);
                        }
                        catch(Exception ex)
                        {
                                Log.i(LOGTAG, "Fail!");
                        }
                }
        }

        /**
         *
         * @param in
         * @return
         */
        public static String getText (InputStream in)
        {
                String text = "";
                BufferedReader reader = new BufferedReader (new 
InputStreamReader
(in));
                StringBuilder sb = new StringBuilder();
                String line = null;

                try
                {
                        while ((line = reader.readLine()) != null)
                        {
                                sb.append(line + "\n");
                        }
                        text = sb.toString();
                }
                catch (Exception ex)
                {
                }
                finally
                {
                        try
                        {
                                in.close();
                        }
                        catch (Exception ex)
                        {
                        }
                }
                return text;
        }

        /**
         *
         * @param response
         * @return
         */
        public static String getText (HttpResponse response)
        {
                String text = "";
                try
                {
                        text = getText(response.getEntity().getContent());
                }
                catch(Exception ex)
                {
                }
                return text;
        }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to