1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by Sandeep92, Jan 1, 2012.

  1. Sandeep92

    Sandeep92 Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    2
    Joined:
    Jan 1, 2012

    Jan 1, 2012
    2
    0
    25
    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 (Text):
    1.  
    2. public class GWRSSReader extends Activity {
    3.     TextView tv;
    4.     /** Called when the activity is first created. */
    5.     @Override
    6.     public void onCreate(Bundle savedInstanceState) {
    7.         super.onCreate(savedInstanceState);
    8.         setContentView(R.layout.main);
    9.         Button button=(Button)findViewById(R.id.button1);
    10.         tv=(TextView)findViewById(R.id.textView1);
    11.     button.setOnClickListener(new View.OnClickListener() {
    12.        
    13.         public void onClick(View v) {
    14.             DownloadWebPageTask task = new DownloadWebPageTask();
    15.             task.execute();
    16.         }
    17.     });
    18.     }
    19.     private class DownloadWebPageTask extends AsyncTask<Void, Void, String> {
    20.         @Override
    21.         protected String doInBackground(Void...voids) {
    22.                 String code="";
    23.             try{
    24.                 SocketAddress sa=new InetSocketAddress("ps1.iiit-bh.ac.in",8080);
    25.                 Proxy p=new Proxy(Proxy.Type.HTTP,sa);
    26.            URL url = new URL("http://google.com");
    27.            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(p);
    28.            int response = httpURLConnection.getResponseCode(); // this line crashes the app
    29.                code=String.valueOf(response);
    30.            }
    31.            catch(Exception e){Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG);}
    32.            
    33.             return code;
    34.         }
    35.  
    36.         @Override
    37.         protected void onPostExecute(String result) {
    38.             tv.setText(result);
    39.         }
    40.     }
    41.    
    42.    
    43. }
    44.  
    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!
     

    Advertisement

Share This Page

Loading...