gpt4 book ai didi

android - 网速慢导致强行关闭

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:13:22 27 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,在每个 Activity 中我需要将一些数据传递到服务器并在进入下一个 Activity 之前取回响应。如果互联网足够快,应用程序可以正常工作。但随着速度下降,应用力会关闭。网速慢怎么处理才不会强制关闭应用?????

部分代码

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 string
try{
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;
}

最佳答案

由于您没有显示任何代码,我猜您的目标是 Android API 级别 10 或更低级别,并且您在 UI 线程中进行所有网络连接,导致可怕的 App Not Responding(ANR) 错误。解决此问题的一种方法是使用 AsyncTask并将所有网络代码移到那里。如果操作正确,AsyncTaskdoInBackground() 将在单独的线程中处理您的所有网络,从而使 UI 保持响应。

它通常是这样工作的:

private class NetworkTask extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
// Do all networking here, this will work away in a background thread.
// In your case:
// HttpResponse response = httpclient.execute(httppost);
// Must happen here
}

@Override
protected void onPostExecute(String result) {
// dismiss progress dialog if any (not required, runs in UI thread)
}

@Override
protected void onPreExecute() {
// show progress dialog if any, and other initialization (not required, runs in UI thread)
}

@Override
protected void onProgressUpdate(Void... values) {
// update progress, and other initialization (not required, runs in UI thread)
}
}

如果启用 StrictMode ,或目标 api 版本 11 及更高版本,Android 将在您尝试执行此操作时抛出 NetworkOnMainThreadException

关于android - 网速慢导致强行关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11184322/

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