Android SocketException - Information not getting received from server
<p>I am trying to send a server json and get json in return to display in listview.  The listview is taken care of by the refreshViewModels() method.  The json string I'm sending is on this line of code:</p>

<p>outputJSONserv = "{\"to\":\"broadcast\",\"type\":\"1\",\"payload\":"+outputJSON+"}"; where outputJSON is a json string.</p>

<p>When I send this string I do get a string back on one phone but not the other.  I am getting a warning in the logcat that says it is an error that has to do with a SocketException for my output json.  Here are the two logcats to show what is happening and to shoew details about the SocketException:</p>

<p>Logcat from phone1:</p>

<pre><code>I/ActivityManager(  110): Config changed: { scale=1.0 imsi=310/0 loc=en_US touch=3     keys=1/1/2 nav=1/1 orien=1 layout=18 uiMode=17 seq=31}
I/ActivityManager(  110): Starting activity: Intent { act=android.intent.action.MAIN    cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher }
W/InputManagerService(  110): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45ca49c8
I/ActivityManager(  110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.google.android.gm/.ConversationListActivityGmail bnds=[83,240][157,319] }
--------- beginning of /dev/log/main
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:{"from":"65.46.170.6:14842","type":11,"payload":{"message":""}}
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883144 bytes in 216ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 221ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 214ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at oorg.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
D/WindowManager(  110): Home Key Test : false : false
D/PhoneWindow(  423): couldn't save which view has focus because the focused view android.webkit.WebView@45affd60 has no id.
D/Gmail   (  423): com.google.android.gm.HtmlConversationActivity@45b240d0 onPause() finished
I/ActivityManager(  110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher }
W/InputManagerService(  110): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45e77378
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 309ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 82ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:{"from":"65.46.170.6:14842","type":11,"payload":{"message":""}}
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883144 bytes in 116ms
D/dalvikvm( 2263): GC_FOR_MALLOC freed 42 objects / 1252232 bytes in 95ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 215 objects / 2513520 bytes in 93ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm(  198): GC_EXTERNAL_ALLOC freed 3572 objects / 355992 bytes in 102ms
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 144 objects / 1883560 bytes in 616ms
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 26 objects / 626264 bytes in 121ms
I/ActivityManager(  110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.iconnexus.client/.SplashScreen }
D/dalvikvm( 2263): GC_FOR_MALLOC freed 22 objects / 1564120 bytes in 155ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:{"from":"65.46.170.6:14842","type":11,"payload":{"message":""}}
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 449 objects / 3462208 bytes in 89ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm( 2263): GC_FOR_MALLOC freed 131 objects / 1883016 bytes in 195ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON
I/ActivityManager(  110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.iconnexus.client/.SplashScreen }
D/dalvikvm( 2263): GC_FOR_MALLOC freed 135 objects / 1883144 bytes in 233ms
W/System.err( 2263): java.net.SocketException: Socket is closed
W/System.err( 2263):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err( 2263):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err( 2263):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err( 2263):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient( 2263): IN REPEATINGTHREAD-INPUTJSON3:{"from":"65.46.170.6:14842","type":11,"payload":{"message":""}}
</code></pre>

<p>Logcat from phone 2:</p>

