Advertisement
jfrubiom

ScreenApp.java

Sep 17th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. package com.newsapp;
  2.  
  3. import java.util.Vector;
  4.  
  5. import org.ksoap2.SoapEnvelope;
  6. import org.ksoap2.serialization.SoapObject;
  7. import org.ksoap2.serialization.SoapSerializationEnvelope;
  8. import org.ksoap2.transport.HttpTransport;
  9. import org.xmlpull.v1.XmlPullParserException;
  10.  
  11. import com.newsapp.ui.TableList;
  12.  
  13. import net.rim.device.api.ui.UiApplication;
  14. import net.rim.device.api.system.DeviceInfo;
  15. import net.rim.device.api.ui.container.MainScreen;
  16.  
  17. public final class ScreenApp extends MainScreen {
  18.     private Vector _data = null;
  19.        
  20.     public ScreenApp() {      
  21.         setTitle("News App");
  22.         this.startLoad();
  23.     }
  24.    
  25.     private void startLoad() {
  26.         String WSD_URL = "http://www.domain.com/service.asmx";
  27.         String WSD_NAMESPACE = "http://www.domain.com/service";
  28.         String WSD_ACTION = "http://www.domain.com/service/GetList";
  29.        
  30.         SoapObject soap = new SoapObject(WSD_NAMESPACE, "GetList");
  31.         int _PIN = DeviceInfo.getDeviceId();
  32.         String PIN = "" + _PIN;
  33.        
  34.         soap.addProperty("deviceId", PIN);
  35.         soap.addProperty("deviceType", "B");
  36.        
  37.         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  38.         envelope.dotNet = true;
  39.         envelope.encodingStyle = SoapSerializationEnvelope.XSD;
  40.         envelope.setOutputSoapObject(soap);
  41.        
  42.         HttpTransport ht = new HttpTransport(WSD_URL);
  43.         ht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  44.        
  45.         try {
  46.                 ht.call(WSD_ACTION, envelope);
  47.                 if(envelope.getResponse() != null) {
  48.                         SoapObject resp = (SoapObject)envelope.getResponse();
  49.                        
  50.                        
  51.                        
  52.                         this.initInterface(resp);
  53.                 }
  54.         } catch (XmlPullParserException ex) {
  55.                
  56.         } catch (Exception e) {
  57.             Item item = new Item("Problem with connection", "Please close app and try again", "");
  58.             this._data = new Vector(1);
  59.             this._data.addElement(item);
  60.            
  61.             TableList list = new TableList(this._data);
  62.             list.setSize(this._data.size());
  63.            
  64.             add(list);    
  65.         }
  66.     }
  67.    
  68.     private void initInterface(SoapObject response) {
  69.         int count = response.getPropertyCount();
  70.         this._data = new Vector(count);
  71.        
  72.         for(int i=0; i<count; i++) {
  73.                 String line = response.getProperty(i).toString();
  74.             String title = this.getAttributeElement(line, "Title");
  75.             String description = this.getAttributeElement(line, "Description");
  76.             String link = this.getAttributeElement(line, "Link");
  77.            
  78.             Item item = new Item(title, description, link);
  79.             this._data.addElement(item);
  80.         }
  81.        
  82.         TableList list = new TableList(this._data);
  83.         list.setSize(this._data.size());
  84.        
  85.         add(list);
  86.     }
  87.    
  88.     private String getAttributeElement(String line, String elem) {
  89.         String attr = line.substring(line.indexOf(elem + "=") + (elem.length() + 1), line.indexOf(";", line.indexOf(elem + "=")));
  90.         return attr;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement