Search This Blog

Showing posts with label networking. Show all posts
Showing posts with label networking. Show all posts

14 April 2012

Unknown Host Exception

I spent an hour trying to figure out why my little 1-page test app was throwing an UnknownHostException but the browser was working. To make matters worse, there was a red herring in that I could not ping from adb shell.

Turns out it was a stupid ommission. If you are having this problem, don't forget to add this to your AndroidManifest:

<uses-permission android:name="android.permission.INTERNET"/>

07 July 2010

Error reporting via email

Let's start with a basic app:
C:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml
2: http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml -> galatea-archetype (null)
Choose a number:  (1/2/3): 2
Confirm properties configuration:
groupId: org.eoti.android.errors
artifactId: ErrorTest
version: 1.0-SNAPSHOT
package: org.eoti.android.errors
C:\work> cd ErrorTest
C:\work\ErrorTest> mvn clean install

Insert this into the end of your onCreate method:
[* NOTE: Make sure to change to your email address if you want to see it actually work ]
        try{
            throw new IOException("This is an error");
        } catch (IOException e) {
            Log.e(TAG, "Standard Error Logging", e);

            // Derived from http://thinkandroid.wordpress.com/2010/01/25/debugging-applications-by-emailing-error-reports/
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"malachid@gmail.com"});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "[" + getClass().getSimpleName() + "] ERROR REPORT");
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, sw.toString());
            startActivity(Intent.createChooser(emailIntent, "Send error report..."));
        }

Redeploy (mvn clean install).and run it.  You should soon have an email show the stack trace.

18 June 2010

Phone ID and Other Useful Tidbits

Let's start with a basic project:

F:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml
2: http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml -> galatea-archetype (null)
groupId: org.eoti.android.phone
artifactId: PhoneIDTest
version: 1.0-SNAPSHOT
package: org.eoti.android.phone


F:\work> cd PhoneIDTest
F:\work\PhoneIDTest> mvn clean install

Add this to your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Add this convenience method to your PhoneIDTestActivity:
    private void showMessage(String msg)
    {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

Insert this into the end of your onCreate method:
        TelephonyManager mgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        switch(mgr.getPhoneType())
        {
            case TelephonyManager.PHONE_TYPE_CDMA:
                showMessage("CDMA");
                break;
            case TelephonyManager.PHONE_TYPE_GSM:
                showMessage("GSM");
                break;
            case TelephonyManager.PHONE_TYPE_NONE:
                showMessage("NONE");
                break;
            default:
                showMessage("Unknown phone type");
        }

        showMessage("Device ID: " + mgr.getDeviceId());
        showMessage("Version: " + mgr.getDeviceSoftwareVersion());
        showMessage("Number: " + mgr.getLine1Number());
        showMessage("Country: " + mgr.getNetworkOperatorName());
        showMessage("Sim Serial#: " + mgr.getSimSerialNumber());
        showMessage("Subscriber ID: " + mgr.getSubscriberId());
        showMessage("Operator: " + mgr.getSimOperatorName());
       
Redeploy (mvn clean install) and run the app.

12 May 2010

Grab content from a URL

First we create a basic project:

    F:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml

choose the galatea-archetype plugin
groupId: org.eoti.android
artifactId: URLTest
version: 1.0-SNAPSHOT


    F:\work> cd URLTest
    F:\work\URLTest> mvn clean install

Add this to your AndroidManifest.xml:   
    <uses-permission android:name="android.permission.INTERNET" />

And here's the code for your activity (rename as needed):

public class URLTestActivity extends Activity {
    private Handler handler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                Toast.makeText(URLTestActivity.this, "Time: " + msg.obj, Toast.LENGTH_SHORT).show();
            }
        };
        Thread t = new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL("http://tycho.usno.navy.mil/cgi-bin/timer.pl");
                    URLConnection connection = url.openConnection();
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String line = "";
                    String s = "";
                    while( (line = in.readLine()) != null )
                    {
                        int idx = line.indexOf("Pacific Time");
                        if( idx != -1)
                        {
                            s = line.substring(4, idx-1).trim(); // strip off "<BR>" and "Pacific Time"
                        }
                    }
                    Message msg = new Message();
                    msg.obj = s;
                    handler.sendMessage(msg);
                } catch (Exception e) {
                    Toast.makeText(URLTestActivity.this, "ERROR: " + e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        };
        t.start();
    }
}

Redeploy (mvn clean install) and launch the app. You should see the time (Pacific Time) displayed as a popup.