HttpURLConnection won't connect?

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

HttpURLConnection won't connect?

Postby danhass » Mon Jan 18, 2010 9:23 am

So this should be a really, really simple task. I'm just trying to GET a webpage. I set up a test page that just has the string "Teh test data". The url is embedded in the java code, and the forum rules won't allow me to post an actual link. /shrug.


Here's the code. It always generates the "Error Connecting".

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package promotxts.com.promotxts;
  3.  
  4.  
  5.  
  6. import android.os.Bundle;
  7.  
  8. import android.widget.TextView;
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.view.View;
  15.  
  16. import android.widget.Button;
  17.  
  18.  
  19.  
  20. import java.io.IOException;
  21.  
  22. import java.io.InputStream;
  23.  
  24. import java.io.InputStreamReader;
  25.  
  26. import java.net.HttpURLConnection;
  27.  
  28. import java.net.URL;
  29.  
  30. import java.net.URLConnection;
  31.  
  32. import java.util.Date;
  33.  
  34.  
  35.  
  36. public class Launch extends Activity implements View.OnClickListener{
  37.  
  38.         /** Called when the activity is first created. */
  39.  
  40.         Button btn;
  41.  
  42.         Button btn2;
  43.  
  44.         @Override
  45.  
  46.         public void onCreate(Bundle savedInstanceState) {
  47.  
  48.         super.onCreate(savedInstanceState);
  49.  
  50.         btn=new Button(this);
  51.  
  52.         btn.setOnClickListener(this);
  53.  
  54.         btn2=new Button(this);
  55.  
  56.         updateTime();
  57.  
  58.         setContentView(btn);
  59.  
  60.         }
  61.  
  62.  
  63.  
  64.         public void onClick(View view)
  65.  
  66.         {
  67.  
  68.           //updateTime();
  69.  
  70.           System.exit(0);
  71.  
  72.         }
  73.  
  74.  
  75.  
  76.         public void updateTime()
  77.  
  78.         {
  79.  
  80.         String data=new Date().toString();
  81.  
  82.         try
  83.  
  84.         {
  85.  
  86.                 InputStream in = null;
  87.  
  88.                 URL url = new URL("http://www.promotxts.com/getdata.html");
  89.  
  90.         URLConnection conn = url.openConnection();
  91.  
  92.  
  93.  
  94.         if (!(conn instanceof HttpURLConnection))                    
  95.  
  96.             throw new IOException("Not an HTTP connection");
  97.  
  98.        
  99.  
  100.         try{
  101.  
  102.             HttpURLConnection httpConn = (HttpURLConnection) conn;
  103.  
  104.             httpConn.setAllowUserInteraction(false);
  105.  
  106.             httpConn.setInstanceFollowRedirects(true);
  107.  
  108.             httpConn.setRequestMethod("GET");
  109.  
  110.             httpConn.connect();
  111.  
  112.         }
  113.  
  114.         catch (Exception ex)
  115.  
  116.         {
  117.  
  118.             throw new IOException("Error connecting");            
  119.  
  120.         }
  121.  
  122.         }catch (IOException e1) {
  123.  
  124.         // TODO Auto-generated catch block
  125.  
  126.         data = e1.getMessage();
  127.  
  128.     }
  129.  
  130.  
  131.  
  132.         btn.setText("Hello World by Dan:\n " + data);
  133.  
  134.         }
  135.  
  136. }
  137.  
  138.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


I just can't believe that it is this hard to retrieve a webpage in an Android app. It really doesn't bode well for it as a platform. I know that having spent the better part of two days just trying to retrieve a simple webpage, I'm thinking that the mobile windows framework may be a much better choice for my mobile apps.
danhass
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 18, 2010 9:02 am

Top

Postby danhass » Mon Jan 18, 2010 10:07 am

Ok. Another hours of searching and I found the solution via some other posts. One must give the Android app permission to access the internet. To do so (in Eclipse), one must open the Android Manifest and use the permissions tab to give permission to use INTERNET. Maybe this will save some other new Android developer some time.

So up next on the learning curve... How to respond to incoming SMS messages!!!
danhass
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 18, 2010 9:02 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 6 guests