Android App-crahs bei Http-request

  • Antworten:7
NeuHier
  • Forum-Beiträge: 30

23.08.2013, 12:38:12 via Website

Hallo Community,

ich benötige den Seiteninhalt einer Bestimmten Url als String, deswegen wollte ich folgendes Snippet verwenden:
http://cdrussell.blogspot.de/2011/12/android-get-body-of-http-response-as.html
Ich habe die Internet-Permission in der Manifestdatei hinzugefügt, dennoch stürzt die App ab.

Hier das Logcat-Protokoll:

08-23 10:32:02.576 1153-1153/com.fotoboxxv1.fotoboxxat I/Choreographer: Skipped 67 frames! The application may be doing too much work on its main thread.
08-23 10:32:03.066 275-291/system_process I/ActivityManager: Displayed com.fotoboxxv1.fotoboxxat/.RegistrierenActivity: +2s446ms
08-23 10:32:06.005 362-362/com.android.inputmethod.latin I/Choreographer: Skipped 80 frames! The application may be doing too much work on its main thread.
08-23 10:32:06.105 37-88/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-23 10:32:10.716 1153-1153/com.fotoboxxv1.fotoboxxat E/fotoboxx: Versuche zu Registieren
08-23 10:32:10.726 1153-1153/com.fotoboxxv1.fotoboxxat D/fotoboxx: Versuche aufzurufen: http://www.google.de?q=test
08-23 10:32:10.896 1153-1153/com.fotoboxxv1.fotoboxxat D/AndroidRuntime: Shutting down VM
08-23 10:32:10.896 1153-1153/com.fotoboxxv1.fotoboxxat W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-23 10:32:10.976 1153-1153/com.fotoboxxv1.fotoboxxat E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3599)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3594)
... 11 more
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.fotoboxxv1.fotoboxxat.RegistrierenActivity.onButtonRegiestrieren(RegistrierenActivity.java:64)
... 14 more
08-23 10:32:11.056 275-275/system_process W/ActivityManager: Force finishing activity com.fotoboxxv1.fotoboxxat/.RegistrierenActivity
08-23 10:32:11.066 275-275/system_process W/WindowManager: Failure taking screenshot for (246x410) to layer 21020
08-23 10:32:11.266 275-292/system_process I/Choreographer: Skipped 35 frames! The application may be doing too much work on its main thread.
08-23 10:32:11.286 37-37/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-23 10:32:11.640 275-289/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{40f68548 u0 com.fotoboxxv1.fotoboxxat/.RegistrierenActivity}
08-23 10:32:13.607 1153-1153/com.fotoboxxv1.fotoboxxat I/Process: Sending signal. PID: 1153 SIG: 9

Und so sieht meine Methode bis jetzt aus:
1String aufzurufendeUrl = "http://www.google.de?q=test";
2
3 Log.d("fotoboxx", "Versuche aufzurufen: " + aufzurufendeUrl);
4
5 DefaultHttpClient http = new DefaultHttpClient();
6 HttpGet httpMethod = new HttpGet();
7 try {
8 httpMethod.setURI(new URI(aufzurufendeUrl));
9 } catch (URISyntaxException e) {
10 e.printStackTrace();
11 Log.e("fotoboxx","UriSyntaxException");
12 }
13 HttpResponse response = null;
14 try {
15 response = http.execute(httpMethod);
16 } catch (IOException e) {
17 e.printStackTrace();
18 Log.e("fotoboxx","IOExeption in http.execute()");
19 }
20 int responseCode = response.getStatusLine().getStatusCode();
21 switch(responseCode)
22 {
23 case 200:
24 HttpEntity entity = response.getEntity();
25 if(entity != null)
26 {
27 try {
28 String responseBody = EntityUtils.toString(entity);
29
30 AlertDialog ad = new AlertDialog.Builder(this).create();
31 ad.setCancelable(false); // This blocks the 'BACK' button
32 ad.setMessage(responseBody);
33 ad.setButton("OK", new DialogInterface.OnClickListener() {
34 @Override
35 public void onClick(DialogInterface dialog, int which) {
36 dialog.dismiss();
37 }
38 });
39 ad.show();
40 } catch (IOException e) {
41 e.printStackTrace();
42 Log.e("fotoboxx","IOExeption in responseCode");
43 }
44
45 }
46 break;
47 }

Hat jemand eventuell eine Idee?
Danke schon mal für eure Hilfe.

Gruß,
NeuHier

Antworten
Christian
  • Forum-Beiträge: 307

23.08.2013, 12:50:29 via Website

Hi NeuHier,

der Netzwerkzugriff MUSS in einem separaten Thread laufen.

Mfg Christian

— geändert am 23.08.2013, 12:50:56

Antworten
NeuHier
  • Forum-Beiträge: 30

23.08.2013, 13:05:32 via Website

