有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将API放置在缺少标记的Android上

我正在使用GooglePlacesAPI,并试图解析XML数据,以获得包括评级在内的许多信息。唯一的问题是,有时没有评级标签(如下所示)。我如何解决这个问题

<name>Zari</name>
<type>restaurant</type>
<type>food</type>
<type>establishment</type>
<formatted_address>212-214 Ifield Drive,            Crawley, United Kingdom</formatted_address>
<geometry>
<location>
 <lat>51.1218980</lat>
   <lng>-0.2135630</lng>
 </location>
 </geometry>
<rating>3.3</rating>
 </result>

 <result>
<name>Happy Meeting</name>
<type>restaurant</type>
<type>food</type>
<type>establishment</type>
<formatted_address>
<geometry>
<location>
<lat>51.1161600</lat>
<lng>-0.1844890</lng>
</location>
</geometry>
</result>
<result>

共 (1) 个答案

  1. # 1 楼答案

    只需检查一下你是否拿到了配给。例如,如果你像这样分析数据

        String temp=null;
        int rating;
            if (localName.equals("rating")){ //this code won't work if rating tag is
                                            // not present thus temp will be null
    
                        temp = attributes.getValue("rating");
                        rating = Integer.parseInt(temp);
        }
    

    现在在代码中进一步检查

    if(temp==null){
    rating=0; //or any that you want
    }