<pre><code>E/AndroidClient(25069): Sent dataOutputStream
E/AndroidClient(25069): Before initEventHandlers
E/AndroidClient(25069): After initEventHandlers
E/AndroidClient(25069): Start Repeat Timer
E/AndroidClient(25069): In RepeatingTask()
E/AndroidClient(25069): Before inputJSON String
E/AndroidClient(25069): Hello String:org.apache.harmony.luni.net.SocketInputStream@45a15fb8
E/AndroidClient(25069): Hello String2:
D/dalvikvm(25069): GC_FOR_MALLOC freed 174 objects / 809424 bytes in 94ms
W/System.err(25069): java.net.SocketException: Socket is closed
W/System.err(25069):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err(25069):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err(25069):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err(25069):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err(25069):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON3:
I/ActivityManager(  110): Displayed activity com.iconnexus.client/.AndroidClient: 2234 ms (total 2234 ms)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm(25069): GC_FOR_MALLOC freed 357 objects / 778680 bytes in 198ms
W/System.err(25069): java.net.SocketException: Socket is closed
W/System.err(25069):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err(25069):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err(25069):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err(25069):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err(25069):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON3:
I/WindowManager(  110): home key test1= 1
D/WindowManager(  110): Home Key Test : false : false
D/WindowManager(  110): Home Key Test : false : false
I/ActivityManager(  110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher }
W/InputManagerService(  110): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45e1c7f8
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm(25069): GC_FOR_MALLOC freed 322 objects / 778768 bytes in 182ms
W/System.err(25069): java.net.SocketException: Socket is closed
W/System.err(25069):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err(25069):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err(25069):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err(25069):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err(25069):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON3:
I/ActivityManager(  110): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.jtb.alogcat/.LogActivity }
D/dalvikvm( 1827): GC_FOR_MALLOC freed 8088 objects / 813080 bytes in 104ms
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON
D/dalvikvm(25069): GC_FOR_MALLOC freed 131 objects / 770248 bytes in 186ms
W/System.err(25069): java.net.SocketException: Socket is closed
W/System.err(25069):    at org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:306)
W/System.err(25069):    at java.net.Socket.getOutputStream(Socket.java:515)
W/System.err(25069):    at com.iconnexus.client.AndroidClient.sendBytes(AndroidClient.java:379)
W/System.err(25069):    at com.iconnexus.client.AndroidClient$RepeatingTask.run(AndroidClient.java:499)
W/System.err(25069):    at java.util.Timer$TimerImpl.run(Timer.java:289)
E/AndroidClient(25069): IN REPEATINGTHREAD-INPUTJSON3:  
</code></pre>

