我在通过httppost
请求上传和文件时遇到问题。当我在具有api level 10
. 但是,当我测试同一个应用程序时,我在 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();
}
}