嘿,我的问题是,在我的 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;
}
最佳答案
您没有在 try {}
中返回任何内容 block :
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);
}
}
}
这意味着如果没有 IOException
被提出,没有任何返回。
看来你的 catch (IOException ex)
逻辑太多了可能表明您应该重构代码的子句(尝试从这个 catch 子句中获取整个 menuItems
东西)。
编辑以下重新评估的代码:
让我们看一下最后的 try-catch block ,这也是您的方法的结尾:
try {
// Open connection
HttpURLConnection httpConn = (HttpURLConnection) conn;
// remainder omitted
} catch (IOException ex) {
try {
throw new IOException("Error connecting");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//return menuItems;
在 try block 中你返回 menuItems
(类型为 ArrayList<HashMap<String, String>>
)但是您在 within 一个 for 循环中执行此操作,该循环最终会在返回 menuItems
之前循环迭代一次。向调用者发送对象,结束方法调用(因为这可能不是您想要的,您应该从 for 循环中获取 menuItems
)。
然后想想如果在 menuItems
之前 抛出 IOException 会发生什么返回对象(之后不会发生)?异常将在你的 catch 子句中被捕获,你抛出另一个异常只是为了在打印当前堆栈跟踪之后立即捕获它然后...... nothing。该函数将在不返回任何内容的情况下结束,这正是编译器所提示的。
所以你应该想办法得到menuItems
无论如何都会返回(提示:由于 java 变量是 block 作用域的,因此从 for 循环中获取 menuItems
声明可能还不够。但是您的最后一行代码行在良好的轨道上!)
关于java - Android:不确定为什么我的方法没有返回 ArrayList<HashMap<String, String>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25364254/