Guest User

Untitled

a guest
Sep 19th, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. package com.androidpeople.xml.parsing;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.HttpVersion;
  9. import org.apache.http.client.ClientProtocolException;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.apache.http.params.BasicHttpParams;
  13. import org.apache.http.params.HttpParams;
  14. import org.apache.http.params.HttpProtocolParams;
  15. import org.xml.sax.Attributes;
  16. import org.xml.sax.SAXException;
  17. import org.xml.sax.helpers.DefaultHandler;
  18.  
  19. import android.util.Base64;
  20.  
  21. public class MyXMLHandler extends DefaultHandler {
  22.  
  23.         public static String getnsdata_url(String url) {
  24.                         BufferedReader input = null;
  25.                 String output = "Geen data";
  26.                 DefaultHttpClient client = new DefaultHttpClient();
  27.                 HttpParams params = new BasicHttpParams();
  28.                 HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  29.                 HttpProtocolParams.setContentCharset(params, "UTF_8");
  30.                 HttpProtocolParams.setUseExpectContinue(params, false);
  31.                 client.setParams(params);
  32.                 HttpGet get = new HttpGet(url);
  33.                 String credentials = getCredentials();
  34.                 get.addHeader("Authorization","Basic " +credentials);
  35.                 try {
  36.                         HttpResponse response;
  37.                                 try {
  38.                                         response = client.execute(get);
  39.                         input = new BufferedReader (new
  40.         InputStreamReader(response.getEntity().getContent()));
  41.                         StringBuffer sb = new StringBuffer("");
  42.                         String line = "";
  43.                         String NL = System.getProperty("line.separator");
  44.                         while ((line = input.readLine()) != null) {
  45.                                         sb.append(line + NL);
  46.                         }
  47.                         input.close();
  48.                         output = sb.toString();
  49.                                 } catch (ClientProtocolException e) {
  50.                                         e.printStackTrace();
  51.                                 } catch (IOException e) {
  52.                                         e.printStackTrace();
  53.                                 }
  54.                 } finally {
  55.                         if (input != null) {
  56.                                 try {
  57.                                         input.close();
  58.                                 } catch (IOException e) {
  59.                                         e.printStackTrace();
  60.                                 }
  61.                         }
  62.                 }
  63.              return output;
  64.             }
  65.        
  66.         public static String getCredentials() {
  67.                 String username = "carlogeertse@student.ru.nl";
  68.                 String password = "BHgXyMXqPt8wJNzsx2Y_5ijdOS07Sjp9ffbw5NO664qv9k5X4mO-kg";
  69.                 String source=username+":"+password;
  70.                 String
  71.         ret=Base64.encodeToString(source.getBytes(),Base64.URL_SAFE|
  72.         Base64.NO_WRAP);
  73.                 return ret;
  74.             }
  75.        
  76.     //end of ns//
  77.     /*Boolean currentElement = false;
  78.     String currentValue = null;
  79.     public static SitesList sitesList = null;
  80.  
  81.     public static SitesList getSitesList() {
  82.         return sitesList;
  83.     }
  84.  
  85.     public static void setSitesList(SitesList sitesList) {
  86.         MyXMLHandler.sitesList = sitesList;
  87.     }
  88.  
  89.     /** Called when tag starts ( ex:- <name>AndroidPeople</name>
  90.      * -- <name> )*/
  91.     //@Override
  92.     /*public void startElement(String uri, String localName, String qName,
  93.             Attributes attributes) throws SAXException {
  94.  
  95.         currentElement = true;
  96.  
  97.         if (localName.equals("maintag"))
  98.         {
  99.             /** Start */
  100.             /*sitesList = new SitesList();
  101.         } else if (localName.equals("website")) {
  102.             /** Get attribute value */
  103.             /*String attr = attributes.getValue("category");
  104.             sitesList.setCategory(attr);
  105.         }
  106.  
  107.     }
  108.  
  109.     /** Called when tag closing ( ex:- <name>AndroidPeople</name>
  110.      * -- </name> )*/
  111.     /*@Override
  112.     /*public void endElement(String uri, String localName, String qName)
  113.             throws SAXException {
  114.  
  115.         currentElement = false;
  116.  
  117.         /** set value */
  118.         /*if (localName.equalsIgnoreCase("name"))
  119.             sitesList.setName(currentValue);
  120.         else if (localName.equalsIgnoreCase("website"))
  121.             sitesList.setWebsite(currentValue);
  122.  
  123.     }
  124.  
  125.     /** Called to get tag characters ( ex:- <name>AndroidPeople</name>
  126.      * -- to get AndroidPeople Character ) */
  127.     /*@Override
  128.     public void characters(char[] ch, int start, int length)
  129.             throws SAXException {
  130.  
  131.         if (currentElement) {
  132.             currentValue = new String(ch, start, length);
  133.             currentElement = false;*/
  134.         //}
  135.  
  136.     }
  137.  
  138. //}
Add Comment
Please, Sign In to add comment