在代号一中捕获未知主机异常 [英] catching unknown host exception in codename one
问题描述
我正在使用代号一个
所以,事实是,我需要使用该应用程序访问URL。该URL会带回一些我在屏幕上显示的结果。
So the thing is, I need to access a URL using the app. THe URL brings back some result which I show on the screen.
所以我用这些行来做到这一点:
SO I use these lines to do that :
ConnectionRequest c = new ConnectionRequest() {
protected void readResponse(InputStream input) throws IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
int ch;
while ((ch = input.read()) != -1) {
bs.write(ch);
}
serverOutput = new String(bs.toByteArray());
bs.close();
}
};
c.setUrl("My URL HERE");
c.setPost(false);
NetworkManager.getInstance().addToQueueAndWait(c);
现在,如果gprs处于活动状态,则此代码可以正常工作。
So, now , if the gprs is active, this code works fine.
但是,如果GPRS处于不活动状态,则会引发未知主机异常
BUT , if the GPRS is inactive, it throws an Unknow Host Exception
SO来捕获此错误,我尝试尝试像这样捕获块:
SO to catch this error, i TRIED to use a try catch block like this:
try{
NetworkManager.getInstance().addToQueueAndWait(c);
}
catch(Exception e)
{
Sys.out.pln(e.troString());
}
但是,我仍然在应用程序中出现对话框形式的错误。我如何捕获此错误并自行处理?
But, i still get the error in the form of a dialog in the app. How do i catch this error and put my own handling for it?
更新1:
我不确定这是否一定是一个代号问题或与Java有关的问题,所以请帮我解决这个问题。
Am not sure this is necessarily a codename one specific questions, or related to java ...so just help me out with this.
推荐答案
尝试以下操作处理所有连接的一般错误:
Try this to handle generic errors for all connections:
NetworkManager.getInstance().addErrorListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//handle your error here consume the event
evt.consume();
}
});
或覆盖:
protected void handleErrorResponseCode(int code, String message) {
}
并且:
protected void handleException(Exception err) {
}
在您的连接请求代码中仅针对一堂课这样做。
In your connection request code to do this for just one class.
这篇关于在代号一中捕获未知主机异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!