Android是挂而Android没有找到连接 [英] android is hang while android don't find connection

查看:116
本文介绍了Android是挂而Android没有找到连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Android应用程序,但同时(如?)找不到服务器,它会挂起工具包(SDK /模拟器?)。 我重视我的登录code。请大家帮我想办法了。我只是想,如果服务器不可用,我的申请将留在登录页面,但不挂本身。

当我调试我的code,我没有得到的HTT presponse价值,这行之后

 的Htt presponse HTT presponse = httpclient.execute(httpPost);
 

发生问题

 公共字符串登录(用户名字符串,字符串密码){
        尝试 {
            字符串URL = URL +标志=?+上的+&放大器;用户=+的userName +&放大器;通=+密码;
            httpPost =新HttpPost(URL);
            HTT presponse HTT presponse = httpclient.execute(httpPost);
            INT状态code = HTT presponse.getStatusLine()的getStatus code()。
            如果(状态code == 200){
                字符串responseMsg = GETRESPONSE(HTT presponse);
                返回responseMsg;
            }其他{
                Log.e(TAG,状态code +);
            }
        }赶上(ClientProtocolException E){
            Log.e(TAG,e.getMessage(),E);
        }赶上(IOException异常E){
            Log.e(TAG,e.getMessage(),E);
        }
        返回null;
    }
 

解决方案

您还应该考虑确定一个时限(如5秒),这样系统不会尝试连接到无法访问服务器很长一段时间。

 最后的HttpParams的HttpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(的HttpParams,5000);
HttpConnectionParams.setSoTimeout(的HttpParams,5000);
 

I am creating an android application but while(if?) server is not found, it will hang the tool kit (SDK/emulator?). I have attached my login code. Please help me to find the solution. I just want that if server is not available, my application will stay on at the login page without hanging itself.

when I debugged my code I didn't get value of httpResponse and after this line

HttpResponse httpResponse = httpclient.execute(httpPost);

the problem occurred

public String login(String userName, String password){
        try {
            String url = URL+ "?flag="+"on"+"&user="+userName+"&pass="+password;
            httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpclient.execute(httpPost);
            int statuscode = httpResponse.getStatusLine().getStatusCode();
            if (statuscode == 200) {
                String responseMsg = getResponse(httpResponse); 
                return responseMsg;
            }else{
                Log.e(TAG, statuscode+"");
            }
        } catch (ClientProtocolException e) {
            Log.e(TAG, e.getMessage(),e);
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(),e);
        }
        return null;
    }

解决方案

You also should consider to define a timeout (e.g. 5 seconds) so that the system does not try to connect to an unreachable server for a long time.

final HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);

这篇关于Android是挂而Android没有找到连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