gpt4 book ai didi

android - IntentService 中的 NetworkOnMainThreadException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:21 24 4
gpt4 key购买 nike

我正在从 IntentService 进行网络调用,但仍收到 NetworkOnMainThreadException。我的理解是 IntentService 总是在工作线程上运行,所以我很惊讶地看到这一点。关键部分可能是我的 IntentService 正在调用执行网络调用的静态帮助器类。静态助手类在我的主应用程序类中实例化。

我认为这仍然会在 IntentService 的工作线程上执行。我错过了什么?

老实说,比起快速的代码修复,我更喜欢内容丰富的讨论。但如果需要代码,应提供代码:

//MyApplication.java
public class MyApplication extends Application{

private static NetworkUtils utils;

@Override
public void onCreate() {
super.onCreate();
utils = new NetworkUtils(this);
...
}
...
}

//NetworkUtils.java
public class NetworkUtils {

private static Context context;
private static final Gson gson = new Gson();

public NetworkUtils(Context context) {
this.context = context;
}

public static final DataResponse login(String email, String password) {
//*** NetworkOnMainThreadException OCCURS HERE ***
DataResponse response = HttpConnection.put(url, json);
...
return response;
}
...
}

//LoginService.java
public class LoginService extends IntentService {

public LoginService() {
super("LoginService");
}

@Override
public void onStart(Intent intent, int startId) {
onHandleIntent(intent);
}

@Override
protected void onHandleIntent(Intent intent) {
Bundle bundle = new Bundle();
DataResponse response = NetworkUtils.login(email, password);
...
bundle.putBoolean(MyConstants.ExtraKeys.LOGGED, response.success);
MainApplication.getApplicationInstance().sendBroadCast(MyConstants.Actions.LOGIN, bundle);
}
}

//LoginActivity.java
public class LoginActivity extends ActionBarActivity implements IDialogClickListener {
...
public void onLoginButtonPressed() {
Intent intent = new Intent(MainApplication.getApplicationInstance(), LoginService.class);
this.startService(intent);
}
}

此外,Logcat:

> 04-01 18:20:41.048: VERBOSE/com.foo.foo(28942):
> com.foo.foo.network.HttpConnection.execute - METHOD: PUT
> 04-01 18:20:41.068: ERROR/com.foo.foo(28942):
> com.foo.foo.social.NetworkUtils.login - class
> android.os.NetworkOnMainThreadException: null
> 04-01 18:20:41.169: DEBUG/com.foo.foo(28942):
> com.foo.foo.MainActivity$MyReceiver.onReceive - BROADCAST RECEIVED:
> com.foo.foo.MainApplication@422d81d8 - Intent { act=com.foo.foo.login
> dat=com.foo.foo.scheme://data/1364854841079 (has extras) }
> 04-01 18:20:41.169: INFO/com.foo.foo(28942):
> com.foo.foo.activity.LoginActivity.setData - ACTION: com.foo.foo.login
> - ISERROR: true

解决方案

根本问题是一些显式调用 onHandleIntent 的遗留代码。在上面的 LoginService.java 中:

@Override 
public void onStart(Intent intent, int startId) {
onHandleIntent(intent);
}

这导致 onHandleIntent 代码在主线程上运行,因为它是从 onStart 事件(显然在主线程上运行)调用的。

最佳答案

我发现了这个问题。在上面的 LoginService.java 中检查这个莫名其妙的覆盖:

@Override 
public void onStart(Intent intent, int startId) {
onHandleIntent(intent);
}

这导致 onHandleIntent 代码在主线程上运行,因为它是从 onStart 事件(显然在主线程上运行)调用的。我很想了解将其放入其中的开发人员的想法!

关于android - IntentService 中的 NetworkOnMainThreadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15753316/

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