readUTF() causing Android app to hang
<p>I'm trying to compile the code at <a href="http://android-er.blogspot.com/2011/01/simple-communication-using.html" rel="nofollow">this</a> link.</p>

<p>The code isn't working because it is getting hung up on this line:</p>

<pre><code>textIn.setText(dataInputStream.readUTF());
</code></pre>

<p>For some reason it doesn't get hung up on <code>writeUTF()</code> but it gets hung up on <code>readUTF()</code>.  </p>

<p>Can anyone point me in the right direction here? </p>

<p>Here is my code:</p>

<pre><code>public Socket socket = null;
public DataOutputStream dataOutputStream = null;
public DataInputStream dataInputStream = null;
public Thread readjsonthrd = new Thread(new ReadJSONThread());

private final static String LOG_TAG = AndroidClient.class.getSimpleName();
private final Handler handler = new Handler();

@Override
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");

        Profile 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");
        p = gson.fromJson(p.getProfileJSONStr(), Profile.class);
        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);
        Log.e(LOG_TAG, "Created outputJSON");

        dataOutputStream.writeUTF(outputJSON); //OUTPUT OF JSON FROM LOCAL PROFILE BEING SENT TO THE SERVER
        dataOutputStream.flush();
        Log.e(LOG_TAG, "Created 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, "Create Thread");
    Thread serverthrd = new Thread(new ServerThread());

    Log.e(LOG_TAG, "Start Thread");
    serverthrd.start();
    Log.e(LOG_TAG, "Started Thread");
}

public class ServerThread implements Runnable { 
    @Override
    public void run() {
        // TODO Auto-generated method stub
    //Socket socket = null;
    //DataOutputStream dataOutputStream = null;
    //DataInputStream dataInputStream = null;
        Log.e(LOG_TAG, "Before Server Try Statement");
    try {
        Log.e(LOG_TAG, "In Server Try Statement");
        //socket = new Socket("23.23.175.213", 1337);

        //dataOutputStream = new DataOutputStream(socket.getOutputStream());
        //dataInputStream = new DataInputStream(socket.getInputStream());
        /*Profile p = null;

        //Gets the local profile via JSON and converts into Profile type
        Gson gson = new Gson();
        p = gson.fromJson(p.getProfileJSONStr(), Profile.class);

        //Gson gson = new Gson();
        outputJSON = gson.toJson(p);
        dataOutputStream.writeUTF(outputJSON); //OUTPUT OF JSON FROM LOCAL PROFILE BEING SENT TO THE SERVER
        */
        //dataOutputStream.writeUTF(textOut.getText().toString());  //OUTPUT JSON GOES HERE
        //textIn.setText(dataInputStream.readUTF());


        Log.e(LOG_TAG, "Start Thread");
        readjsonthrd.start();
        Log.e(LOG_TAG, "Started Thread");
        /*Log.e(LOG_TAG, "Before inputJSON String");
        inputJSON = dataInputStream.readUTF();

        //Convert 
        Log.e(LOG_TAG, "After inputJSON String");
        Log.e(LOG_TAG, "InputJSON:" + inputJSON);*/

        //textIn.setText(dataInputStream.readUTF());  //GET JSON COMING FROM SERVER HERE
        refreshViewModels();
    } /*catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }*/
    finally{
        if (socket != null){
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (dataOutputStream != null){
            try {
                dataOutputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (dataInputStream != null){
            try {
                dataInputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }


    }
}

public class ReadJSONThread implements Runnable { 
    @Override
    public void run() {
        // TODO Auto-generated method stub
    //Socket socket = null;
    //DataOutputStream dataOutputStream = null;
    //DataInputStream dataInputStream = null;
        Log.e(LOG_TAG, "Before Read JSON Try Statement");
        try {
            Log.e(LOG_TAG, "Before inputJSON String");
            inputJSON = dataInputStream.readUTF();

            //Convert 
            Log.e(LOG_TAG, "After inputJSON String");
        }
        catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
</code></pre>