Danke für die schnelle Antwort, meine Methode sieht jetzt so aus:
1Thread t = new Thread(){
2 public void run(){
3
4 String aufzurufendeUrl = "http://www.google.de?q=test";
5
6 Log.d("fotoboxx", "Versuche aufzurufen: " + aufzurufendeUrl);
7
8 DefaultHttpClient http = new DefaultHttpClient();
9 HttpGet httpMethod = new HttpGet();
10 try {
11 httpMethod.setURI(new URI(aufzurufendeUrl));
12 } catch (URISyntaxException e) {
13 e.printStackTrace();
14 Log.e("fotoboxx","UriSyntaxException");
15 }
16 HttpResponse response = null;
17 try {
18 response = http.execute(httpMethod);
19 } catch (IOException e) {
20 e.printStackTrace();
21 Log.e("fotoboxx","IOExeption in http.execute()");
22 }
23 int responseCode = response.getStatusLine().getStatusCode();
24 switch(responseCode)
25 {
26 case 200:
27 HttpEntity entity = response.getEntity();
28 if(entity != null)
29 {
30 try {
31 String responseBody = EntityUtils.toString(entity);
32
33 AlertDialog ad = new AlertDialog.Builder(RegistrierenActivity.this).create();
34 ad.setCancelable(false); // This blocks the 'BACK' button
35 ad.setMessage(responseBody);
36 ad.setButton("OK", new DialogInterface.OnClickListener() {
37 @Override
38 public void onClick(DialogInterface dialog, int which) {
39 dialog.dismiss();
40 }
41 });
42 ad.show();
43 } catch (IOException e) {
44 e.printStackTrace();
45 Log.e("fotoboxx","IOExeption in responseCode");
46 }
47
48 }
49 break;
50 }
51 }
52 };
53 t.start();

Allerdings stürzt die App immer noch ab. Hier wäre wieder die Logcat-Meldung:
08-23 11:03:26.244 906-906/com.fotoboxxv1.fotoboxxat E/fotoboxx: Versuche zu Registieren
08-23 11:03:26.264 906-938/com.fotoboxxv1.fotoboxxat D/fotoboxx: Versuche aufzurufen: http://www.google.de?q=test
08-23 11:03:26.994 906-938/com.fotoboxxv1.fotoboxxat W/dalvikvm: threadid=16: thread exiting with uncaught exception (group=0x40a71930)
08-23 11:03:27.014 906-938/com.fotoboxxv1.fotoboxxat E/AndroidRuntime: FATAL EXCEPTION: Thread-86
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:197)
at android.os.Handler.<init>(Handler.java:111)
at android.app.Dialog.<init>(Dialog.java:107)
at android.app.AlertDialog.<init>(AlertDialog.java:114)
at android.app.AlertDialog$Builder.create(AlertDialog.java:931)
at com.fotoboxxv1.fotoboxxat.RegistrierenActivity$1.run(RegistrierenActivity.java:84)
08-23 11:03:27.034 286-512/system_process W/ActivityManager: Force finishing activity com.fotoboxxv1.fotoboxxat/.RegistrierenActivity
08-23 11:03:27.044 286-512/system_process W/WindowManager: Failure taking screenshot for (246x410) to layer 21015
08-23 11:03:27.344 37-83/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-23 11:03:27.634 37-83/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
08-23 11:03:29.024 906-906/com.fotoboxxv1.fotoboxxat I/Choreographer: Skipped 37 frames! The application may be doing too much work on its main thread.

Gibt es vielleicht noch etwas zu beachten?

— geändert am 23.08.2013, 13:05:53

Antworten
Christian
  • Forum-Beiträge: 307

23.08.2013, 13:17:20 via Website

das ist der Fehler:


08-23 11:03:27.014 906-938/com.fotoboxxv1.fotoboxxat E/AndroidRuntime: FATAL EXCEPTION: Thread-86
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

und hier tritt er auf:

at com.fotoboxxv1.fotoboxxat.RegistrierenActivity$1.run(RegistrierenActivity.java:84)

Ich vermute mal es liegt daran das du den AlertDialog im Thread erstellst bzw manipuliert.
Google mal nach AsynTask könnte die ganze Sache ein bissel einfacher machen.

Mfg Christian

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

23.08.2013, 14:12:13 via Website

Du kannst keine UI in einem anderen Thread erzeugen.

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
NeuHier
  • Forum-Beiträge: 30

23.08.2013, 16:37:39 via Website

Mac Systems
Du kannst keine UI in einem anderen Thread erzeugen.
Danke, dass ist die Lösung, jetzt funktioniert es.

Christian Hempe
Ich vermute mal es liegt daran das du den AlertDialog im Thread erstellst bzw manipuliert.
Google mal nach AsynTask könnte die ganze Sache ein bissel einfacher machen.
Danke auch für deine Antwort, aber das ist jetzt nicht mehr nötig, ich warte einfach mit t.join(), darauf, bis der Thread fertig ist, speichere das result in einer globalen Variable und lasse es dann im Hauptthreat ausgeben.

Schönen Feierabend noch und danke für die Hilfe.

Gruß,
NeuHier

— geändert am 23.08.2013, 16:38:41

Antworten
Mac Systems
  • Forum-Beiträge: 1.727

23.08.2013, 16:44:31 via Website

Danke auch für deine Antwort, aber das ist jetzt nicht mehr nötig, ich warte einfach mit t.join(), darauf, bis der Thread fertig ist, speichere das result in einer globalen Variable und lasse es dann im Hauptthreat ausgeben.

Schlechte Idee, wenn der Request länger braucht was unter Mobile mal schnell passieren kann würgst du den UI Thread ab und bekommst einen ANR vom System. Schau dir mal wie du auf den UI Thread posten kannst mittels eines Handlers. Asnyc Task ist eine möglichkeit das direkt zu adressieren, hat aber wieder andere probleme (Drehen des Devices). Du kannst mittels runOnUiThread ein runnable übergeben das der UI thread abarbeitet.

Windmate HD, See you @ IO 14 , Worked on Wundercar, Glass V3, LG G Watch, Moto 360, Android TV

Antworten
NeuHier
  • Forum-Beiträge: 30

23.08.2013, 17:17:21 via Website

Ok, dass habe ich nicht bedacht. Ich werde mich mal etwas einlesen, "runOnUiThread" klingt schon mal ganz hilfreich.

Gruß,
NeuHier

Antworten