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

Apps How to go about getting data from ASP.NET webpage?

Valten1992

Newbie
I am planning on designing an app that will take certain bits of info from a webpage and display it on the app. Problem is that the website was made in ASP.NET.

I have had some experience getting data from xml files and the like but im sure it must be quite different for ASP. Everywhere I research seems to tell me a different way of doing it, can someone point me in the right direction?

EDIT:To be more precise I want the text elements of a page
 
Accessing ASP and PHP pages are done relatively similarly. Here is an example of how to do that:


Code:
//url parameter is the URL to the asp script file (not including form entity)
public void readTextElements(String url) throws IOException
{
         HttpClient client = new DefaultHttpClient();
         HttpPost post =  new HttpPost(url);         //Can also use HttpGet depending on yourr requirements
         ArrayList<NameValuePair> attr = new ArrayList<NameValuePair>();
         
         attr.add(new BasicNameValuePair( "name1", "value1");
         attr.add(new BasicNameValuePair("name2", "value2");
        //add more name value pairs if needed. These represent the entity of the asp request (placed after the '?' in the url)
 
         post.setEntity(new UrlEncodedFormEntity(attr));
         
         HttpResponse response = client.execute(post);

         //Do whatever you need to do with the reposne here. It will contain the reply from the asp script
}

For example, if your script outputs some text, you could do something like this:

Code:
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getInputStream()));

String tmp;
String result = "";

while((tmp = br.readLine()) != null)
        result += tmp + "\n";

//result contains the returned string of text. do w/e you need with it.
 
Hmm weird
If I have the right idea, I want something like this

Code:
public void readTextElements(String url) throws IOException
      {
          try
             {    
               client = new DefaultHttpClient();
                post =  new HttpPost(url);         //Can also use HttpGet depending on your requirements
               
               ArrayList<NameValuePair> attr = new ArrayList<NameValuePair>();
               attr.add(new BasicNameValuePair( "pagename", "news"));
               attr.add(new BasicNameValuePair( "pageid", "5"));
              //add more name value pairs if needed. These represent the entity of the asp request (placed after the '?' in the url)
       
               post.setEntity(new UrlEncodedFormEntity(attr));
               
               HttpResponse response = client.execute(post);
         
               //response actions
             
               br = new BufferedReader(new InputStreamReader(((URLConnection) response.getEntity()).getInputStream()));
               
             
               
               String tmp;
               String result = "";

               
               while((tmp = br.readLine()) != null)
                       result += tmp + "\n";

               t1.setText(result);
               }
               catch(Exception e)
               {
                  Log.e("ERROR", e.getMessage());
               }
with the method call being

Code:
readTextElements("http://www.brigids.ie/aspfiles/engine.asp?");
I get a weird error where the message displayed on the log is just my url minus the "
http:// and/aspfiles onward, where have I gone wrong? i havent really done ASP for long.
 
The log will be from you catching an exception in the catch block at the end of the method. Unfortunately you aren't logging enough information.

Changed your catch to this and you will get the full exception details.
Code:
catch (IOException e)
{
  Log.e(
      this.getClass().getSimpleName(),
      "I/O exception swallowed in readTextElements(String)",
      e
    );
}
 
Back
Top Bottom