android api在定位多个设备时出现问题

时间:2013-11-11 06:23:37

标签: android

我在通过httppost请求上传和归档时遇到问题。当我在拥有api level 10的Android设备上运行我的应用程序时,我得到了预期的结果。但是,当我测试相同的应用程序时,我得到Android设备中的错误以及api级别为17的模拟器。我面临的问题是当我运行我的应用程序时,我得到items.gethLenght() = 1的值在api级别10但是当我在api级别17运行相同的应用程序时,我获得了items.getLenght() = 0的价值请帮我解决这个问题。

public void uploadFileToServer()
 {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url_context + "/response/responses");
                try {                       
                    StringEntity entity = new StringEntity(xmlString, "UTF-8");
                    httppost.setEntity(entity); 
                    httppost.addHeader("Accept", "application/xml");
                    httppost.addHeader("Content-Type", "application/xml");

                   HttpResponse response = httpclient.execute(httppost);

                    if (response.getStatusLine() != null){

                        response_data= new ArrayList<String>();

                 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                    factory.setNamespaceAware(true);
                    try
                    {
                        HttpEntity r_entity = response.getEntity();
                        String xmlString = EntityUtils.toString(r_entity);
                        DocumentBuilder db = factory.newDocumentBuilder();
                        InputSource inStream = new InputSource();
                        inStream.setCharacterStream(new StringReader(xmlString));
                        Document doc = db.parse(inStream);
                        Document doc = db.parse(new InputSource(new StringReader(xmlString)));
                        Element root = doc.getDocumentElement();
                        NodeList items = root.getElementsByTagName("order");
                        for (int i=0;i<items.getLength();i++)
                        {
                            Node item = items.item(i);
                            NodeList properties = item.getChildNodes();
                            for (int j=0;j<properties.getLength();j++)
                            {
                                Node property = properties.item(j);
                                String name = property.getNodeName();
                                if (name.equalsIgnoreCase("name"))
                                {
                                    try{
                                        response_data.add(property.getFirstChild().getNodeValue());
                                        response_data_data= response_data.toString();

                                    }
                                    catch(Exception nu)
                                    {
                                        nu.printStackTrace();
                                    }
                                }                       
                            }
                        }
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }

                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch(Exception e){
                    e.printStackTrace();
                }

 }

1 个答案:

答案 0 :(得分:0)

尽管“尽可能不快”,但无论如何这都是答案:

这是一个错误。在API级别10以上,getElementsByTagName()返回具有给定Name的所有后代元素。但是在API级别10及以下版本中,getElementsByTagName()错误地包含了调用该方法的元素本身。

在你的情况下,getElementsByTagName()只返回一个元素:“root”本身。在API级别10之上它正常工作,返回一个空列表,因为它看起来没有称为“order”的后代元素。

相关问题