gpt4 book ai didi

java - Android Connection/SocketTimeOutException被捕获,但应用仍然崩溃

转载 作者:行者123 更新时间:2023-12-02 07:19:57 56 4
gpt4 key购买 nike

基本上,我试图让用户知道应用程序何时无法访问服务器/服务器,而他们的电话互联网/数据连接完全正常。因此,我通过在此处关注此帖子来设置超时:How to set HttpResponse timeout for Android in Java

效果很好,除非正确捕获了异常后,应用仍然崩溃,而不是显示Toast消息并返回到应用屏幕。请注意,“ Log.e(“ CONN TIMEOUT”,e.toString());“在logcat中正确记录为:“ CONN TIME org.apache.http.conn.ConnectTimeoutException:连接到/192.168.11.60:80超时”

根据要求,完整日志:http://pastebin.com/rpe8iKRi

    // Making HTTP request
try {

HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ConnectTimeoutException e) {
Toast.makeText(getApplicationContext(), "Server timeout", Toast.LENGTH_LONG).show();
Log.e("CONN TIMEOUT", e.toString());
} catch (SocketTimeoutException e) {
Toast.makeText(getApplicationContext(), "Server timeout", Toast.LENGTH_LONG).show();
Log.e("SOCK TIMEOUT", e.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
Log.e("OTHER EXCEPTIONS", e.toString());
}

最佳答案

解决了。在函数末尾缺少引发Exception {。 h

感谢@Alin的好提示。

关于java - Android Connection/SocketTimeOutException被捕获,但应用仍然崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14373042/

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