我的Android应用程序中有两个Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Connection.Response response = new Second().get();
}
}
和
public class Second extends Activity {
Connection.Response response = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
get();
}
public Connection.Response get()
{
try {
response = Jsoup.connect("http://example.com").execute();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
第二个活动正常工作 - 我在我的课堂上使用方法get,这很好,但如果我打开MainActivity我有错误:
08-25 19:31:30.764: E/AndroidRuntime(999): FATAL EXCEPTION: main
08-25 19:31:30.764: E/AndroidRuntime(999): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.js/com.example.js.MainActivity}: android.os.NetworkOnMainThreadException
08-25 19:31:30.764: E/AndroidRuntime(999): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.os.Handler.dispatchMessage(Handler.java:99)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.os.Looper.loop(Looper.java:137)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-25 19:31:30.764: E/AndroidRuntime(999): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 19:31:30.764: E/AndroidRuntime(999): at java.lang.reflect.Method.invoke(Method.java:511)
08-25 19:31:30.764: E/AndroidRuntime(999): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-25 19:31:30.764: E/AndroidRuntime(999): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-25 19:31:30.764: E/AndroidRuntime(999): at dalvik.system.NativeStart.main(Native Method)
08-25 19:31:30.764: E/AndroidRuntime(999): Caused by: android.os.NetworkOnMainThreadException
08-25 19:31:30.764: E/AndroidRuntime(999): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
08-25 19:31:30.764: E/AndroidRuntime(999): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-25 19:31:30.764: E/AndroidRuntime(999): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-25 19:31:30.764: E/AndroidRuntime(999): at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
08-25 19:31:30.764: E/AndroidRuntime(999): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
08-25 19:31:30.764: E/AndroidRuntime(999): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:425)
08-25 19:31:30.764: E/AndroidRuntime(999): at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
08-25 19:31:30.764: E/AndroidRuntime(999): at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
08-25 19:31:30.764: E/AndroidRuntime(999): at com.example.js.Second.get(Second.java:27)
08-25 19:31:30.764: E/AndroidRuntime(999): at com.example.js.MainActivity.onCreate(MainActivity.java:15)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.app.Activity.performCreate(Activity.java:4465)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-25 19:31:30.764: E/AndroidRuntime(999): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-25 19:31:30.764: E/AndroidRuntime(999): ... 11 more
Second.java:27:response = Jsoup.connect(“http://example.com”)。execute();
MainActivity.java:15:Connection.Response response = new Second()。get();
我该怎么做?
答案 0 :(得分:1)
您无法在主线程上访问网络。这是为了防止您的UI呈现被长时间运行的网络调用阻止。您需要将网络逻辑放在自己的线程中。我建议使用AsyncTask。
此外,您不应该以这种方式创建自己的Activity。如果您需要两个活动进行沟通。使用startActivityForResult和setResult使用Intent的数据包来回传递数据。
从外观上看,您正在使用Second Activity纯粹用于数据检索。您应该考虑在服务中执行此操作。您仍然需要在服务中处理线程(或者您可以使用IntentService)
答案 1 :(得分:1)
天啊,从哪里开始......好吧,一个人。您只需要在UI页面之间转换的活动。看起来,你在这里想要实现的只是一个在另一个类中调用方法的活动。
所以在你的情况下,我会不让Second
从Activity
延伸。
你在Second
正在做的是进行网络通话。您无法从主线程进行网络呼叫,最好使用AsyncTask并且需要向manifest
添加permission,表示您需要网络访问