title: Android Local Service Architecture
body: <p>I'm trying to implement a service to handle the communication with the server for the following code.  I don't know much about the design architecture for these.</p>
  
  <p>Here is my service class</p>
  
  <pre><code>public class BgService extends Service {
      private static final String TAG = BgService.class.getSimpleName();
      private Timer timer;
      SendJsonRequest sjr;
  
      private TimerTask updateTask = new TimerTask(){
          @Override
          public void run(){
              try{
                  SendJsonRequest sjr = new SendJsonRequest();
                  sjr.carMake();
                  Log.i(TAG, "LOOK AT ME");
              }
              catch(Exception e){
                  Log.w(TAG,e);
              }
          }
      };
      @Override
      public IBinder onBind(Intent intent) {
          // TODO Auto-generated method stub
          return null;
      }
      @Override
      public void onCreate(){
          super.onCreate();
          Log.i(TAG, "Service creating");
          timer = new Timer("Server listening timer");
          timer.schedule(updateTask, 1000L, 60*1000L);
      }
  
      @Override
      public void onDestroy(){
          super.onDestroy();
          Log.i(TAG, "Service Destroying");
          timer.cancel();
          timer = null;
      }
  }
  </code></pre>
  
  <p>Here is my SendJsonRequest class</p>
  
  <pre><code>public class SendJsonRequest{
           private static final String TAG = "SendJsonRequest";
           private static String URL = "xxxxxxxxx";
           private static String infoRec;
  
           public static void createJsonObj(String path, Map x){
               infoRec = CreateJsonRequest.jsonRequest(URL+path, x );
               System.out.println(infoRec);
           }
           public static void carMake(){
               String path = "/CarMake";
               Map&lt;String, Object&gt; z = new HashMap&lt;String,Object&gt;();
               z.put("Name", "Ford");
               z.put("Model", "Mustang");
               createJsonObj(path, z);
           }
  
          }
  </code></pre>
  
  <p>Here is my CreateJsonObject class</p>
  
  <pre><code>public class CreateJsonRequest {
      public static String jsonRequest(String URL, Map&lt;String,Object&gt; params){
          try{
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(URL);
  
              JSONObject holder = new JSONObject();
  
              for (Map.Entry&lt;String, Object&gt; m : params.entrySet()){
                  try {
                      holder.put(m.getKey(), m.getValue());
                  }
                  catch (JSONException e) {
                      Log.e("Hmmmm", "JSONException : "+e);
                  }
              }   
              StringEntity se;
              se = new StringEntity(holder.toString());
  
              httpPost.setEntity(se);
              httpPost.setHeader("Accept", "text/json");
              httpPost.setHeader("Content-type", "text/json");
  
              HttpResponse response = (HttpResponse) httpClient.execute(httpPost);
              HttpEntity entity = response.getEntity();
  
              if(entity != null){
                  InputStream is = entity.getContent();
                  Header contentEncoding = response.getFirstHeader("Content-Encoding");
  
                  String result= convertToString(is);
                  is.close();
  
                  System.out.println(result);
                  return result;
  
              }
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
          return null;
      }
  </code></pre>
  
  <p>Sorry for the massive amount of code.  How I implemented my service is obviously not correct, I just have no clue where to start to get a service handling the json requests to the server.  Thanks in advance.</p>
  
  <p>To be more clear, this did work on a button click, now I'm trying to get it to all run in the background with the service.  So I guess my question is what goes where in the service?</p>
  
  <p>My activity successfully starts the service, the service would work and print "look at me" to the logcat every minute.  Then I added the <code>try{ sjr.carMake()}</code> and it catches an exception.</p>