2012-03-19

Practice Midterm - Problem 8 and 10.

Originally Posted By: mkwan
Group: Michael, Gayathri, Sayali, Amruta

Problem 8 solution:
1. Android:Code:  Log.i("TooSmallApp", "It's cramped in here");
2. iOS: Code: NSLog(@"TooSmallApp: It's cramped in here");

Problem 10 Android solution:
1. Add the following to AndroidManifest.xml:
Code:

2. Then create this class:
Code: public class MailTestActivity extends Activity {
   private final static String MAIL_COMMAND = "HELO my.mailserver.org";
   private final static String DESTINATION_ADDRESS = "my.mailserver.org";   
   private final static int DESTINATION_PORT = 25;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Socket connection = null;
        DataOutputStream outputStream = null;
       
        // This is needed for SDK 9 and above when opening sockets in the main thread.
        ThreadPolicy tp = ThreadPolicy.LAX;
        StrictMode.setThreadPolicy(tp);       
       
        try {
         connection = new Socket(DESTINATION_ADDRESS, DESTINATION_PORT);
         outputStream = new DataOutputStream(connection.getOutputStream());
         outputStream.writeUTF(MAIL_COMMAND);
      } catch (UnknownHostException e) {
         Log.d("MailTest", "Unable to connect to " + DESTINATION_ADDRESS + ":" + DESTINATION_PORT);
         e.printStackTrace();
      } catch (IOException e) {
         Log.d("MailTest", "Unable to get IO stream");
         e.printStackTrace();
      } finally
      {      
         try {
            outputStream.close();
            connection.close();
         } catch (IOException e) {
            e.printStackTrace();
         }         
      }       
    }
}
'''Originally Posted By: mkwan''' Group: Michael, Gayathri, Sayali, Amruta<br><br>Problem 8 solution:<br>1. Android:Code: &nbsp;Log.i(&quot;TooSmallApp&quot;, &quot;It's cramped in here&quot;);<br>2. iOS: Code: NSLog(@&quot;TooSmallApp: It's cramped in here&quot;);<br><br>Problem 10 Android solution:<br>1. Add the following to AndroidManifest.xml:<br>Code: <br><br>2. Then create this class:<br>Code: public class MailTestActivity extends Activity {<br>&nbsp; &nbsp;private final static String MAIL_COMMAND = &quot;HELO my.mailserver.org&quot;;<br>&nbsp; &nbsp;private final static String DESTINATION_ADDRESS = &quot;my.mailserver.org&quot;;&nbsp; &nbsp;<br>&nbsp; &nbsp;private final static int DESTINATION_PORT = 25;<br>&nbsp; &nbsp; /** Called when the activity is first created. */<br>&nbsp; &nbsp; @Override<br>&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState) {<br>&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<br>&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.main);<br>&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; Socket connection = null;<br>&nbsp; &nbsp; &nbsp; &nbsp; DataOutputStream outputStream = null;<br>&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; // This is needed for SDK 9 and above when opening sockets in the main thread.<br>&nbsp; &nbsp; &nbsp; &nbsp; ThreadPolicy tp = ThreadPolicy.LAX;<br>&nbsp; &nbsp; &nbsp; &nbsp; StrictMode.setThreadPolicy(tp);&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; try {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;connection = new Socket(DESTINATION_ADDRESS, DESTINATION_PORT);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;outputStream = new DataOutputStream(connection.getOutputStream());<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;outputStream.writeUTF(MAIL_COMMAND);<br>&nbsp; &nbsp;&nbsp; &nbsp;} catch (UnknownHostException e) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Log.d(&quot;MailTest&quot;, &quot;Unable to connect to &quot; + DESTINATION_ADDRESS + &quot;:&quot; + DESTINATION_PORT);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.printStackTrace();<br>&nbsp; &nbsp;&nbsp; &nbsp;} catch (IOException e) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Log.d(&quot;MailTest&quot;, &quot;Unable to get IO stream&quot;);<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.printStackTrace();<br>&nbsp; &nbsp;&nbsp; &nbsp;} finally<br>&nbsp; &nbsp;&nbsp; &nbsp;{&nbsp; &nbsp;&nbsp; &nbsp;<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;try {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;outputStream.close();<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;connection.close();<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;} catch (IOException e) {<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;e.printStackTrace();<br>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br>&nbsp; &nbsp;&nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; }<br>}<br>
X