Problem beim xml parsen..

J

jonaven

Neues Mitglied
0
Hi
Ich habe versucht ein tutorial aus dem netz zu verwenden und bekomme immer eine exception. Bei dem Versch geht es darum von google-wetter bestimmte daten aus der xml zu lesen. In der App wird eine Stadt eigegeben und es soll die Temp angezeigt werden.
Hier mal ein Teil des codes:

Code:
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WeatherXMLParsing extends Activity implements OnClickListener {
	
	static final String baseURL = "http://www.google.com/ig/api?weather=";
	 TextView tv;
	 EditText city, state;
	
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle icicle) {
           super.onCreate(icicle);

           setContentView(R.layout.weather);
           Button b = (Button)findViewById(R.id.bWeather);
           tv = (TextView)findViewById(R.id.tvWeather);
           city = (EditText)findViewById(R.id.etCity);
           state = (EditText)findViewById(R.id.etState);
           b.setOnClickListener(this);
  
   }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		String c = city.getText().toString();
		String s = state.getText().toString();
		
		StringBuilder URL = new StringBuilder(baseURL);
		URL.append(c + "," + s);
		String fullUrl = URL.toString();
		try{
			URL website = new URL(fullUrl);
			//getting xmlreader to parse data
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			HandlingXMLStuff doingWork = new HandlingXMLStuff();
			xr.setContentHandler(doingWork);
exception-->       xr.parse(new InputSource(website.openStream())); 
			
			
			String information = doingWork.getInformation();
			tv.setText(information);
		        }catch (Exception e){
			tv.setText("XML parse error: " + e.getMessage());

			
		}
	}
}

Ich hoffe ihr könnt mir weiter helfen :)
schonma danke
 
Poste mal den Logcat bzw. die Exception - machts ein wenig einfacher ;)
 
Wenn ich das so genau wüsste... Ich habe nur durch breakpoints rausgefunden, dass es nach dem xr.parse(new InputSource(website.openStream())); in den Catch abschnitt springt... LogCat sehe ich nichts
Wie kann ich weiter vorgehen?
 
jonaven schrieb:
Wenn ich das so genau wüsste... Ich habe nur durch breakpoints rausgefunden, dass es nach dem xr.parse(new InputSource(website.openStream())); in den Catch abschnitt springt... LogCat sehe ich nichts
Wie kann ich weiter vorgehen?

Dafür müsste man die Exeption in den Logcat weiterleiten.. und nicht abfangen und in TextView anzeigen.

regards

edit.:


jonaven schrieb:
Code:
		try{
			URL website = new URL(fullUrl);
			//getting xmlreader to parse data
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			HandlingXMLStuff doingWork = new HandlingXMLStuff();
			xr.setContentHandler(doingWork);
exception-->       xr.parse(new InputSource(website.openStream())); 
			
			
			String information = doingWork.getInformation();
			tv.setText(information);
		 [B] }catch (Exception e){
			tv.setText("XML parse error: " + e.getMessage());[/B]

			
		}
	}
}
 
Code:
catch (Exception e){
	e.printStackTrace();	
		}
 
Moin,
Ich tippe mal auf eine NetworkOnMainThreadException... Was ist denn dein Build-target?

MfG
 
Also genau die NetworkOnMainThreadException hat er geworfen... Jetzt habe ich da mal nachgelesen:
The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged.
...
Nur hilft mir das jetzt auch nicht wirklich weiter außer, dass ich runter auf ein mini API gehe.. Was bedeutet denn "their main event loop threads" ? ich habe jetzt mal ne andere Activity vorangestellt die mit nem Button weiterleitet..
das hat aber auch nichts gebracht. Hier mal der LogCat-code
dank euch :)

