【发布时间】:2014-08-18 13:15:07
【问题描述】:
嘿,我的问题是,在我的 AsyncTask 方法 doInBackground 中,我定义了一个返回类型为 ArrayList<HashMap<String, String>>,然后在我的代码中,我返回了类型为 ArrayList<HashMap<String, String>> 的 menuItems。我见过类似的情况,它说并非所有出口都支持返回ArrayList<HashMap<String, String>>,但查看我的代码,我看不到除了ArrayList<HashMap<String, String>> 以外的任何地方,任何帮助都会非常感激。这是我的 doInBackground 代码
protected ArrayList<HashMap<String, String>> doInBackground(String...petrolPriceURL){
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
{
for(int i = 0; i < 100; i++){
publishProgress(1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String urlString = petrolPriceURL.toString();
String result = "";
InputStream anInStream = null;
int response = -1;
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
return null;
}
URLConnection conn = null;
try {
conn = url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
// Check that the connection can be opened
if (!(conn instanceof HttpURLConnection))
try {
throw new IOException("Not an HTTP connection");
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
try
{
// Open connection
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
// Check that connection is OK
if (response == HttpURLConnection.HTTP_OK)
{
// Connection is OK so open a reader
anInStream = httpConn.getInputStream();
InputStreamReader in= new InputStreamReader(anInStream);
BufferedReader bin= new BufferedReader(in);
// Read in the data from the RSS stream
String line = new String();
while (( (line = bin.readLine())) != null)
{
result = result + "\n" + line;
Log.v(TAG, "index=" + result);
}
}
}
catch (IOException ex)
{
try {
throw new IOException("Error connecting");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Handler parser = new Handler();
String xml = result.toString(); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_FUEL);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_HIGHEST, parser.getValue(e, KEY_HIGHEST));
map.put(KEY_AVERAGE, parser.getValue(e, KEY_AVERAGE));
map.put(KEY_LOWEST, "Rs." + parser.getValue(e, KEY_LOWEST));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
// adding HashList to ArrayList
menuItems.add(map);
}
return menuItems;
}
}
}
我得到的错误是
This method must return a result of type ArrayList<HashMap<String,String>> PetrolPriceActivity.java /PetrolpriceTestProject/src/org/me/myandroidstuff line 78
重新评估的代码:
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String...petrolPriceURL) {
for(int i = 0; i < 100; i++) {
publishProgress(1);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String urlString = petrolPriceURL.toString();
String result = "";
InputStream anInStream = null;
int response = -1;
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
return null;
}
URLConnection conn = null;
try {
conn = url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
// Check that the connection can be opened
if (!(conn instanceof HttpURLConnection)) {
try {
throw new IOException("Not an HTTP connection");
} catch (IOException e) {
// TODO Auto-generated catch block
return null;
}
}
try {
// Open connection
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
// Check that connection is OK
if (response == HttpURLConnection.HTTP_OK) {
// Connection is OK so open a reader
anInStream = httpConn.getInputStream();
InputStreamReader in= new InputStreamReader(anInStream);
BufferedReader bin= new BufferedReader(in);
// Read in the data from the RSS stream
String line = new String();
while (( (line = bin.readLine())) != null) {
result = result + "\n" + line;
Log.v(TAG, "index=" + result);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
Handler parser = new Handler();
String xml = result.toString(); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_FUEL);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_HIGHEST, parser.getValue(e, KEY_HIGHEST));
map.put(KEY_AVERAGE, parser.getValue(e, KEY_AVERAGE));
map.put(KEY_LOWEST, "Rs." + parser.getValue(e, KEY_LOWEST));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
// adding HashList to ArrayList
menuItems.add(map);
return menuItems;
}
}
}
} catch (IOException ex) {
try {
throw new IOException("Error connecting");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//return menuItems;
}
【问题讨论】:
-
只有在出现
IOException时才会返回List... -
所有代码中的第一个应该是编译时异常,因为我可以在代码开始时有两个
{。同样,在您的第一个try块内的线程睡眠时间结束后,您不会返回任何内容。代码中的最后一个 return 语句来自您的catch块内 -
Deb 是我的代码而不是我的 catch 块......
-
catch (IOException ex) { try { throw new IOException("Error connected"); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }
-
这不是我的catch块结束了吗?等等,我想我明白你的意思了。我试试看
标签: java android arraylist android-asynctask