【发布时间】:2011-11-27 10:44:45
【问题描述】:
我需要存储从 http 检索到的大量内容。我创建了一种检索内容的方法。但我需要将它存储在一个在外部声明的数组中。我在做返回值时遇到了麻烦。
我的问题是:
1)我在哪里放置退货声明?
2)如何将 searchInfo 中的内容存储到数组 mStrings[] 中?
这是我的代码。
public class MainActivity extends Activity
{
ListView list;
Adapter adapter;
private static final String targetURL ="http://www.google.com/images";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView)findViewById(R.id.list);
adapter=new Adapter(this, mStrings);
list.setAdapter(adapter);
}
public String searchInfo()
{
try {
// Get the URL from text box and make the URL object
URL url = new URL(targetURL);
// Make the connection
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
// Read the contents line by line (assume it is text),
// storing it all into one string
String content ="";
String line = reader.readLine();
Pattern sChar = Pattern.compile("&.*?;");
Matcher msChar = sChar.matcher(content);
while (msChar.find()) content = msChar.replaceAll("");
while (line != null) {
if(line.contains("../../"))
{
content += xyz;
line = reader.readLine();
}
else if (line.contains("../../") == false)
{
line = reader.readLine();
}
}
// Close the reader
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private String[] mStrings={searchImage()};
}
【问题讨论】:
标签: java android arrays class methods