Code:
02-29 17:26:45.816: W/System.err(16719): android.os.NetworkOnMainThreadException
02-29 17:26:45.816: W/System.err(16719): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
02-29 17:26:45.816: W/System.err(16719): 	at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
02-29 17:26:45.826: W/System.err(16719): 	at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
02-29 17:26:45.826: W/System.err(16719): 	at java.net.InetAddress.getAllByName(InetAddress.java:220)
02-29 17:26:45.826: W/System.err(16719): 	at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
02-29 17:26:45.826: W/System.err(16719): 	at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-29 17:26:45.826: W/System.err(16719): 	at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
02-29 17:26:45.826: W/System.err(16719): 	at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
02-29 17:26:45.826: W/System.err(16719): 	at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-29 17:26:45.826: W/System.err(16719): 	at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
02-29 17:26:45.826: W/System.err(16719): 	at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
02-29 17:26:45.836: W/System.err(16719): 	at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
02-29 17:26:45.836: W/System.err(16719): 	at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
02-29 17:26:45.836: W/System.err(16719): 	at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
02-29 17:26:45.836: W/System.err(16719): 	at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
02-29 17:26:45.836: W/System.err(16719): 	at java.net.URL.openStream(URL.java:462)
02-29 17:26:45.836: W/System.err(16719): 	at de.jonas.parsing.ParsingXML.onCreate(ParsingXML.java:46)
02-29 17:26:45.836: W/System.err(16719): 	at android.app.Activity.performCreate(Activity.java:4465)
02-29 17:26:45.846: W/System.err(16719): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-29 17:26:45.846: W/System.err(16719): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-29 17:26:45.846: W/System.err(16719): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-29 17:26:45.846: W/System.err(16719): 	at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-29 17:26:45.846: W/System.err(16719): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-29 17:26:45.846: W/System.err(16719): 	at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 17:26:45.846: W/System.err(16719): 	at android.os.Looper.loop(Looper.java:137)
02-29 17:26:45.846: W/System.err(16719): 	at android.app.ActivityThread.main(ActivityThread.java:4424)
02-29 17:26:45.846: W/System.err(16719): 	at java.lang.reflect.Method.invokeNative(Native Method)
02-29 17:26:45.856: W/System.err(16719): 	at java.lang.reflect.Method.invoke(Method.java:511)
02-29 17:26:45.856: W/System.err(16719): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-29 17:26:45.856: W/System.err(16719): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-29 17:26:45.856: W/System.err(16719): 	at dalvik.system.NativeStart.main(Native Method)
02-29 17:26:45.886: E/parse(16719): WeatherQueryError
02-29 17:26:45.886: E/parse(16719): android.os.NetworkOnMainThreadException
02-29 17:26:45.886: E/parse(16719): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
02-29 17:26:45.886: E/parse(16719): 	at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
02-29 17:26:45.886: E/parse(16719): 	at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
02-29 17:26:45.886: E/parse(16719): 	at java.net.InetAddress.getAllByName(InetAddress.java:220)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
02-29 17:26:45.886: E/parse(16719): 	at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
02-29 17:26:45.886: E/parse(16719): 	at java.net.URL.openStream(URL.java:462)
02-29 17:26:45.886: E/parse(16719): 	at de.jonas.parsing.ParsingXML.onCreate(ParsingXML.java:46)
02-29 17:26:45.886: E/parse(16719): 	at android.app.Activity.performCreate(Activity.java:4465)
02-29 17:26:45.886: E/parse(16719): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-29 17:26:45.886: E/parse(16719): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-29 17:26:45.886: E/parse(16719): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-29 17:26:45.886: E/parse(16719): 	at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-29 17:26:45.886: E/parse(16719): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-29 17:26:45.886: E/parse(16719): 	at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 17:26:45.886: E/parse(16719): 	at android.os.Looper.loop(Looper.java:137)
02-29 17:26:45.886: E/parse(16719): 	at android.app.ActivityThread.main(ActivityThread.java:4424)
02-29 17:26:45.886: E/parse(16719): 	at java.lang.reflect.Method.invokeNative(Native Method)
02-29 17:26:45.886: E/parse(16719): 	at java.lang.reflect.Method.invoke(Method.java:511)
02-29 17:26:45.886: E/parse(16719): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-29 17:26:45.886: E/parse(16719): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-29 17:26:45.886: E/parse(16719): 	at dalvik.system.NativeStart.main(Native Method)
 
Das bedeutet nur das du deinen Netzerwerkram in einem seperaten Thread machen sollst...
 
Ok soweit habe ich das geschafft -vielen Dank!!!
Jetzt habe ich ein weiteres Problem:
Der die folgende Datei soll ausgelesen werden:
Mensaplan
Könnt ihr mir sagen wie ich das in diesem fall richtig machen kann? Bisher wird ziemlicher misst ausgelesen und es trennt nicht richtig.
welche ausdrücke soll ich für if (localName.equals("??")) eingeben?
Einfach title oder description funktioniert nicht...
ich brauche immer das Datum, die zwei Essen und die Wahlbeilage.
Habt ihr eine idee? muchas gracias
grüße
 
[FONT=&quot]Naja das ist Standard XML, also am besten einfach in einen XML Parser[/FONT]schmeißen
Code:
  [FONT=&quot]<?xml version="1.0" encoding='utf-8' ?>[/FONT]
  [FONT=&quot]<rss version='2.0'>[/FONT]
  [FONT=&quot]<channel>[/FONT]
  [FONT=&quot]<title>Speiseplan Mensa Schwenningen</title>[/FONT]
  [FONT=&quot]<link>http://www.studentenwerk.uni-freiburg.de</link>[/FONT]
  [FONT=&quot]<description>Mensa Schwenningen</description>[/FONT]
  [FONT=&quot]<language>de-de</language>[/FONT]
  [FONT=&quot]<pubDate>Tue, 06 Mar 2012 00:00:00 +0100</pubDate>[/FONT]
  
  [FONT=&quot]<item>[/FONT]
  [FONT=&quot]  <title>Dienstag, 06.03.2012</title>[/FONT]
  [FONT=&quot]  <description><![CDATA[<u>Essen 1</u><br>[/FONT]
  [FONT=&quot]                        <b>Eierpfannkuchen</b><br>mit Schwarzwurzeln Holländische  Art<br><br>[/FONT]
  [FONT=&quot]                        <u>Essen 2</u><br>[/FONT]
  [FONT=&quot]                        <b>Grünkohl mit Mettenden</b> (1,3,5,6)<br><br>[/FONT]
  [FONT=&quot]                        <u>Wahlbeilagen</u><br>[/FONT]
  [FONT=&quot]                        Dampfkartoffeln<br>Gemüse oder Salat<br><br>[/FONT]
  [FONT=&quot]                        <u></u><br>[/FONT]
  [FONT=&quot]                        <br>[/FONT]
  [FONT=&quot]                        <u></u><br>[/FONT]
  [FONT=&quot]                        <br>[/FONT]
  [FONT=&quot]                        <u></u><br>[/FONT]
  [FONT=&quot]                        <br>[/FONT]
  [FONT=&quot]                       <u></u><br>[/FONT]
  [FONT=&quot]                       <br>[/FONT]
  [FONT=&quot]                       <u></u><br>[/FONT]
  [FONT=&quot]                       <br>[/FONT]
  [FONT=&quot]                       <u>Kennzeichnung</u><br>[/FONT]
  [FONT=&quot]                       1-mit Schwein  3-mit Geschmacksverstärker  4-mit Farbstoff  5-mit Antioxidationsmittel  6-mit Konservierungstoff<br>]]></description>[/FONT]
  [FONT=&quot]  <link>http://www.studentenwerk.uni-freiburg.de/index.php?id=186&amp;Tag=0&amp;Ort_ID=671</link>[/FONT]
  [FONT=&quot]<guid>Dienstag, 06.03.2012</guid>[/FONT]
  [FONT=&quot]<pubDate>Tue, 06 Mar 2012 00:00:00 +0100</pubDate>[/FONT]
  [FONT=&quot]</item>[/FONT]
  [FONT=&quot]

[/FONT]
[FONT=&quot]
und den einfach die Daten auslesen, die du haben willst[/FONT]
 
xpath heißt das Zauberwort. :D
 

Ähnliche Themen

S
  • Sempervivum
Antworten
2
Aufrufe
593
Sempervivum
S
W
  • WuDiDong
Antworten
3
Aufrufe
755
jogimuc
J
B
Antworten
2
Aufrufe
1.383
deek
D
Zurück
Oben Unten