2011-03-02 10 views
13

के लिए HTTP अनुरोध मैं एंड्रॉइड पर अपने प्रोग्राम के लिए फ्लोरी एनालिटिक्स का उपयोग करने की कोशिश कर रहा हूं और मुझे सर्वर से एक्सएमएल फ़ाइल प्राप्त करने में परेशानी हो रही है।एक्सएमएल फ़ाइल

मैं बंद हो रहा हूं क्योंकि लॉग कैट सिस्टम में। टैग टैग में मुझे कुछ कारणों से इसका आधा मिल सकता है और यह कहता है "एक्सएमएल पासिंग अपवाद = java.net.MalformedURLException: प्रोटोकॉल नहीं मिला:? Xml संस्करण = 1.0 एन्कोडिंग = "UTF-8" आदि ... मेरी एक्सएमएल कोड के माध्यम से के बारे में आधे रास्ते तक। यकीन है कि मैं गलत क्या कर रहा है, मैं एक HTTP भेज रहा नहीं हैडर application/xml स्वीकार करने के लिए अनुरोध के साथ मिलता है और यह काम नहीं कर रहा ठीक से। किसी भी मदद की सराहना की है!

try { 

       //HttpResponse response = client.execute(post); 
       //HttpEntity r_entity = response.getEntity(); 
       //String xmlString = EntityUtils.toString(r_entity); 

     HttpClient client = new DefaultHttpClient(); 
     String URL = "http://api.flurry.com/eventMetrics/Event?apiAccessCode=????&apiKey=??????&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated"; 
     HttpGet get = new HttpGet(URL); 
     get.addHeader("Accept", "application/xml"); 
     get.addHeader("Content-Type", "application/xml"); 
     HttpResponse responsePost = client.execute(get); 
     HttpEntity resEntity = responsePost.getEntity(); 
     if (resEntity != null) 

     { 
        System.out.println("Not null!"); 

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 

        DocumentBuilder db = dbf.newDocumentBuilder(); 

        String responseXml = EntityUtils.toString(responsePost.getEntity()); 
        Document doc = db.parse(responseXml); 
        doc.getDocumentElement().normalize(); 

        NodeList nodeList = doc.getElementsByTagName("eventMetrics"); 


        for (int i = 0; i < nodeList.getLength(); i++) 
        { 
         Node node = nodeList.item(i); 

         Element fstElmnt = (Element) node; 

         NodeList nameList = fstElmnt.getElementsByTagName("day"); 

         Element dayElement = (Element) nameList.item(0); 

         nameList = dayElement.getChildNodes(); 

         countString = dayElement.getAttribute("totalCount"); 
         System.out.println(countString); 
         count = Integer.parseInt(countString); 
         System.out.println(count); 
         count += count; 

        } 
     } 

    } catch (Exception e) { 

        System.out.println("XML Passing Exception = " + e); 

       } 

उत्तर

18

पार्स विधि है कि एक स्ट्रिंग एक URL स्वरूप के लिए है लेता है। आप इसे पार्स करने से पहले एक StringReader में स्ट्रिंग रैप करने के लिए की जरूरत है। यदि आप एक्सएमएल प्राप्त कर सकते हैं और भी बेहतर है एक इनपुटस्ट्रीम और पार्स के रूप में, कुछ ऐसा:

String uri = 
    "http://api.flurry.com/eventMetrics/Event?apiAccessCode=?????&apiKey=??????&startDate=2011-2-28&endDate=2011-3-1&eventName=Tip%20Calculated"; 

URL url = new URL(uri); 
HttpURLConnection connection = 
    (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.setRequestProperty("Accept", "application/xml"); 

InputStream xml = connection.getInputStream(); 

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(xml); 
+2

बहुत बढ़िया! यह काम कर रहा है, आपको बहुत धन्यवाद, मैं इस दिन काम कर रहा हूं! अब xml के माध्यम से मेरी पढ़ाई को हल करने के लिए ... – rwarner

0

मैंने HttpURLConnection का उपयोग किया और यह एक कार्य कोड है।

URL url = new URL("...."); 
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); 

httpConnection.setRequestMethod("POST"); 
httpConnection.setRequestProperty("Accept", "application/xml"); 
httpConnection.setRequestProperty("Content-Type", "application/xml"); 

httpConnection.setDoOutput(true); 
OutputStream outStream = httpConnection.getOutputStream(); 
OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8"); 
outStreamWriter.write(requestedXml); 
outStreamWriter.flush(); 
outStreamWriter.close(); 
outStream.close(); 

System.out.println(httpConnection.getResponseCode()); 
System.out.println(httpConnection.getResponseMessage()); 

InputStream xml = httpConnection.getInputStream(); 
संबंधित मुद्दे