2011-09-22 57 views
0

我真的很喜歡Android,所以有很多令我困惑的事情。我已經看過了100個教程和如何從Android上的Web服務獲取信息的例子,但我需要的是一個沒有線索的人。這裏有兩件事情特別是我沒有變:需要一個簡單的教程android/webservice的工作?

  • 我不知道如何處理XML文件..意思做,一旦我做了Java的工作,是所有需要被做了什麼?或者是否需要在XML文件中更改任何內容?
  • 好像也許我應該創建一些教程的一個新的類,但我不知道,如果是的話,我不知道,一旦我做了類
  • 做什麼我想以JSON格式檢索信息。就目前來說,只要我能夠得到那些很好的信息,我就可以學習如何使用JSON。
  • 好像kSoap2是做到這一點的最好辦法。我有一個與它的工作所需要的jar文件
  • 我鑽研得到PhoneGap的,所以如果有,它使用一個答案的話,我可以用

我的web服務工作正常,並且與我在許多教程中看到的基本相同,所以這裏沒有問題。

如果有人能指點我的教程,這將幫助我學習所有我需要知道的創建示例應用程序,從我的Web服務獲取信息,或者如果有人願意帶我通過它,我將不勝感激!

在此先感謝!

回答

3

最初你必須建立一個http連接,以便你可以從你的api得到響應xml響應或者json響應。您可以使用下面的代碼。
保持課程不同於活動。 - :所以在這裏

Response res = new Response("your_url"); 
String getResponse = res.getResponse(); 

你從API響應 -

public class Response { 

String get_url, response; 
Activity activity; 

public Response(String url){ 
    this.get_url = url; 

} 

public String getResponse(){ 
    InputStream in = null;   
     byte[] data = new byte[1000]; 
     try { 
       URL url = new URL(get_url); 
       URLConnection conn = url.openConnection(); 
       conn.connect(); 
      /* conn.*/ 
       in = conn.getInputStream(); 
       Log.d("Buffer Size +++++++++++++", ""+in.toString().length()); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(in),in.toString().length()); 
       String line; 
       StringBuilder sb = new StringBuilder(); 
       while ((line = rd.readLine()) != null) { 
        sb.append(line); 
       } 
       rd.close(); 
       response = sb.toString(); 

      in.read(data); 
      Log.d("INPUT STREAM PROFILE RESPONSE",response); 
      in.close(); 
     } catch (IOException e1) { 
      Log.d("CONNECTION ERROR", "+++++++++++++++++++++++++++"); 
      // TODO Auto-generated catch block 

      e1.printStackTrace(); 
     } 
     return response; 
} 
} 

,您可以調用類的活動是這樣的。

現在可以使解析器

  //Extend the class with Default Handler 

     public class XMLParser extends DefaultHandler { 
       //You must have basic knowledge about Array List and setter/getter methods 
       // This is where the data will be stored 
     ArrayList<Item> itemsList; 
      Item item; 
      String data; 
      String type; 
      private String tempVal; 

       //Create the Constructor 
      public XMLParser(String data){ 
     itemsList = new ArrayList<Item>(); 

     this.data = data; 

    } 

    public byte parse(){ 

      SAXParserFactory spf = null; 
      SAXParser sp = null; 
      InputStream inputStream = null; 

      try { 
       inputStream = new ByteArrayInputStream(data.getBytes()); 
       spf = SAXParserFactory.newInstance(); 
       if (spf != null) { 
        sp = spf.newSAXParser(); 
        sp.parse(inputStream, this); 
       } 
      } 
      /* 
      * Exceptions need to be handled MalformedURLException 
      * ParserConfigurationException IOException SAXException 
      */ 

      catch (Exception e) { 
       System.out.println("Exception: " + e); 
       e.printStackTrace(); 
      } finally { 
       try { 
        if (inputStream != null) 
         inputStream.close(); 
       } catch (Exception e) { 
       } 
      } 

      if (itemsList != null && itemsList.size() > 0) { 
      // //Log.d("Array List Size",""+tipsList.get(4).getTitle()); 


       return 1; 
      } else { 
       return 0; 
      } 

     } 

    public ArrayList<Item> getItemList(){ 
     return itemsList; 
    } 


       // Here you can check for the xml Tags 
    @Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 


     if(localName.equalsIgnoreCase("item")){ 
      item = new Item(); 
      Log.d("Working", "+++++++++++++++++++++++"); 
     } 


    } 
     //tempVal is the variable which stores text temporarily and you 
       // may save the data in arraylists 
    public void characters(char[] ch, int start, int length) 
       throws SAXException { 
      tempVal = new String(ch, start, length); 
     } 


    @Override 
    public void endElement(String uri, String localName, String qName) 
      throws SAXException { 




     if(localName.equalsIgnoreCase("item")){ 
      itemsList.add(item); 
      Log.d("Working in endelement", "+++++++++++++++++++++++"); 

      item.setTitle(tempVal); 

     } 
     } 

結合這一切: -

現在讓我們看看活動

  public void oncreate(){ 
       // Do something or mostly the basic code 
       // Call the class to initate the connection and get the data 
       FetchList fl = new FetchList(); 
        fl.execute(); 
        } 
      //Always better to use async task for these purposes 
      public class FetchList extends asyncTask<Void,Void,Byte>{ 

       doinbackground{ 
        // this was explained in first step 
        Response res = new Response("url"); 
        String response = res.getResponse(); 
        XmlParser xml = new XmlParser(response); 
         ArrayList<item> itemList = xml.getItemList(); 
         xml.parse(); 
       } 
       } 

那麼這一切都是爲了它。

+0

感謝您的答覆! 所以,以確保我有這個權利...... 我創建一個名爲響應新的Java類,然後調用在活動java代碼響應。 我看到你把所有的東西組合在一起,但我把解析器放在哪裏?什麼東西進入main.xml或任何其他文件的輸出?或者這將是我需要運行應用程序的全部? –

+0

如果你看到doinbackground方法調用響應,因爲你已經正確理解後,我打電話給XmlParser類,並傳遞我從我的Response類獲得的響應。 Main.xml只是設置佈局並顯示與解析無關的UI。爲了顯示ui上的項目列表,您必須在main.xml中添加一個線性佈局,並創建一個自定義視圖。然後在doinbackground之後,你可以使用arraylist創建自定義視圖,然後將它添加到你的線性佈局中,比如layout.addView(CustomListItem) – abhishek