我有一个需要执行的AsyncTask代码块,但它给了我一些错误。
我得到的错误是:
无法解析符号“执行”
和:
未知类:“ URL”
即使我声明了字符串“ URL”。我非常确定问题在于它无法以某种方式执行AsyncTask
。
这是我需要执行的AsyncTask代码:
package com.jasperhonkasalo.merenpinnankorkeusvsa;
import android.os.AsyncTask;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.xml.namespace.*;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class Testclass {
public static class NamespaceResolver implements NamespaceContext {
private Document document;
public NamespaceResolver(Document doc) {
document = doc;
}
public String getNamespaceURI(String prefix) {
if (prefix.equals("")) {
return document.lookupNamespaceURI(null);
} else {
return document.lookupNamespaceURI(prefix);
}
}
public String getPrefix(String namespaceURI) {
return document.lookupPrefix(namespaceURI);
}
public Iterator<String> getPrefixes(String namespaceURI) {
return null;
}
}
public static Document createDocumentFromString(String xml) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
}
public static class getAndReturnValues extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... url) {
StringBuilder sb = new StringBuilder();
try (BufferedReader r = new BufferedReader(new InputStreamReader(new URL(url.toString()).openStream(), "UTF-8"))) {
String line;
while ((line = r.readLine()) != null) {
sb.append(line + "\n");
}
} catch (Exception e) {
return e.toString();
}
return sb.toString();
}
}
}
这是我尝试使用以下代码调用AsyncTask
的代码:
String url = "http://opendata.fmi.fi/wfs/fin?service=WFS&version=2.0.0&request=GetFeature&storedquery_id=fmi::observations::mareograph::timevaluepair&fmisid=134223&";
public TestClass myTask = new TestClass();
myTask.execute(url)