gpt4 book ai didi

android - 应用程序崩溃而不是给出 Internet 连接错误

转载 作者:行者123 更新时间:2023-11-29 21:31:18 25 4
gpt4 key购买 nike

如果我的设备未连接到互联网,那么它应该会在 Toast 中显示互联网连接错误。但是我的应用程序崩溃了。它不会捕获无互联网连接的错误。我的代码在互联网连接上完美运行。请帮助我

private class MyAsyncTask extends AsyncTask<String, Integer, String>{


@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String s=postData();
return s;
}

protected void onPostExecute(String result){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}

public String postData() {
// Create a new HttpClient and Post Header


String origresponseText="";
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/ServletParams/AndroidServlet");

// Add your data cnic,mobileNo,name,address,nextkin
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("param1",cnic));
nameValuePairs.add(new BasicNameValuePair("param2", mobileNo));
nameValuePairs.add(new BasicNameValuePair("param3", name));
nameValuePairs.add(new BasicNameValuePair("param4", address));
nameValuePairs.add(new BasicNameValuePair("param5", nextkin));
nameValuePairs.add(new BasicNameValuePair("param6", sendImages));


httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
/* execute */

HttpResponse response = httpclient.execute(httppost);
HttpEntity rp = response.getEntity();
origresponseText=readContent(response);

}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), "No Internet Connection", Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), "sorry", Toast.LENGTH_SHORT).show();
}
String responseText = origresponseText.substring(7, origresponseText.length());
return responseText;

}


}
String readContent(HttpResponse response)
{
String text = "";
InputStream in =null;

try {
in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
text = sb.toString();
} catch (IllegalStateException e) {
Toast.makeText(getBaseContext(), "sorry", Toast.LENGTH_SHORT).show();

} catch (IOException e) {
Toast.makeText(getBaseContext(), "Sorry", Toast.LENGTH_SHORT).show();
}
finally {
try {

in.close();
} catch (Exception ex) {
Toast.makeText(getBaseContext(), "Sorry", Toast.LENGTH_SHORT).show();
}
}

return text;
}

这是一个日志:

10-17 19:45:05.061: E/AndroidRuntime(22597): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.os.Handler.<init>(Handler.java:121)
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.widget.Toast$TN.<init>(Toast.java:322)
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.widget.Toast.<init>(Toast.java:91)
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.widget.Toast.makeText(Toast.java:238)
10-17 19:45:05.061: E/AndroidRuntime(22597): at com.example.androidufoneapp.CustomerRegistrationL0$MyAsyncTask.postData(CustomerRegistrationL0.java:647)
10-17 19:45:05.061: E/AndroidRuntime(22597): at com.example.androidufoneapp.CustomerRegistrationL0$MyAsyncTask.doInBackground(CustomerRegistrationL0.java:602)
10-17 19:45:05.061: E/AndroidRuntime(22597): at com.example.androidufoneapp.CustomerRegistrationL0$MyAsyncTask.doInBackground(CustomerRegistrationL0.java:1)
10-17 19:45:05.061: E/AndroidRuntime(22597): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-17 19:45:05.061: E/AndroidRuntime(22597): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-17 19:45:05.061: E/AndroidRuntime(22597): ... 4 more

最佳答案

我认为,您遇到了窗口泄漏,因为当没有互联网连接时,您的代码正在访问 doInBackground 方法中的 UI 线程。在 ReadContent 方法中查看 Toast 消息。如果没有互联网并且出现异常,您的 Toast 消息将访问 UI 线程。但是由于您的应用程序当时处于后台线程中,因此您将收到窗口泄漏错误并且应用程序将崩溃,因为您无法从后台线程。

好的..要解决此问题,请从 PostData 方法中删除 Toast 消息。如果你想显示 toast 然后在 onPostExecute 方法中显示它。我建议另一个好方法。

使用此方法使用以下方法检查连接是否可用

public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
}
return false;
}

然后当你开始你的 AsyncTask 时,这样做

if(isOnline()){
// Start your AsyncTask
} else{
// Show internet not available alert
}

您还需要添加 ACCESS_NETWORK_STATE 权限才能使用此方法。希望对您有所帮助。

关于android - 应用程序崩溃而不是给出 Internet 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19430195/

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