我的应用程序中有一个RSS阅读器。但是我热衷于将线程从主I / O移开并使其成为异步。
我真的被卡住了。
我知道这可以通过几行简单的方式完成,但我对如何实现它感到非常困惑。
这是填充列表视图的代码: -
package co.uk.androidreader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class CustomizedListView extends Activity {
Intent intent = getIntent();
// All static variables
static final String URL = "URL of RSS Feed";
// XML node keys
static final String KEY_ARTICLE = "article"; // parent node
//static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_STRAPLINE = "strapline";
static final String KEY_DETAIL = "detail";
static final String KEY_THUMB_URL = "image";
ListView list;
LazyAdapter adapter;
CustomizedListView thisReference = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ARTICLE);
// looping through all song nodes <song>
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_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_STRAPLINE, parser.getValue(e, KEY_STRAPLINE));
map.put(KEY_DETAIL, parser.getValue(e, KEY_DETAIL));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(thisReference, DetailsActivity.class);
intent.putExtra(KEY_TITLE, (String) ((Map)adapter.getItem(position)).get(KEY_TITLE));
intent.putExtra(KEY_DATE, (String) ((Map)adapter.getItem(position)).get(KEY_DATE));
intent.putExtra(KEY_DETAIL, (String) ((Map)adapter.getItem(position)).get(KEY_DETAIL));
startActivity(intent);
}
});
}
public void GoToHome(View v)
{
Intent myIntent = new Intent(CustomizedListView.this, MainActivity.class);
//myIntent.putExtra("communityName", (String)getIntent().getExtras().get(CustomizedListViewCommunity.KEY_NAME));
startActivityForResult(myIntent, 0);
}
public void GoToBack(View v)
{
Intent myIntent = new Intent(CustomizedListView.this, MainActivity.class);
//myIntent.putExtra("communityName", (String)getIntent().getExtras().get(CustomizedListViewCommunity.KEY_NAME));
startActivityForResult(myIntent, 0);
}
}
答案 0 :(得分:1)
这是onCreate方法的一个barbones实现...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//define the task as an anonymous type (you could create a concrete inner class as well)
AsyncTask<Void, Void, ArrayList<HashMap<String,String>>> task = new AsyncTask<Void, Void, ArrayList<HashMap<String,String>>>() {
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
Void... params) {
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ARTICLE);
// looping through all song nodes <song>
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_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_STRAPLINE, parser.getValue(e, KEY_STRAPLINE));
map.put(KEY_DETAIL, parser.getValue(e, KEY_DETAIL));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
return songsList;
}
@Override
protected void onPostExecute(
ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
ListView list=(ListView)CustomizedListView.this.findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
list.setAdapter(new LazyAdapter(this, result));
}
};
//execute the task
task.execute((Void[])null);
list=(ListView)findViewById(R.id.list);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(thisReference, DetailsActivity.class);
intent.putExtra(KEY_TITLE, (String) ((Map)adapter.getItem(position)).get(KEY_TITLE));
intent.putExtra(KEY_DATE, (String) ((Map)adapter.getItem(position)).get(KEY_DATE));
intent.putExtra(KEY_DETAIL, (String) ((Map)adapter.getItem(position)).get(KEY_DETAIL));
startActivity(intent);
}
});
}
答案 1 :(得分:0)
希望这会有所帮助:
AsyncTask<String, Void, String> worker = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... param) {
// here goes the code you want off the main thread
return "the result of this code that goes on onPostExecute";
}
@Override
protected void onPostExecute(String str) {
super.onPostExecute(str);
// here goes the code that will run after you doInBackground has finished working
}
};
worker.execute("any param you want");
答案 2 :(得分:0)
任何网络任务都无法在mainthread上运行,因此我们需要一个asynctask。
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null; // THIS IS WHERE THE INFO IS SAVED
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString("UTF-8");
}
else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}
catch (ClientProtocolException e) {
e.printStackTrace();
//Handle httprequest when answer is not expected
}
catch (IOException e) {
e.printStackTrace();
//Handle httprequest when answer is not expected
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
//handle your result here and any catches
}
}
}
然后你用
new RequestTask().execute("http://www.yourapilocation.com/whatever");
调用该方法。
希望这有帮助。