Advertisement
Guest User

JAVA SOURCE GETXMLFROMURL

a guest
Jan 18th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9.  
  10. import android.app.Activity;
  11. import android.app.ProgressDialog;
  12. import android.os.AsyncTask;
  13. import android.os.Bundle;
  14. import android.view.Menu;
  15.  
  16. public class MainActivity extends Activity {
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.        
  23.         //Initializing the globals
  24.         Globals.mainActivity = this;
  25.         Globals.ip = "192.168.178.53:9001";
  26.         if(Globals.shopCon == null){
  27.             final ProgressDialog p = ProgressDialog.show(MainActivity.this, "", "Loading...");
  28.  
  29.            
  30.             class initConnection extends AsyncTask<Void, Void, Void>{
  31.                 protected Void doInBackground(Void... nothing){
  32.                     Globals.shopCon=new ShopConnection("http://" + Globals.ip + "/ws410/rest/");
  33.                    
  34.                     p.dismiss();
  35.                     return null;                   
  36.                 }
  37.             }
  38.            
  39.             new initConnection().execute();
  40.         }
  41.     }
  42.     public String getXmlFromUrl(String sUrl){
  43.         InputStream is = null;
  44.         URLConnection conn = null;
  45.         URL pURL = null;
  46.         InputStreamReader isr = null;
  47.         BufferedReader in  = null;
  48.        
  49.         String strFileContents = new String("");
  50.         try{
  51.             String user = "admin:nimda";
  52.             String base64 = Globals.Base64(user);
  53.             pURL = new URL(sUrl);
  54.             if(pURL != null) conn = pURL.openConnection();
  55.             if(conn != null){
  56.                 conn.setRequestProperty("Authorization", base64);
  57.                  is = conn.getInputStream();
  58.             }
  59.  
  60.             if(is != null) isr = new InputStreamReader(is);
  61.             if(isr != null) in=new BufferedReader(isr);
  62.            
  63.             if(in != null){
  64.                 String data = new String("");
  65.                 while((data = in.readLine()) != null)
  66.                 {
  67.                     strFileContents += data;
  68.                 }
  69.                 in.close();
  70.             }
  71.         }catch(MalformedURLException e){
  72.             e.printStackTrace();
  73.         }catch(IOException e){
  74.             e.printStackTrace();
  75.         }
  76.         return strFileContents;
  77.     }
  78.     @Override
  79.     public boolean onCreateOptionsMenu(Menu menu) {
  80.         // Inflate the menu; this adds items to the action bar if it is present.
  81.         getMenuInflater().inflate(R.menu.activity_main, menu);
  82.         return true;
  83.     }
  84.  
  85. }
  86.  
  87.  
  88. ___________________________________________________________________________________________________________
  89. NEW FILE; NEW FILE; NEW FILE
  90. Globals.java, class Globals, only included is the function that is named in my source...
  91. _______________________________________
  92.     static public String Base64(String clear){
  93.         String crypt = new String("");
  94.         byte[] byteArray = null;
  95.         try {
  96.             byteArray = clear.getBytes("UTF-16");
  97.         } catch (UnsupportedEncodingException e) {
  98.             e.printStackTrace();
  99.         }
  100.         if(byteArray !=null) crypt = Base64.encodeToString(byteArray, Base64.DEFAULT);
  101.         return crypt;
  102.     }
  103. _________________________________________
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement