我正在尝试从我的Android应用程序中的网络服务中获取一些数据。
我使用的代码是:
@Override
protected String doInBackground(String... params) {
String highScore = null;
int points = 0;
try {
URI uri = new URI("http://vestsoft.somee.com/api/scores");
URL website = new URL(uri.toASCIIString());
InputStream inputStream = website.openStream();
InputSource input = new InputSource(inputStream);
SAXParserFactory saxp = SAXParserFactory.newInstance();
SAXParser sp = saxp.newSAXParser();
XMLReader xmlReader = sp.getXMLReader();
HighScoreHandler handler = new HighScoreHandler();
xmlReader.setContentHandler(handler);
xmlReader.parse(input); // Here is an exception thrown
highScore = handler.highScore.getUserName();
points = handler.highScore.getPoints();
} catch (Exception e) {
e.printStackTrace();
}
return highScore + " " + points;
}
我得到一个例外说:
“在第1行,第0列:语法错误”
我不知道我做错了什么。
如您所见,我从中获取数据的API是:
http://vestsoft.somee.com/api/scores
答案 0 :(得分:0)
您的XML缺少正确的标题:
<?xml version="1.0" encoding="UTF-8"?>
将该字符串放在XML文件的顶部,SAX Parser将不再烦恼。
答案 1 :(得分:0)
有一些非常好的开源XML解析工具可以使这更容易。例如,要使用Simple XML解决此问题,我会这样做:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.squareup.okhttp.OkHttpClient;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
URL website = new URL("http://vestsoft.somee.com/api/scores");
OkHttpClient client = new OkHttpClient();
HttpURLConnection conn = client.open(website);
conn.addRequestProperty("Accept", "application/xml");
InputStream stream = conn.getInputStream();
Serializer serializer = new Persister();
ArrayOfScore scoreArray = serializer.read(ArrayOfScore.class, stream);
// do something with the ArrayScore object here
} catch (Exception err) {
Log.i(TAG, err.toString());
}
}
};
new Thread(r).start();
}
});
}
}
这引用了另外两个类,XML注释的java类。他们是:
import java.util.List;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root
public class ArrayOfScore {
@ElementList(inline=true,required=false)
private List<Score> scores;
public List<Score> getScores() {
return scores;
}
}
和
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name="Score")
public class Score {
@Element(name="Id",required=false)
Integer Id;
@Element(name="Name",required=false)
String Name;
@Element(name="Points",required=false)
String Points;
@Element(name="dateTime",required=false)
String dateTime;
}
使用这个简单的模型,您几乎可以解析遇到的任何XML,并自动解析为第一类对象。 JSON有类似的类,例如Gson