android – 慢速互联网连接导致强制关闭

android – 慢速互联网连接导致强制关闭,第1张

概述我正在开发一个 Android应用程序,在每个活动中我需要将一些数据传递到服务器上并在进入下一个活动之前获取响应.如果互联网足够快,该应用程序工作正常.但随着速度下降,应用程序力量将关闭.如何处理缓慢的互联网连接,以便它可能不会导致强制关闭应用程序????? 这是代码的一部分 public void onClick(View v) { // TODO Auto-generated meth 我正在开发一个 Android应用程序,在每个活动中我需要将一些数据传递到服务器上并在进入下一个活动之前获取响应.如果互联网足够快,该应用程序工作正常.但随着速度下降,应用程序力量将关闭.如何处理缓慢的互联网连接,以便它可能不会导致强制关闭应用程序?????

这是代码的一部分

public voID onClick(VIEw v) {    // Todo auto-generated method stub    UserFunctions userFunction = new UserFunctions();    if(userFunction.isNetworkAvailable(getApplicationContext()))    {        answer="";        for(int check1=0;check1<counter2;check1++){            int check2=0;            answer=answer+option4[check1]+"|";            while(check2<counter1){                if(edTxt[check1][check2].getText().toString().equals("")){                    answer="";                    break;                }                else{                    answer=answer+edTxt[check1][check2].getText().toString()+"|";                }                check2++;                   }               if(answer.equals("")){                break;            }            else{                answer=answer+"||";            }        }        if(answer.equals("")){            Toast.makeText(this,"Please fill all fIElds",600).show();        }        else{        userFunction.form1(surveyID,userID,quesNo,answer);        if(total>0){            draw(temp);         }        else{            ques_no++;            ArrayList<nameValuePair> params = new ArrayList<nameValuePair>();              params.add(new BasicnameValuePair("quesNo",Integer.toString(ques_no)));            params.add(new BasicnameValuePair("surveyID",surveyID));            count = getJsonFromURL22(surveyCond,params);                       j=Integer.parseInt(result);            if(j==22)            {                Toast.makeText(this,"Survey Completed",600).show();                Intent home=new Intent(Format16.this,SurveyCompleted.class);                UserFunctions userFunctions = new UserFunctions();                userFunctions.full(surveyID);                Bundle d=new Bundle();                d.putString("userID",userID);                home.putExtras(d);                startActivity(home);            }     public String getJsonFromURL22(String url,List<nameValuePair> params){try{    httpClIEnt httpclIEnt = new DefaulthttpClIEnt();    httpPost httppost = new httpPost(url);    httppost.setEntity(new UrlEncodedFormEntity(params));    httpResponse response = httpclIEnt.execute(httppost);    httpentity entity = response.getEntity();    is = entity.getContent();}catch(Exception e){    Log.e("log_tag","Error in http connection"+e.toString());}//convert response to stringtry{    BufferedReader reader = new BufferedReader(new inputStreamReader(is,"iso-8859-1"),8);    sb = new StringBuilder();    sb.append(reader.readline());    String line="0";    while ((line = reader.readline()) != null) {        sb.append(line);    }    is.close();    result=sb.toString();}catch(Exception e){    Log.e("log_tag","Error converting result "+e.toString());}return result;}
解决方法

The application works fine if the internet is fast enough. But as the
speed goes down the application force closes.

它清楚地表明您正在UI Thread上进行网络 *** 作.如果在主线程上执行异步 *** 作,并且如果超过5秒,那么您的应用程序将显示强制关闭对话框,这对于最终用户非常不满意. .

事实上,如果你试图在最新的AndroID版本(即4.0或更高版本)上运行这样的应用程序它将不允许你运行应用程序,它会在检测到在主线程上执行异步 *** 作时立即崩溃.

您必须使用AsyncTask或Handlers来执行长时间运行的应用程序.

浏览以下博客了解更多信息.

http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html

总结

以上是内存溢出为你收集整理的android – 慢速互联网连接导致强制关闭全部内容,希望文章能够帮你解决android – 慢速互联网连接导致强制关闭所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1128632.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存