Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android中通过线程从资产中获取XML文件?_Java_Android_Multithreading_Xml Parsing - Fatal编程技术网

Java 如何在Android中通过线程从资产中获取XML文件?

Java 如何在Android中通过线程从资产中获取XML文件?,java,android,multithreading,xml-parsing,Java,Android,Multithreading,Xml Parsing,这是我的设想 我有MainActivity.java,在其中我像这样调用线程 private void callXMLParserThread() { String filePath = "file:///android_asset/weather_conditions.xml"; parserThread = new XMLParserThread(context, filePath); parserThread.start(); } 这是我的XMLParserT

这是我的设想

我有
MainActivity.java
,在其中我像这样调用线程

private void callXMLParserThread() {

    String filePath = "file:///android_asset/weather_conditions.xml";
    parserThread = new XMLParserThread(context, filePath);
    parserThread.start();

}
这是我的
XMLParserThread.java

public class XMLParserThread extends Thread {

Context context;
String fileName;
XMLParser xmlParser;

public XMLParserThread(Context context, String fileName) {

    this.context = context;
    this.fileName = fileName;
}

@Override
public void run() {

    xmlParser = new XMLParser();

    String xmlResponse = null;
    try {
        xmlResponse = xmlParser.getXmlFromFile(context, fileName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Log.d("xmlResponse", xmlResponse + "");

    super.run();
}

}
注意:在run()方法中,我调用另一个方法
getXmlFromFile()
驻留在
XMLParser.java

public class XMLParserThread extends Thread {

Context context;
String fileName;
XMLParser xmlParser;

public XMLParserThread(Context context, String fileName) {

    this.context = context;
    this.fileName = fileName;
}

@Override
public void run() {

    xmlParser = new XMLParser();

    String xmlResponse = null;
    try {
        xmlResponse = xmlParser.getXmlFromFile(context, fileName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Log.d("xmlResponse", xmlResponse + "");

    super.run();
}

}
下面是我的
getXmlFromFile()
方法

public String getXmlFromFile(Context context, String fileName) throws IOException {

    Log.e("fileName", fileName);

    InputStream is = null;
    try {
        is = context.getAssets().open(fileName);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    int result = bis.read();
    while(result != -1) {
      byte b = (byte)result;
      buf.write(b);
      result = bis.read();
    }        
    return buf.toString();
}
问题

当我执行代码时,它抛出
java.io.FileNotFoundException:file:///android_asset/weather_conditions.xml 在xml.parser.XMLParser.getXmlFromFile(XMLParser.java:43)

其中第43行是
is=context.getAssets().open(文件名)getXmlFromFile()
方法中的code>


此外,我确信该文件存在于assets文件夹中。我哪里出错了?

如果我没弄错,你可以这样说,而不必担心。”file:///...“部分


从资源定义路径时,仅写入资源子文件夹的路径

如果您的下有xml文件:

assets/android_asset/weather_conditions.xml
因此,文件路径应为:

String filePath = "android_asset/weather_conditions.xml";
顺便说一句,代码中有助手:

is = context.getAssets().open(fileName);

context.getAssets()
意味着打开
assets
文件夹,并找出其中的路径。

文件夹在哪里?我认为您的路径是绝对的,但如果我没有错,应用程序将无法访问根文件夹。您可以查看以下文档: