2017-02-07 4 views
0

私はRESTfulなWebサービスを持っているし、ブラウザでは、そのように見える動作しません。
接続などは機能しますが、それでも正しく動作していません。接続は、私はこの出力を取得する作業をしているがアンドロイド残りFUL Webサービスクライアントが正しく

InputStream in = null; 
    String  s = ""; 
    try { 
     in = OpenHttpConnection(url); 
     Document doc = null; 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db; 
     try { 
      db = dbf.newDocumentBuilder(); 
      doc = db.parse(in); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     doc.getDocumentElement().normalize(); 

     NodeList defs = doc.getElementsByTagName("users");      // iterate through <Definition>s 
     for (int i=0; i<defs.getLength(); i++) { 
      Node node = defs.item(i); 
      if (node.getNodeType() == Node.ELEMENT_NODE) { 
       Element e = (Element)node;        // convert Definition node into an element 
       NodeList words = (e).getElementsByTagName("user"); // iterate through its <WordDefinition>s 
       for (int j=0; j<words.getLength(); j++) { 
        Element we = (Element)words.item(j); 
        NodeList textNodes = ((Node) we).getChildNodes(); 
        s += ((Node) textNodes.item(0)).getNodeValue() + ". \n"; 
       } 
      } 
     } 
    } catch (IOException e1) { 
     Log.d("NetworkingActivity", e1.getLocalizedMessage()); 
    } 
    return s;  

::私は、Webサービスをこのようにアクセスしよう

this xml file does not appear to have any style information associated with it

:ブラウザで私はまた、次のメッセージが表示されます

null.
null.
null.

答えて

1

以下のスニペットを使用してください。

try { 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(in); 

      Element element = doc.getDocumentElement(); 
      element.normalize(); 

      NodeList nList = doc.getElementsByTagName("user"); 
      for (int i=0; i<nList.getLength(); i++) { 

       Node node = nList.item(i); 
       if (node.getNodeType() == Node.ELEMENT_NODE) { 
        Element element2 = (Element) node; 
        s+= element2.getElementsByTagName("email").item(0).getChildNodes().item(0).getNodeValue(); 

       } 
      }//end of for loop 
      System.out.print(s); 
     } catch (Exception e) {e.printStackTrace();} 
関連する問題