我需要存储从http检索到的内容负载。我创建了一个方法来执行内容检索。但我需要将它存储在一个声明为外部的数组中。我有麻烦做返回值。从公共方法向私有数组传递值
我的问题是:
1)我在哪里把我return语句?
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()};
}
感谢
:您可以ArrayList然后转换成一个阵列。我想我坚持使用字符串数组。因为它来自我得到的一个例子。我如何将字符串存储到字符串数组中?我有一个String []数组;必须将String xyz存储到数组中。并返回值的错误。但是,如果我想更改为ArrayList,其他类文件中的代码是否会有任何更改? – Hend