无法获得显示错误的响应

时间:2014-04-29 12:19:08

标签: java android

public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * 
     * @param url
     *            string
     * */
    public String getXmlFromUrl(String url)

    {
        String xml = null;

        try

        {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    // Parsing the XML content and retrieve DOM element in the XML.

    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 doc;
    }

    /**
     * Getting node value
     * 
     * @param elem
     *            element
     */
    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 "";
    }

    // Retrieve each element child element value by using node name of element.
    public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }
}

这是我的Xml解析器类。 当我在Actvity中调用这样的创建方法时

    static final String URL = "http://www.webservicex.net/country.asmx/GetISD?CountryName=INDIA";

XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL);
        Document doc = parser.getDomElement(xml); // getting DOM element

它的响应请求格式无效dom解析失败,此响应来了

System.InvalidOperationException: Request format is invalid: .
   at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
   at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

当我使用其他服务时,我不知道我在哪里犯了错误

1 个答案:

答案 0 :(得分:1)

尝试通过以下代码更改您的getXmlFromUrl:

public String getXmlFromUrl(String url)

{
    String xml = null;

    try

    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();

        HttpGet httpGet = new HttpGet(url);
        HttpResponse response = httpClient.execute(httpGet, localContext);

        HttpEntity entity = response.getEntity();

        xml = getASCIIContentFromEntity(entity);


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
            return xml;
}

方法getASCIIContentFromEntity(HttpEntity实体):

private String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
    InputStream in = entity.getContent();


    StringBuffer out = new StringBuffer();
    int n = 1;
    while (n>0) {
        byte[] b = new byte[4096];
        n =  in.read(b);


        if (n>0) out.append(new String(b, 0, n));
    }


    return out.toString();
}

然后,您可能需要更改XML内容的结果而不是ASCII。

为此您可以使用:

String resultFromGetXmlFromUrl = parser.getXmlFromUrl(URL);
String xml = Html.fromHtml(resultFromGetXmlFromUrl).toString();