gpt4 book ai didi

Android HttpResponse

转载 作者:行者123 更新时间:2023-11-29 02:02:02 27 4
gpt4 key购买 nike

我的 if (httpResponse == null) block 没有运行。如何在 HttpResponse httpResponse = httpClient.execute(httpPost);

之前实现

请告诉我怎么做。

    public class XMLParser {
private Activity activity = null;
// constructor
public XMLParser(Activity act) {
activity = act;
}

/**
* 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;
}

/**
* Getting XML DOM element
* @param XML string
* */
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) {
AlertDialog.Builder builder = new AlertDialog.Builder( activity ); //<<-- This part throws an exception " ThreadPoolExecutor "
builder.setMessage( "Host not found" )
.setCancelable(false)
.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
System.exit(0);
}

});
AlertDialog alert = builder.create();
alert.show();
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 "";
}

/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}

最佳答案

当出现错误时,execute 不会返回 null 而是抛出异常。例如,当找不到主机时,它会抛出一个 UnknownHostException。此异常是 IOException 的子类。

您的代码旨在“捕获”IOException。但是当这种情况发生时,您只打印一个堆栈跟踪(这可以在您的 LogCat 中以橙色看到)而什么都不做。所以接下来,执行“return xml”语句,您的方法结束。

所以如果你想“捕捉”主机不存在的情况,你可以像下面这样重写它。要捕获更多错误,您可能应该计算出 IOException catch block 。请确保您了解发生的情况以及异常处理的工作原理。

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 (UnknownHostException e) {
AlertDialog.Builder builder = new AlertDialog.Builder( activity );
builder.setMessage( "Host not found" )
.setCancelable(false)
.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
System.exit(0);
}

});
AlertDialog alert = builder.create();
alert.show();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

// return XML
return xml;
}

关于Android HttpResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129706/

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