java Android:不确定为什么我的方法不返回ArrayList<HashMap<String,String>>
嘿,我的问题是,在AsyncTask方法doInBackground中,我定义了一个返回类型ArrayList<HashMap<String, String>>
,然后在代码中,我返回类型为ArrayList<HashMap<String, String>>
的menuItems。我看到过类似的情况,它说不是所有出口都支持返回ArrayList<HashMap<String, String>>
,但看看我的代码,除了ArrayList<HashMap<String, String>>
之外,我看不到我在哪里返回任何东西。任何帮助都将非常宝贵。这是我的背景代码
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/my安卓stuff 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;
}
# 1 楼答案
在
try {}
块中不返回任何内容:这意味着如果没有
IOException
被引发,则不会返回任何内容似乎你的
catch (IOException ex)
子句中有太多逻辑,这可能表明你应该重构代码(试着从这个catch子句中去掉整个menuItems
)编辑以下重新评估的代码:
让我们看一下你的最后一个catch块,这也是你的方法的结尾:
在try块中,您确实返回
menuItems
(类型为ArrayList<HashMap<String, String>>
),但在for循环中执行menuItems
,该循环将在将menuItems
对象返回给调用方之前重复一次,从而结束方法调用(因为它可能不是您想要的,所以您应该将menuItems
从for循环中取出)然后考虑一下,如果在返回
menuItems
对象之前(之后不能发生)抛出一个IOException,会发生什么?该异常将在you catch子句中被捕获,在该子句中,您抛出另一个异常只是为了在打印当前堆栈跟踪之后立即捕获它,然后什么都没有。函数将在不返回任何编译器所抱怨的内容的情况下结束因此,无论发生什么情况,您都应该找到一种方法来获取返回的
menuItems
(提示:由于java变量是块作用域的,所以从for循环中获取menuItems
声明可能还不够。但是,您在上一行代码中做得很好!)