在Android Studio中运行两个异步任务
有人能帮我修改代码吗 显然,问题是我不能在主线程上运行多个异步任务。谁能给我一些关于如何修复代码的建议? 谢谢大家! 我很抱歉没有评论我的代码。如果你们在读的时候感到困惑,我可以解释在Android Studio中运行两个异步任务,android,android-studio,android-asynctask,Android,Android Studio,Android Asynctask,有人能帮我修改代码吗 显然,问题是我不能在主线程上运行多个异步任务。谁能给我一些关于如何修复代码的建议? 谢谢大家! 我很抱歉没有评论我的代码。如果你们在读的时候感到困惑,我可以解释 public class MainActivity extends AppCompatActivity { ListView listView; private SQLiteDatabase myDatabase; private Cursor cursor; boolean f
public class MainActivity extends AppCompatActivity {
ListView listView;
private SQLiteDatabase myDatabase;
private Cursor cursor;
boolean finished = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
myDatabase = this.openOrCreateDatabase("HackerNews", MODE_PRIVATE, null);
Cursor cursor = myDatabase.rawQuery("SELECT * FROM ids", null);
int index = cursor.getColumnIndex("urlID");
cursor.moveToFirst();
DownloadContent content = new DownloadContent();
while(cursor != null){
String newUrl = "https://hacker-news.firebaseio.com/v0/item/" + cursor.getString(index) + ".json?print=pretty";
content.execute(newUrl);
cursor.moveToNext();
}
}
public class DownloadIDs extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
int data = reader.read();
while (data >= 0) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
return "Fail";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS ids (id INTEGER PRIMARY KEY, urlID VARCHAR)");
cursor = myDatabase.rawQuery("SELECT COUNT(*) FROM ids", null);
cursor.moveToFirst();
int count = cursor.getInt(0);
if (!(count > 0)) {
try {
JSONArray ids = new JSONArray(s);
for (int i = 0; i < ids.length(); i++) {
myDatabase.execSQL("INSERT INTO ids (urlID) VALUES ('" + ids.getString(i) + "')");
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.i("message", "TABLE1 IS NOT EMPTY");
}
}
}
public class DownloadContent extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
int data = reader.read();
while (data >= 0) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS content(id INTEGER PRIMARY KEY, title VARCHAR, url VARCHAR)");
cursor = myDatabase.rawQuery("SELECT COUNT(*) FROM content", null);
cursor.moveToFirst();
int count = cursor.getInt(0);
if (!(count > 0)) {
try {
JSONObject jsonObject = new JSONObject(s);
String title = jsonObject.getString("title");
String url = jsonObject.getString("url");
myDatabase.execSQL("INSERT INTO content (title, url) VALUES('" + title +"','" + url + "')");
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.i("mess2", "table 2 is NOT EMPTY");
}
}
}
}
public类MainActivity扩展了AppCompatActivity{
列表视图列表视图;
私有SQLiteDatabase-myDatabase;
私有游标;
布尔完成=假;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(listView)findViewById(R.id.listView);
myDatabase=this.openOrCreateDatabase(“HackerNews”,MODE_PRIVATE,null);
Cursor Cursor=myDatabase.rawQuery(“SELECT*FROM-id”,null);
int index=cursor.getColumnIndex(“urlID”);
cursor.moveToFirst();
DownloadContent=新的DownloadContent();
while(光标!=null){
字符串newUrl=”https://hacker-news.firebaseio.com/v0/item/“+cursor.getString(index)+”.json?print=pretty”;
content.execute(newUrl);
cursor.moveToNext();
}
}
公共类下载ID扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串结果=”;
网址;
HttpURLConnection-urlConnection=null;
试一试{
url=新url(参数[0]);
urlConnection=(HttpURLConnection)url.openConnection();
InputStream InputStream=urlConnection.getInputStream();
InputStreamReader reader=新的InputStreamReader(inputStream);
int data=reader.read();
而(数据>=0){
当前字符=(字符)数据;
结果+=电流;
data=reader.read();
}
返回结果;
}捕获(例外e){
e、 printStackTrace();
返回“失败”;
}
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
execSQL(“如果不存在创建表id(id INTEGER主键,urlID VARCHAR)”;
cursor=myDatabase.rawQuery(“从ids中选择计数(*),null);
cursor.moveToFirst();
int count=cursor.getInt(0);
如果(!(计数>0)){
试一试{
JSONArray ID=新JSONArray;
对于(int i=0;i=0){
当前字符=(字符)数据;
结果+=电流;
data=reader.read();
}
返回结果;
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
execSQL(“如果不存在创建表的内容(id整数主键,title VARCHAR,url VARCHAR)”;
cursor=myDatabase.rawQuery(“从内容中选择计数(*),null);
cursor.moveToFirst();
int count=cursor.getInt(0);
如果(!(计数>0)){
试一试{
JSONObject JSONObject=新的JSONObject;
String title=jsonObject.getString(“title”);
字符串url=jsonObject.getString(“url”);
execSQL(“插入内容(标题、url)值(““+title+”,“+url+”)”);
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.i(“mess2”,“表2不为空”);
}
}
}
}
每个异步任务
实例只能运行一次。解决这个问题的最简单方法是在需要运行时创建一个新实例
while(cursor != null) {
String newUrl = "https://hacker-news.firebaseio.com/v0/item/" + cursor.getString(index) + ".json?print=pretty";
new DownloadContent().execute(newUrl);
cursor.moveToNext();
}
每个
AsyncTask
实例只能运行一次。解决这个问题的最简单方法是在需要运行时创建一个新实例
while(cursor != null) {
String newUrl = "https://hacker-news.firebaseio.com/v0/item/" + cursor.getString(index) + ".json?print=pretty";
new DownloadContent().execute(newUrl);
cursor.moveToNext();
}
谢谢这就解决了问题。我试着接受你的回答,但是网站不允许。谢谢!这就解决了问题。我试图接受您的回答,但网站不允许。请注意,所有AsyncTasks共享相同的后台线程,因此第二个网络请求将被阻止,直到第一个竞争。默认情况下,Async Tasks使用串行执行器,请参阅此说明,所有AsyncTasks共享相同的后台线程,因此,第二个网络请求将被阻止,直到第一个竞争。异步任务默认使用串行执行器,请参考此