Ich mache Netzwerkanrufe von einem IntentService, aber immer noch eine NetworkOnMainThreadException. Mein Verständnis ist, dass ein IntentService immer auf einem Arbeitsthread läuft, also bin ich überrascht, dies zu sehen. Der entscheidende Teil könnte sein, dass mein IntentService eine statische Hilfsklasse aufruft, die die Netzwerkaufrufe durchführt. Die statische Hilfsklasse wird in meiner Hauptanwendungsklasse instanziiert.NetworkOnMainThreadException in IntentService
Ich dachte, dies würde immer noch auf dem Arbeitsthread des IntentService ausgeführt werden. Was vermisse ich?
Ehrlich gesagt bevorzuge ich berauschende informative Diskussionen über einen schnellen Code-Fix. Aber wenn Code erforderlich ist, muss Code zur Verfügung gestellt werden:
//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);
}
}
Auch 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:
> [email protected] - 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
SOLUTION
Das zugrunde liegende Problem ein wenig von Legacy-Code war die onHandleIntent
ausdrücklich rief . In LoginService.java oben:
@Override
public void onStart(Intent intent, int startId) {
onHandleIntent(intent);
}
Dies verursacht der onHandleIntent Code auf dem Haupt-Thread ausgeführt werden, wie es aus dem onStart Ereignisse aufgerufen wird (die offensichtlich auf Haupt-Thread ausgeführt wird).
Sie verpassen etwas von Ihrem Code, damit wir sehen können, was vor sich geht. Bitte poste etwas Code, die logcat-Ausgabe, und zeige auf die Zeile, von der der Fehler kommt. – MCeley