<p>onCreate() method:</p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.e(LOG_TAG, "Before OnCreate() Try");
    try {
        Log.e(LOG_TAG, "In OnCreate() Try");
        socket = new Socket("23.23.175.213", 9000); 
        Log.e(LOG_TAG, "Created Socket");
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        Log.e(LOG_TAG, "Created DataOutputStream");
        dataInputStream = new DataInputStream(socket.getInputStream());
        Log.e(LOG_TAG, "Created DataInputStream");

        //out = new OutputStream();
        out = socket.getOutputStream();
        inputStr = socket.getInputStream();

        //Thread readjsonthrd = new Thread(new ReadJSONThread());
        //inputstrrd = new InputStreamReader(socket.getInputStream());

        p = new Profile();
        Log.e(LOG_TAG, "Created Profile Instance");

        //Gets the local profile via JSON and converts into Profile type
        Gson gson = new Gson();
        Log.e(LOG_TAG, "Created Gson Instance" + "GetProfileJSONStr:" + p.getProfileJSONStr());
        p = gson.fromJson(p.getProfileJSONStr(), Profile.class);
        setProfile(p);
        Log.e(LOG_TAG, "Converted Profile to JSON");

        //Gson gson = new Gson();
        Log.e(LOG_TAG, "Before: outputJSON = gson.toJson(p);");
        outputJSON = gson.toJson(p).toString();
        outputJSON = removeExcessStr(outputJSON);
        Log.e(LOG_TAG, "ProfilePicStr Base64:"+p.getProfilePicStr());

        outputJSON = outputJSON.replace("Name","name");
        outputJSON = outputJSON.replace("TagLine","message");
        outputJSON = outputJSON.replace("Title","title");
        outputJSON = outputJSON.replace("Company", "company");
        outputJSON = outputJSON.replace("Industry","industry");
        outputJSON = outputJSON.replace("WhatIDo","whatido");
        outputJSON = outputJSON.replace("WhoDoIWantToMeet","meetwho");
        outputJSON = outputJSON.replace("WHOOZNEAR_PROFILEPIC","photo");
        outputJSON = outputJSON.replaceAll("[c][o][n][t][e][n][t][:][/][/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+[/][a-zA-Z0-9]+", getPicBase64Str()); /*"helloworld2"*/

        if (!outputJSON.contains(",\"photo\":")) { 
            outputJSON = outputJSON.replace("}",",\"photo\":"+"\"IconnexUs\"}");
            outputJSON = outputJSON.replace("}",",\"photo\":"+"\""+getPicBase64Str()+"\"}");
            outputJSON = outputJSON.replace("}",",\"status\":\"enabled\"}");
        }
        else { 
            outputJSON = outputJSON.replace("}",",\"status\":\"enabled\"");
        }

        outputJSONserv = "{\"to\":\"broadcast\",\"type\":\"1\",\"payload\":"+outputJSON+"}";

        Log.e(LOG_TAG, "Created outputJSON:" + outputJSON);
        Log.e(LOG_TAG, "Created outputJSON Server:" + outputJSONserv);
        JSONObject outObject = new JSONObject();
        try {
            outObject.put("photo", "hello");
            outObject.put("type", "50");
            outObject.put("payload", outputJSON);
            outputJSON = gson.toJson(outObject).toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.e(LOG_TAG, "Value of PROFILEPIC STRING FROM PROFILEMAP: "+profileMap.get("WHOOZNEAR_PROFILEPIC"));
        p.setProfilePicStr(ConvertandSetImagetoBase64(profileMap.get("WHOOZNEAR_PROFILEPIC")));
        //String headerJSON = gson.toJson(outObject).toString();
        outputJSON = outputJSON.substring(nthOccurrence(outputJSON, '{', 2)-1, nthOccurrence(outputJSON, '}', 1)-1);

        String input = "["+"Ryan"+"[";
        //"[foo".replaceAll(Pattern.quote("["), "\""); 
        String result = input.replaceAll(Pattern.quote("["), "\"");
        Log.e(LOG_TAG, "REGEX REPLACEALL:"+result);

        outputstrwr = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
        outputstrwr.write(outputJSONserv);

        Log.e(LOG_TAG, "Base64 String:"+p.getProfilePicStr());
        Log.e(LOG_TAG, "Sent dataOutputStream");
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Log.e(LOG_TAG, "Before initEventHandlers");
    initEventHandlers();
    Log.e(LOG_TAG, "After initEventHandlers");
    //refreshViewModels();

    /*Log.e(LOG_TAG, "Start Repeat Thread");
    rt = new Thread(new RepeatingThread());
    //rt = new RepeatingThread();
    rt.start();
    Log.e(LOG_TAG, "Started Repeat Thread");*/

    Log.e(LOG_TAG, "Start Repeat Timer");
    TimerTask task = new RepeatingTask();
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(task, 0, 3000);
    Log.e(LOG_TAG, "Started Repeat Timer");
}
</code></pre>

<p>My Timer Task: </p>

<pre><code>public class RepeatingTask extends TimerTask {
     //private int len = 0; 
     //private byte[] input = new byte[len];

     public RepeatingTask() {
            Log.e(LOG_TAG, "In RepeatingTask()");
            Log.e(LOG_TAG, "Before inputJSON String");

            String hello = "hello world";
            //String inputJSON = getStringFromBuffer(new InputStreamReader(socket.getInputStream()));
            try {
                Log.e(LOG_TAG, "Hello String:"+inputStr);
                inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
                Log.e(LOG_TAG, "Hello String2:"+inputJSON);
                sendBytes(ConvertStringToByteArray(outputJSONserv), 0, ConvertStringToByteArray(outputJSONserv).length);
                Log.e(LOG_TAG, "Hello String3:"+hello);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //Convert 
            Log.e(LOG_TAG, "After inputJSON String:" + inputJSON); 


         //LOOK HERE FIRST  
         //inputJSON is what is received back from the server - Take the inputJSON 
         //String and use regular expressions HERE to remove all the other characters in the 
         //string except the payload JSON.
         //refreshViewModels(inputJSON);
     }

     @Override
     public void run() { 
          /*try {
              Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
              //outputstrwr.write(outputJSONserv);  //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
              //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
              inputJSON = ConvertByteArrayToString(getFileBytes(inputStr));
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }

          Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
          refreshViewModels(inputJSON);*/

      try {
          Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON");
          //outputstrwr.write(outputJSONserv);  //UNCOMMENT IF NEED TO SEND DATA TO GET JSON BACK
          //byte[] = myByteArray = readBytes(inputStr);
          sendBytes(ConvertStringToByteArray(outputJSONserv), 0, ConvertStringToByteArray(outputJSONserv).length);
          //sendBytes(myByteArray, 0, myByteArray.length);
          Log.e(LOG_TAG, "AFTER SENDING DATA");
          //inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
              inputJSON = ConvertByteArrayToString(getBytesFromInputStream(inputStr));
              Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON2:" + inputJSON);
      } 
      catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
      }

      Log.e(LOG_TAG, "IN REPEATINGTHREAD-INPUTJSON3:" + inputJSON);
      refreshViewModels(inputJSON);   //UNCOMMENT THIS AFTER I GET IT WORKING
     }
}
</code></pre>

<p>refreshViewModels() method:</p>

<pre><code>    public void refreshViewModels(String inputJSON) {

        try {
            ListView servicesListView = (ListView) this
                    .findViewById(R.id.profilesListView);

            String[] from = new String[] { "profilePic", "neighborName",
                    "tagLine" };
            int[] to = new int[] { R.id.avatar, R.id.username, R.id.email };

            // prepare the list of all records
            List&lt;HashMap&lt;String, Object&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;();
            List&lt;Profile&gt; profiles = new ArrayList&lt;Profile&gt;();

            // Clear the position mapping list and reset it
            this.posMap.clear();
            Log.i(LOG_TAG, "NEW inputJSON: " + inputJSON);
            inputJSON = getPayloadStr(inputJSON);
            Log.i(LOG_TAG, "NEW inputJSON2: " + inputJSON);
            JSONArray profileArray = new JSONArray(inputJSON);

            for (int i=0; i&lt;profileArray.length(); i++) { 
                JSONObject jsonObject = profileArray.getJSONObject(i);
                Gson gson = new Gson(); 
                Profile p = gson.fromJson(gson.toJson(jsonObject).toString(), Profile.class);
                profiles.add(p);
            }

            int pos = 0;

            // Creates the fillMaps list for the listAdapter
            Log.i(LOG_TAG, "Profiles size: " + profiles.size());
            //showToast("Profiles size: " + profiles.size());
            for (Profile p : profiles) {
                // Create mapping for list adapter
                HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
                map.put("profilePic",
                        p.getAttributeValue("photo")== null? "Not Set" : p
                                .getAttributeValue("photo"));
                map.put("neighborName",
                        p.getAttributeValue("Name") == null? "Not Set" : p
                                .getAttributeValue("Name"));
                map.put("tagLine",
                        p.getAttributeValue("TagLine") == null? "Not Set" : p
                                .getAttributeValue("TagLine"));
                fillMaps.add(map);

                // Reset the postion mapping
                this.posMap.put(pos++, p);

            }

            ListAdapter servicesListAdapter = new myAdapter(this, fillMaps,
                    R.layout.listitem, from, to);
            servicesListView.setAdapter(servicesListAdapter);

        } catch (Exception e) {
            Log.e(LOG_TAG, "Error making list adapter: " + e.getMessage());
        }

    }
</code></pre>

<p>Here are my conversions methods:</p>

<pre><code>public String ConvertByteArrayToString(byte[] b) { 
    // byte[] to string
    String input = new String(b);
    return input;
}

public byte[] ConvertStringToByteArray(String str) { 
    // string to byte[]
    byte[] bytes = str.getBytes();
    return bytes;
}
</code></pre>

<p>Here are the methods I'm using to send and receive json strings:</p>

<pre><code>public void sendBytes(byte[] myByteArray, int start, int len) throws IOException {
    if (len &lt; 0)
        throw new IllegalArgumentException("Negative length not allowed");
    if (start &lt; 0 || start &gt;= myByteArray.length)
        throw new IndexOutOfBoundsException("Out of bounds: " + start);
    // Other checks if needed.

    // May be better to save the streams in the support class;
    // just like the socket variable.

    OutputStream out = socket.getOutputStream(); 
    DataOutputStream dos = new DataOutputStream(out);

    dos.writeInt(len);
    if (len &gt; 0) {
        dos.write(myByteArray, start, len);
    }

    //OutputStream out = socket.getOutputStream(); //COMMENTED THIS OUT

    //DataOutputStream dos = new DataOutputStream(out); //COMMENTED THIS OUT

    /*dataOutputStream.writeInt(len);    //COMMENTED THIS OUT
    if (len &gt; 0) {
        dataOutputStream.write(myByteArray, start, len);
    }*/
}

public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
        // Get the size of the file
        int length = is.available();

        if (length &gt; Integer.MAX_VALUE) {
        // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int) length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset &lt; bytes.length
        &amp;&amp; (numRead = is.read(bytes, offset, bytes.length - offset)) &gt;= 0) {
        offset += numRead;
        }
        // Ensure all the bytes have been read in
        if (offset &lt; bytes.length) {
        throw new IOException("Could not completely stream ");
        }
        // Close the input stream and return bytes
        is.close();
        return bytes;
}
</code></pre>

<p>Any help or pointers in the right direction would be appreciated.  Any help via chat would be greatly appreciated too.  Whole file could be posted or viewed in chat session if needed.</p>
