Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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.lang.string不能通过xml解析器转换为java.lang.integer android_Java_Android_Xml_Parsing_Xml Parsing - Fatal编程技术网

如何解析java.lang.string不能通过xml解析器转换为java.lang.integer android

如何解析java.lang.string不能通过xml解析器转换为java.lang.integer android,java,android,xml,parsing,xml-parsing,Java,Android,Xml,Parsing,Xml Parsing,我试图解析XML并将其显示在列表视图中,但在运行应用程序后,什么也没有发生——列表显示出来,但不显示XML数据。我不知道我是否错过了什么 main活动类 public class MainActivity extends ListActivity { // All static variables static final String URL = "http://api.androidhive.info/pizza/?format=xml"; // XML node keys static f

我试图解析XML并将其显示在列表视图中,但在运行应用程序后,什么也没有发生——列表显示出来,但不显示XML数据。我不知道我是否错过了什么

main活动类

public class MainActivity extends ListActivity {
// All static variables
static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }
    ListView myList=(ListView)findViewById(android.R.id.list);

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = null; // getting XML
    try {
        xml = parser.getXmlFormUrl(URL);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Document doc = parser.getDomElement(xml); // getting DOM element
    NodeList nl = doc.getElementsByTagName(KEY_ITEM);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
        map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

        // adding HashList to ArrayList
        menuItems.add(map);
    }
    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.activity_main,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
            R.id.name, R.id.desciption, R.id.cost });

    setListAdapter(adapter);
    // selecting single ListView item

}
 public class XMLParser {
    String result;
    public String getXmlFormUrl(String link) throws IOException{
    URL url=new URL(link.toString());
    HttpURLConnection UrlConnection= (HttpURLConnection)     url.openConnection();
    int status=UrlConnection.getResponseCode();
    if(status==200){
        InputStream inputStream=UrlConnection.getInputStream();
        BufferedReader bufferedReader=new BufferedReader(new     InputStreamReader(inputStream,"UTF8"));
        StringBuilder stringBuilder= new StringBuilder();
        String line;
        while ((line=bufferedReader.readLine())!=null){
            stringBuilder.append((line+"\n"));
        }
        result=stringBuilder.toString();
        inputStream.close();
    }
    return  result;
}
public Document getDomElement(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        doc = db.parse(is);

    } catch (ParserConfigurationException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (SAXException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e("Error: ", e.getMessage());
        return null;
    }
    // return DOM
    return doc;
}
public String getValue(Element item, String str) {
    NodeList n = item.getElementsByTagName(str);
    return this.getElementValue(n.item(0));
}

public final String getElementValue( Node elem ) {
    Node child;
    if( elem != null){
        if (elem.hasChildNodes()){
            for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                if( child.getNodeType() == Node.TEXT_NODE  ){
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}
}

ActivityMain.xml



如果您有一个字符串,并且您确定其中唯一的内容是一个不带空格的整数,则可以使用
Integer.parseInt(String)
将其转换为整数。如果字符串不仅仅是数字,则此方法将引发异常。

您不能直接将
字符串
对象更改或引用为
整数
类型,因为这两个类之间没有
父子关系

如果您试图以这种方式将
字符串
转换为
整数
,它将引发
类异常

String someValue = "123";
Integer intValue = (Integer) someValue; // ClassCastException Raise.
因此,如果要将
字符串
变量更改或引用为
整数
,则必须使用解析技术

int intValue = Integer.parseInt(someValue); //this code will work for this.

请包含stacktrace和您试图解析的xml。这是XML的url您得到了什么错误以及在哪里?@NielsMasdorp java.lang.string无法转换为java.lang。integer@NielsMasdorp该行setListAdapter(适配器);