gpt4 book ai didi

java - 无法获得显示错误的响应

转载 作者:行者123 更新时间:2023-12-01 13:03:58 26 4
gpt4 key购买 nike

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

it响应请求格式无效dom解析失败,出现此响应

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

我不知道我在哪里做错了,而当我使用其他服务时它工作正常

最佳答案

尝试通过以下代码更改您的 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(HttpEntityEntity) :

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();

关于java - 无法获得显示错误的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23364463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com