gpt4 book ai didi

java - 如何通过在 android 中启动一个新的 Activity 来处理 IOException

转载 作者:行者123 更新时间:2023-11-29 02:58:25 27 4
gpt4 key购买 nike

我正尝试在抛出异常时启动我的主页 Activity 。现在当异常发生时,应用程序关闭并从主页启动。我想让它无缝地转到主页面应用程序,而不是让用户警告应用程序正在关闭。它给我的错误是这样的:

D/Issue with Http:: IOException
04-19 13:57:31.518 27520-27570/myapp W/System.err: java.io.FileNotFoundException: my webpage
04-19 13:57:31.518 27520-27570/myapp W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
04-19 13:57:31.518 27520-27570/myapp W/System.err: at myapp.HttpManager.MyHttpGet.httpDownloadData(MyHttpGet.java:79)
04-19 13:57:31.518 27520-27570/myapp W/System.err: at myapp.HttpManager.MyHttpGet.doInBackground(MyHttpGet.java:53)
04-19 13:57:31.518 27520-27570/myapp W/System.err: at myapp.HttpManager.MyHttpGet.doInBackground(MyHttpGet.java:31)
04-19 13:57:31.518 27520-27570/myapp W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-19 13:57:31.518 27520-27570/myapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-19 13:57:31.519 27520-27570/myapp W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-19 13:57:31.519 27520-27570/myapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-19 13:57:31.519 27520-27570/myapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-19 13:57:31.519 27520-27570/myapp W/System.err: at java.lang.Thread.run(Thread.java:818)
04-19 13:57:31.538 27520-27520/myapp D/AndroidRuntime: Shutting down VM
04-19 13:57:31.594 27520-27520/myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: myapp, PID: 27520
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.contains(java.lang.CharSequence)' on a null object reference
at myapp.JSON.LoginJsonParser.checkUsername(LoginJsonParser.java:41)
at myapp.LoginPage$2.onClick(LoginPage.java:128)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这对开发人员来说很好,但我想在发生这种情况时开始新的 Activity 。我对这个问题的代码是这样的。

 private String httpDownloadData(String myUrl)

{
String respone = null;
HttpURLConnection urlConnection = null;
try
{
URL url = new URL(myUrl);
//put in the username and pssword for parmas to send to url
//this is good for login
if (username!=null)
{
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}

urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();

if(inputStream != null)
{
respone = streamToString(inputStream);
inputStream.close();
}

}catch (IOException ie)
{
//ie.printStackTrace();
Log.d("Issue with Http: " , "IOException");
ie.printStackTrace();
Intent newIntent = new Intent(activity.getApplicationContext(), ECom.class);
activity.startActivity(newIntent);
}finally {
if(urlConnection != null)
{
urlConnection.disconnect();
}
}
return respone;
}

感谢您对此的任何帮助。

最佳答案

你不能在 UI 线程中执行任何网络操作,它会给你 networkOnMainThreadException。请使用 AsyncTask 执行网络和长时间运行的操作。你可以找到samples and docs here

或使用 volley处理网络操作的框架

关于java - 如何通过在 android 中启动一个新的 Activity 来处理 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36709746/

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