Getting XML from webpage

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

Getting XML from webpage

Postby VelcroMan » Thu Nov 12, 2009 2:14 pm

Hi, i'm trying to get the XML/HTML source of a webpage. The only thing printed is "false".

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package se.eclaesson.spelradion;
  3.  
  4.  
  5.  
  6. import android.app.TabActivity;
  7.  
  8.  
  9.  
  10. import org.apache.http.HttpResponse;
  11.  
  12. import org.apache.http.client.HttpClient;
  13.  
  14.  
  15.  
  16. import org.apache.http.client.methods.HttpGet;
  17.  
  18. import org.apache.http.impl.client.DefaultHttpClient;
  19.  
  20.  
  21.  
  22. import android.os.Bundle;
  23.  
  24. import android.widget.TabHost;
  25.  
  26. import android.app.AlertDialog.Builder;
  27.  
  28.  
  29.  
  30. import se.eclaesson.spelradion.HttpHelper.*;
  31.  
  32.  
  33.  
  34. public class Spelradion extends TabActivity {
  35.  
  36.        
  37.  
  38.         HttpClient http;
  39.  
  40.         HttpGet get;
  41.  
  42.         HttpResponse response;
  43.  
  44.        
  45.  
  46.         Builder alertb;
  47.  
  48.        
  49.  
  50.     /** Called when the activity is first created. */
  51.  
  52.     @Override
  53.  
  54.     public void onCreate(Bundle savedInstanceState) {
  55.  
  56.         super.onCreate(savedInstanceState);
  57.  
  58.         setContentView(R.layout.main);
  59.  
  60.  
  61.  
  62.         TabHost mTabHost = getTabHost();
  63.  
  64.        
  65.  
  66.         mTabHost.addTab(mTabHost.newTabSpec("tab_senaste").setIndicator("Senaste").setContent(R.id.LinearLayout01));
  67.  
  68.         mTabHost.addTab(mTabHost.newTabSpec("tab_visaalla").setIndicator("Visa Alla").setContent(R.id.textview2));
  69.  
  70.         mTabHost.addTab(mTabHost.newTabSpec("tab_sok").setIndicator("Sök").setContent(R.id.textview3));
  71.  
  72.        
  73.  
  74.         mTabHost.setCurrentTab(0);
  75.  
  76.        
  77.  
  78.         //Set up the Alert Builder
  79.  
  80.         alertb = new Builder(getBaseContext());
  81.  
  82.         alertb.setTitle("Ett fel har uppstått");
  83.  
  84.        
  85.  
  86.         //Set up HttpClient
  87.  
  88.         http = new DefaultHttpClient();
  89.  
  90.         get = new HttpGet("http://spelradion.libsyn.com/rss");
  91.  
  92.        
  93.  
  94.         try
  95.  
  96.         {
  97.  
  98.                 response = http.execute(get);
  99.  
  100.                 //System.out.print("123");
  101.  
  102.                 System.out.print(HttpHelper.request(response));
  103.  
  104.         }
  105.  
  106.         catch(Exception e)
  107.  
  108.         {
  109.  
  110.                 alertb.setMessage("Kunde inte ansluta till Spelradion.");
  111.  
  112.                 alertb.show();
  113.  
  114.         }
  115.  
  116.        
  117.  
  118.     }
  119.  
  120. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


The HtppHelper class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package se.eclaesson.spelradion.HttpHelper;
  3.  
  4.  
  5.  
  6. import java.io.BufferedReader;
  7.  
  8. import java.io.InputStream;
  9.  
  10. import java.io.InputStreamReader;
  11.  
  12.  
  13.  
  14. import org.apache.http.HttpResponse;
  15.  
  16.  
  17.  
  18. public class HttpHelper {
  19.  
  20.    
  21.  
  22.     public static String request(HttpResponse response){
  23.  
  24.         String result = "";
  25.  
  26.         try{
  27.  
  28.             InputStream in = response.getEntity().getContent();
  29.  
  30.             BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  31.  
  32.             StringBuilder str = new StringBuilder();
  33.  
  34.             String line = null;
  35.  
  36.             while((line = reader.readLine()) != null){
  37.  
  38.                 str.append(line + "\n");
  39.  
  40.             }
  41.  
  42.             in.close();
  43.  
  44.             result = str.toString();
  45.  
  46.         }catch(Exception ex){
  47.  
  48.             result = "Error";
  49.  
  50.         }
  51.  
  52.         return result;
  53.  
  54.     }
  55.  
  56. }
  57.  
  58.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
VelcroMan
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 12, 2009 2:04 pm

Top

Postby vunguyen » Fri Nov 13, 2009 7:28 am

The simple way to get data from the web

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                 String http = "http://www.anddev.org";
  3.  
  4.                 URL url = new URL(http);
  5.  
  6.                 URLConnection uc = url.openConnection();
  7.  
  8.                 HttpURLConnection connection = (HttpURLConnection) uc;
  9.  
  10.  
  11.  
  12.                 InputStream in = connection.getInputStream();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Welcome to talk with me :)
KL&KL
vunguyen
Freshman
Freshman
 
Posts: 6
Joined: Sat Oct 10, 2009 10:42 am

Postby padde » Fri Nov 13, 2009 6:41 pm

I love those one-line-does-it-all stuff :D
You should see my sources since i got my 24" 16/9 TFT :lol:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. InputStream in = DefaultHttpClient().execute(new HttpPost("http://link.to/your.xml")).getEntity().getContent()
  3.  
  4.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


-- Edit --
I would love to have more space in posts too. Is there some kind of trick to get more space?!
The site looks like this |.........|content|...........| (. = empty space/background)
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Similar Problem

Postby baseman68 » Tue Nov 17, 2009 9:25 pm

I am having a problem accessing my API, JSON. When I put in the URL I have issues connecting, On the emulator and device I get "Check you Internet connection"...it is an external URL.

I've allowed internet in the manifest.

I've been told to use the IP address but that doesn't sound like a good fix to the problem.

Any thoughts?
baseman68
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Nov 17, 2009 9:14 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 8 guests