我正在编写一个使用asmx调用Web服务的Android应用。我想要的JSON响应是在xml中,所以我试图提取一个名为“string”的标签,这是JSON所在的位置。我得到了上述例外。
我已经完成了一些日志记录,而且我正在获取一个流,所以url正在传递。 parser.nextTag()上发生异常。 这是文件和打印输出。有任何想法吗? 感谢。
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final String URL = "http://212.169.27.217:84/rotaservice.asmx/GetRota?CarerID=36&RotaDate=04-09-2012";
TextView tv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textview1);
Log.e(TAG, "about run DownloadXmlTask");
new DownloadXmlTask().execute(URL);
Log.e(TAG, "finished running DownloadXmlTask");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class DownloadXmlTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
Log.e(TAG, "url = " + urls[0].toString());
return loadXmlFromNetwork(urls[0]);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
setContentView(R.layout.activity_main);
// Displays the HTML string in the UI via a WebView
tv.setText(result);
}
}
private String loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
InputStream stream = null;
XmlParser xmlParser = new XmlParser();
List<WebServiceString> entries = null;
String string = null;
try {
stream = downloadUrl(urlString);
Log.e(TAG, "stream = " + stream.toString());
entries = xmlParser.parse(stream);
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (stream != null) {
stream.close();
}
}
for (WebServiceString entry : entries) {
Log.e("main", "WebserviceString = " + entry.toString());
string = entry.toString();
}
return string;
}
// Given a string representation of a URL, sets up a connection and gets
// an input stream.
private InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
}// end of class
public class XmlParser {
private static final String TAG = XmlParser.class.getSimpleName();
// We don't use namespaces
private final static String ns = null;
public List parse(InputStream in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
Log.e(TAG, "nextTag = " + parser.nextTag());
return readFeed(parser);
} finally {
in.close();
}
}
private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List string = new ArrayList();
// parser.require(XmlPullParser.START_TAG, ns, "feed");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the entry tag
if (name.equals("string")) {
string.add(readString(parser));
} else {
skip(parser);
}
}
return string;
}
public static class WebServiceString {
public final String string;
private WebServiceString(String string ) {
this.string = string;
}
}
// Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them off
// to their respective "read" methods for processing. Otherwise, skips the tag.
private WebServiceString readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "string");
String string = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("string")) {
string = readString(parser);
} else {
skip(parser);
}
}
return new WebServiceString(string);
}
private String readString(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "string");
String string = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "string");
return string;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
}
09-04 13:20:51.890: E/MainActivity(12076): about run DownloadXmlTask
09-04 13:20:51.900: E/MainActivity(12076): finished running DownloadXmlTask
09-04 13:20:51.900: E/MainActivity(12076): url = http://212.169.27.217:84/rotaservice.asmx/GetRota?CarerID=36&RotaDate=04-09-2012
09-04 13:20:52.200: D/libEGL(12076): loaded /system/lib/egl/libEGL_tegra.so
09-04 13:20:52.280: D/libEGL(12076): loaded /system/lib/egl/libGLESv1_CM_tegra.so
09-04 13:20:52.290: D/libEGL(12076): loaded /system/lib/egl/libGLESv2_tegra.so
09-04 13:20:52.330: D/OpenGLRenderer(12076): Enabling debug mode 0
09-04 13:20:52.410: E/MainActivity(12076): stream = libcore.net.http.FixedLengthInputStream@40d80638
09-04 13:20:52.610: W/System.err(12076): org.xmlpull.v1.XmlPullParserException: unexpected type (position:TEXT {"0":{"StartDate...@2:8360 in java.io.InputStreamReader@40d85e20)
09-04 13:20:52.610: W/System.err(12076): at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:2035)
09-04 13:20:52.610: W/System.err(12076): at com.carefreegroup.XmlParser.parse(XmlParser.java:30)
09-04 13:20:52.610: W/System.err(12076): at com.carefreegroup.MainActivity.loadXmlFromNetwork(MainActivity.java:80)
09-04 13:20:52.610: W/System.err(12076): at com.carefreegroup.MainActivity.access$1(MainActivity.java:67)
09-04 13:20:52.610: W/System.err(12076): at com.carefreegroup.MainActivity$DownloadXmlTask.doInBackground(MainActivity.java:50)
09-04 13:20:52.610: W/System.err(12076): at com.carefreegroup.MainActivity$DownloadXmlTask.doInBackground(MainActivity.java:1)
09-04 13:20:52.610: W/System.err(12076): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-04 13:20:52.610: W/System.err(12076): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-04 13:20:52.610: W/System.err(12076): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-04 13:20:52.620: W/System.err(12076): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-04 13:20:52.620: W/System.err(12076): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-04 13:20:52.620: W/System.err(12076): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-04 13:20:52.620: W/System.err(12076): at java.lang.Thread.run(Thread.java:864)
09-04 13:39:44.950: I/RestClient(14037): <?xml version="1.0" encoding="utf-8"?>
09-04 13:39:44.950: I/RestClient(14037): <string xmlns="http://tempuri.org/">{"0":{"StartDate":"04/09/2012 07:30:00","EndDate":"04/09/2012 07:50:00","Duration":"20","CallStatusID":"1","CallStatusName":"Completed","ClientSurname":"Brown","ClientForename":"Margaret","NeedName":"Assist With Dressing, Careline, Diabetic Type 2, Empty Commode, KEY SAFE, Leave A Drink, Make A Hot Drink, Meds - Level II Administer, Prepare Breakfast, Strip Wash, TIME BAND - Early Breakfast","CarerAwayReason":"","CallID":"7862d5a1-4cc3-4931-bb76-b23f723087b9","DoubleUpCallID":"","ActualTimeIn":"07:51","ActualTimeOut":"07:53"},"1":{"StartDate":"04/09/2012 07:30:00","EndDate":"04/09/2012 07:55:00","Duration":"25","CallStatusID":"1","CallStatusName":"Completed","ClientSurname":"Ebbs","ClientForename":"Vera","NeedName":"Assist With Dressing, Laundry, Leave A Drink, Leave A Sandwich For Later, Make A Hot Drink, Prepare Breakfast, Shower, TIME BAND - Early Breakfast","CarerAwayReason":"","CallID":"5e6f77b5-651b-4e4a-9876-0a52192a7b47","DoubleUpCallID":"","ActualTimeIn":"07:25","ActualTimeOut":"07:40"},"2":{"StartDate":"04/09/2012 08:00:00","EndDate":"04/09/2012 08:40:00","Duration":"40","CallStatusID":"1","CallStatusName":"Completed","ClientSurname":"Weaver","ClientForename":"Margaret","NeedName":"KEY SAFE, Morning Call - Get Up And Breakfast, Personal Care","CarerAwayReason":"","CallID":"286bee6d-3994-4785-998f-7ea02bd871c4","DoubleUpCallID":"","ActualTimeIn":"08:19","ActualTimeOut":"08:59"},"3":{"StartDate":"04/09/2012 08:08:00","EndDate":"04/09/2012 00:00:00","Duration":"0","CallStatusID":"0","CallStatusName":"Unknown or incomplet","ClientSurname":"Brown","ClientForename":"Margaret","NeedName":"","CarerAwayReason":"","CallID":"d02a6072-bbdd-481d-b9da-f057180ce2ea","DoubleUpCallID":"","ActualTimeIn":"08:08","ActualTimeOut":"00:00"},"4":{"StartDate":"04/09/2012 09:00:00","EndDate":"04/09/2012 09:20:00","Duration":"20","CallStatusID":"1","CallStatusName":"Completed","ClientSurname":"Howarth","ClientForename":"Edith","NeedName":"Change Incontinence Pads, KEY SAFE, Make Bed / Change As Neccessary, Meds - Level II Administer, Personal Care, Prepare Breakfast, TIME BAND - Late Breakfast","CarerAwayReason":"","CallID":"3199a956-647e-4737-9ca3-fea6356f807e","DoubleUpCallID":"","ActualTimeIn":"09:43","ActualTimeOut":"10:19"},"5":{"StartDate":"04/09/2012 09:00:00","EndDate":"04/09/2012 09:50:00","Duration":"50","CallStatusID":"1","CallStatusName":"Completed","ClientSurname":"Kastik","ClientForename":"Lydia","NeedName":"Keys Needed, Leave A Drink, Leave A Sandwich For Later, Make A Hot Drink, Make Bed / Change As Neccessary, Medication Prompt, Meds - Level II Administer, Personal Care, Shower, TIME BAND - Early Breakfast","CarerAwayReason":"","CallID":"71fd1754-9f15-4898-90cf-3af5c0c06e6e","DoubleUpCallID":"","ActualTimeIn":"09:07","ActualTimeOut":"09:35"},"6":{"StartDate":"04/09/2012 10:30:00","EndDate":"04/09/2012 11:00:00","Duration":"30","CallStatusID":"1","CallStatusName":"Completed","ClientSurname":"Terett","ClientForename":"Mamie","NeedName":"Assist With Dressing, Meds - Self Medicating, Morning Call, Personal Care, Strip Wash, TIME BAND - Late Breakfast","CarerAwayReason":"","CallID":"84fb31f7-507d-4a6b-a1d4-ae1ca5122a35","DoubleUpCallID":"","ActualTimeIn":"10:26","ActualTimeOut":"11:37"},"7":{"StartDate":"04/09/2012 12:00:00","EndDate":"04/09/2012 12:15:00","Duration":"15","CallStatusID":"1","CallStatusName":"Completed","ClientSurname":"Garland","ClientForename":"Mavis","NeedName":"Empty Catheter Bag, Medication Prompt, Meds - Level II Administer, TIME BAND - Early Lunch, Toiletting / Commode","CarerAwayReason":"","CallID":"d36d8bcf-8993-4529-99bb-9b16798f1dfe","DoubleUpCallID":"","ActualTimeIn":"11:51","ActualTimeOut":"12:07"},"8":{"StartDate":"04/09/2012 12:30:00","EndDate":"04/09/2012 12:50:00","Duration":"20","CallStatusID":"1","CallStatusName":"Completed","ClientSurname":"Weaver","ClientForename":"Margaret","NeedName":"KEY SAFE, Lunch Call, Make A Hot Drink, Make Lunch","CarerAwayReason":"","CallID":"6cc0c046-79a8-4