views:

252

answers:

1

Hey,

I've just started developing Android applications. I'm having a little problem with networking. If I run the following code I get an "unknown error" exception message:

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.ClientProtocolException;
import.org.apache.http.client.methods.HttpGet;


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Menu extends Activity {
    /** Called when the activity is first created. */
    @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DefaultHttpClient client = new DefaultHttpClient();

    HttpGet method = new HttpGet("http://www.google.com");

    try {
  client.execute(method);
  TextView t = (TextView) findViewById(R.id.myTextView);
  t.setText("Ok");
 } catch (ClientProtocolException e) {
  TextView t = (TextView) findViewById(R.id.myTextView);
  t.setText(e.getMessage());
 } catch (IOException e) {
  TextView t = (TextView) findViewById(R.id.myTextView);
  t.setText(e.getMessage());
 }

    }
}

I've been lookign up the error and it seems fairly common. It's a problem with DNS resolution on the emulator. However, I can use the browser on the emulator without a problem and visit any website I want. I've also tried replacing the domains with IP addresses without luck.

How can I fix this problem? I'm using Windows Vista and developing in eclipse with the ADT plugin.

Cheers,

Pete

+4  A: 

According to this blog, you need to set the 'INTERNET' permission to your application in your AndroidManifest.xml

ccheneson