Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old January 1st, 2012, 05:16 AM   #1 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 2
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Android: Thread Exiting with uncaught exception while using httpUrlConnection (Android 4.0.3)

My PC is connected to the LAN through proxy. I have set the proxy in emulator too. But when I am trying to use HttpURLConnection, the app crashes and the log says "Thread exiting with uncaught exception" .

Please have a look at my Code. The activity creates a button and textview. On clicking the button it starts an async task which fetches the contents of a URL using HttpURLConnection and updates the textview with the response code.
Code:
public class GWRSSReader extends Activity {
	TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button=(Button)findViewById(R.id.button1);
        tv=(TextView)findViewById(R.id.textView1);
    button.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			DownloadWebPageTask task = new DownloadWebPageTask();
			task.execute();
		}
	});
    }
    private class DownloadWebPageTask extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void...voids) {
                String code="";
			try{
				SocketAddress sa=new InetSocketAddress("ps1.iiit-bh.ac.in",8080);
				Proxy p=new Proxy(Proxy.Type.HTTP,sa);
		   URL url = new URL("http://google.com");
		   HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(p);
	       int response = httpURLConnection.getResponseCode(); // this line crashes the app
               code=String.valueOf(response);
		   }
		   catch(Exception e){Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG);}
		   
			return code;
		}

		@Override
		protected void onPostExecute(String result) {
			tv.setText(result);
		}
	}
    
    
}
Where am I doing wrong? One more question: Am I setting the proxy in the correct way?

When I remove the line "int response = httpURLConnection.getResponseCode();" the app works. But if I add it the app crashes.

Any help will be highly appreciated.

Regards!

Sandeep92 is offline  
Reply With Quote
Sponsors
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:01 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo