我有一个我最初用Java创建的程序,现在我正在尝试将其格式化为Android应用程序。该程序在Java控制台中正如我所希望的那样工作,但在我作为Android应用程序运行时却不行。
这是我的活动类的相关代码:
public class ViewTeamStatsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_team_stats);
ArrayList<Team> teams = new ArrayList<Team>();
//Want to make sure there is a network available.
//This always returns true since I added the relevant permissions in the manifest
if (isNetworkAvailable()) {
Main m = new Main();
teams = m.download();
}
因此它调用我的Main类,最终调用Retrieve类。然后,该类应该从特定URL检索HTML并将内容解析为可用数据并将其作为ArrayList返回。
//Retrieves the HTML from the specified URL and then parses it into usable data.
public ArrayList<Game> Retrieve(String url) {
this.url = url;
ArrayList<Game> data = new ArrayList<Game>();
try {
URL my_url = new URL(url);
URLConnection conn = my_url.openConnection();
//The following returns null when run "as an Android Application" but not in the Java console. Why?
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sort.addBufferedReader(br);
data = sort.sort();
} catch (Exception ex) {
ex.printStackTrace();
}
return data;
}
所以我想我的问题是,如果程序在Android上运行,为什么BufferedReader'br'为null?我的代码丢失了什么?
注意:我已经添加了相关权限,因此这不是问题。
答案 0 :(得分:0)