0

私はアンドロイド開発の初心者です。XMLパーサーにSAXパーサーを使用しています。この例外の理由が見つかりませんでした。getAsset() メソッドを試してみました。しかし、うまくいきませんでした。

xmlParser コード :::

 public class XMLParser {

     public static Country parseCountry(InputStream is) {
        try {
         Country country= new Country(null, null, null);

         XMLReader xmlReader =  SAXParserFactory.newInstance().newSAXParser().getXMLReader();
         XMLHandler xmlHandler = new XMLHandler();
         xmlReader.setContentHandler(xmlHandler);     
         xmlReader.parse(new InputSource(new FileInputStream(http://64.85.165.53/dharatest/xmlarray.xml));
         country = xmlHandler.getParsedCountryData();

        } catch(ParserConfigurationException pce) { 
               Log.e("SAX XML", "sax parse error", pce); 
        } catch(SAXException se) { 
               Log.e("SAX XML", "sax error", se);       
        } catch(IOException ioe) { 
               Log.e("SAX XML", "sax parse io error", ioe); 
        }     
       return country;
   }
 }
4

3 に答える 3

2

URL で FileInputStream を使用しているのはなぜですか? 試す:

 public class XMLParser {

     public static Country parseCountry(InputStream is) {
        try {
         Country country= new Country(null, null, null);

         XMLReader xmlReader =  SAXParserFactory.newInstance().newSAXParser().getXMLReader();
         XMLHandler xmlHandler = new XMLHandler();
         xmlReader.setContentHandler(xmlHandler);     
         xmlReader.parse(new InputSource(new URL("http://64.85.165.53/dharatest/xmlarray.xml").openStream());
         country = xmlHandler.getParsedCountryData();

        } catch(ParserConfigurationException pce) { 
               Log.e("SAX XML", "sax parse error", pce); 
        } catch(SAXException se) { 
               Log.e("SAX XML", "sax error", se);       
        } catch(IOException ioe) { 
               Log.e("SAX XML", "sax parse io error", ioe); 
        }     
       return country;
   }
 }
于 2012-06-27T13:03:19.607 に答える
0
 public class XMLParser {

     public static Country parseCountry(InputStream is) {
         try {
         Country country= new Country(null, null, null);

       XMLReader xmlReader =SAXParserFactory.newInstance().newSAXParser().getXMLReader();
     XMLHandler xmlHandler = new XMLHandler();
     xmlReader.setContentHandler(xmlHandler);     
     xmlReader.parse(new InputSource(getAssets().open("data.xml"));
     country = xmlHandler.getParsedCountryData();

    } catch(ParserConfigurationException pce) { 
           Log.e("SAX XML", "sax parse error", pce); 
    } catch(SAXException se) { 
           Log.e("SAX XML", "sax error", se);       
    } catch(IOException ioe) { 
           Log.e("SAX XML", "sax parse io error", ioe); 
    }     
  return country;
   }
   }
于 2012-06-27T13:37:50.107 に答える
-1

あなたのラインを変更します

xmlReader.parse(new InputSource(new FileInputStream(http://64.85.165.53/dharatest/xmlarray.xml));  

 xmlReader.parse(new InputSource(callWebservice("http://64.85.165.53/dharatest/xmlarray.xml")));

ここで、callWebserviceメソッドは次のとおりです。

 private InputStream callWebservice(String serviceURL) {
        HttpClient client=new DefaultHttpClient();
        HttpGet getRequest=new HttpGet();

           try {
             // construct a URI object
             getRequest.setURI(new URI(serviceURL));
              } catch (URISyntaxException e) {
                Log.e("URISyntaxException", e.toString());
               }

            // buffer reader to read the response
           BufferedReader in=null;
           // the service response
          HttpResponse response=null;
             try {
                  // execute the request
                  response = client.execute(getRequest);
               } catch (ClientProtocolException e) {
                 Log.e("ClientProtocolException", e.toString());
               } catch (IOException e) {
                 Log.e("IO exception", e.toString());
            }
      if(response!=null)
            try {
                return response.getEntity().getContent();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        else
          return null;
        return null;

    }
于 2012-06-27T13:10:14.360 に答える