• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android: Thread Exiting with uncaught exception while using httpUrlConnection (Android 4.0.3)

Sandeep92

Lurker
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!
 
Back
Top Bottom