Android widget asynctask - java

I have an android app with widget which should parse some rss and get some data.
Here is my onReceive method:
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context,intent);
if (intent.getAction().equals(ACTION_TOAST)) {
new RetrieveTask().execute(context);
}
}
And async method itself:
class RetrieveTask extends AsyncTask<Context,Void, List<String>> {
private Exception exception;
private Context context;
private RemoteViews views;
#Override
protected List<String> doInBackground(Context... params) {
List<String> output = new ArrayList<String>();
try {
int k = 15;
doc = Jsoup.connect("http://vlg-media.ru/transport").get();
String img = "";
int i = 0;
Title = doc.select(".art_img");
for (Element titles : Title) {
String IMG = URL;
String HREF;
//Element Img = titles.select(".art_image").first();
IMG = "http://vlg-media.ru" + titles.select("img").attr("src").toString();
IMG = IMG.replace("small", "medium");
HREF = "http://vlg-media.ru" + titles.select("a").attr("href").toString();
// Element Title = titles.select(".con_titlelink").first();
String title = titles.select("img").attr("alt").toString();
m = new HashMap<String, Object>();
m.put(ATTRIBUTE_NAME_TITLE, title);
//добавление данных в наш контейнер
data.add(m);
publishProgress();
i++;
//}
if (i == k)
break;
}
//}
} catch (Exception ex) {
this.exception = ex;
return null;
}
return output;
}
protected void onPostExecute(List<String> output) {
//to make sure it run
Toast.makeText(context,"Done",Toast.LENGTH_LONG).show();
}
}
After doInBackground happens nothing. onPostExecute never run.
I just need to run async parser in my AppWidgetProvider. Don't know where is the problem.

you never assign the Context object. In your case is just
context = params[0];
as first line of doInBackground

Related

Callable doesn't return anything from class

I'm doing an App that scrapes a website an get some images, that works fine, I created an ThreadPoolExecutor and some callable but when I try to get the results from a Callable that I created I'm unnable to, it goes to the ExecutionException.
This is the Scraper Class:
public class ImageScraper implements Callable<String>{
Context context = null;
public Logo logoActivity;
Document[] doc = new Document[1];
List<ImageObject> imageList = new ArrayList<ImageObject>();
int pagAnterior;
String url;
int pag;
public ImageScraper(Logo act, String url, int pag, int pagAnterior) {
this.logoActivity = act;
this.url = url;
this.pag = pag;
this.pagAnterior = pagAnterior;
context = act.getApplication();
}
#Override
public String call() throws Exception {
getResponse(url,pag);
getImages(doc[0]);
Log.i("listaaa", "listaa : "+imageList.size());
String something = "got something";
return something;
}
public void getImages(Document docfinal) {
Log.i("Documento1", "documento1 : "+docfinal);
Elements entradas = docfinal.select("img[src]");
Elements titulo = doc[0].select("title");
String tituloPagina = titulo.text();
String urlImage = "";
if(!tituloPagina.toLowerCase().contains("page "+pagAnterior)) {
for (Element elem : entradas) {
if (elem.attr("abs:src").toLowerCase().contains("mfiles")) {
urlImage = elem.attr("abs:src").replace("thumb-", "");
Log.i("GridVerticalFragment", "Pillando url: " + urlImage);
ImageObject image = new ImageObject(urlImage);
Log.i("GridVerticalFragment", "Url Pillada: " + image.getUrl());
imageList.add(image);
}
}
}
Log.i("Logo", "Lista2: "+imageList.size());
}
public void getResponse(String urlRoot, int pagina) {
Log.i("GridVerticalLayaout", "Pagina: "+pagina);
String url;
String urlFinal = "";
if(pagina==0){
url = urlRoot;
urlFinal = url;
}else{
url = urlRoot.concat("?page="+Integer.toString(pagina));
urlFinal = url;
}
RequestQueue rq = Volley.newRequestQueue(context);
Log.i("GridVerticalLayaout", "fuuck: "+url);
Log.i("GridVerticalLayaout", "lool: "+urlFinal);
StringRequest stringRequest = new StringRequest(Request.Method.GET, urlFinal,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Do something with the response
doc[0] = Jsoup.parse(response);
getImages(doc[0]);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle error
Log.e("ERROR", "Error occurred ", error);
}
});
rq.add(stringRequest );
}
}
And this is the main class:
public class Logo extends AppCompatActivity {
public List<ImageObject> GlobalImageList = new ArrayList<ImageObject>() ;
Document[] doc = new Document[1];
String url;
int pagAnterior = 0;
int i = 0;
Context context = null;
public ThreadPoolExecutor executor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logo);
getSupportActionBar().hide();
Collection<Callable<String>> callableList = new ArrayList<>();
context = getApplication();
int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
executor = new ThreadPoolExecutor(
NUMBER_OF_CORES*2,
NUMBER_OF_CORES*2,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>()
);
while(i<4){
callableList.add(new ImageScraper(this,"www.google.es,i,i-1););
i++;
}
List<Future<String>> result = null;
try {
result = executor.invokeAll(callableList);
} catch (InterruptedException e) {
Log.i("Fallo", "Fallo");
e.printStackTrace();
}
for (Future a : result) {
String SingleImageList = null;
try {
SingleImageList = (String) a.get();
Log.i("Single", "Single"+SingleImageList);
} catch (InterruptedException e) {
Log.i("Fallo3", "Fallo3");
e.printStackTrace();
} catch (ExecutionException e) {
Log.i("Fallo2", "Fallo2");
e.printStackTrace();
}
}
}
}
This doesn't return anything but the scraper do his job,( the getImages() and getResponse() do the job and updates the list , instead if in this part of the code in MainClass:
while(i<4){
callableList.add(new ImageScraper(this,"www.google.es,i,i-1););
i++;
}
If I change that for this( the class is not called the callable is created in the class), it works, the string is returned:
while(i<4){
callableList.add(new Callable<String>() {
public String call() throws Exception {
return "haha";
}
});
i++;
}
Someone can help me with this? I've been reading a lot and according to what I've read, what I have in the Scraper class is fine, but I still don't get it.
Sorry for the bad english, Im trying to :), I don't want to return an string I want to return an ArrayList, im returning a string in the code because thought it was a problem for returning ArrayList, but it seems like that is not that, and again, Thanks!
Please check this line of code
while(i<4){
callableList.add(new ImageScraper(this,"www.google.es,i,i-1););
i++;
}
You are not closing the double quotes!
Instead, try this code
while(i<4){
callableList.add(new ImageScraper(this,"www.google.es",i,i-1););
i++;
}

How to merge, create tags and put an ascending order the feeds of more RSS Feed links like on Yahoo Pipes?

I need to take the feeds of more RSS Feed urls that I have to aggregate and show the news in descending order.
Some links don't have an xml tag for <category> so I need to create one: I have to know where feeds come from so that I can categorize them.
This is what I'm trying to achieve: ( for Source n I mean the category)
[![enter image description here][1]][1]
I used to use Yahoo!Pipes to make all these changes.
My attempt was to create a CustomListView for each url and then execute the AsyncTasks all at once but that is not working correctly - the feeds are not displayed in ascending order.
MainActivity
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private RSSFeed myRssFeed = null;
String[] urlFeed = {"url1", "url2"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
for (int i = 0; i < urlFeed.length; i++) {
News news = new News(i);
news.execute();
}
}
#Override
public void onRefresh() {
for (int i = 0; i < urlFeed.length; i++) {
News news = new News(i);
news.execute();
}
}
private class News extends AsyncTask<Object, Void, Void> {
protected int number;
public News(int urlNumber) {
number = urlNumber;
}
#Override
protected Void doInBackground(Object... arg0) {
String data = "";
InputStream iStream;
try{
URL url = new URL(urlFeed[number]);
// Creating an http connection to communicate with url
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuilder sbf = new StringBuilder();
String line;
while( ( line = br.readLine()) != null){
sbf.append(line);
}
data = sbf.toString();
br.close();
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(url.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (ParserConfigurationException | IOException | SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (myRssFeed != null) {
NestedListView list = (NestedListView)findViewById(android.R.id.list);
list.setVisibility(View.VISIBLE);
CustomList adapter = new CustomList(MainActivity.this, myRssFeed.getList());
adapter.addAll();
list.setAdapter(adapter);
} else
Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_LONG).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
RSSFeed
public class RSSFeed {
private String title = null;
private String description = null;
private String link = null;
private String pubdate = null;
private String image = null;
private String enclosure = null;
private String author = null;
private List<RSSItem> itemList;
RSSFeed(){
itemList = new Vector<RSSItem>(0);
}
void addItem(RSSItem item){
itemList.add(item);
}
RSSItem getItem(int location){
return itemList.get(location);
}
List<RSSItem> getList(){
return itemList;
}
}
RSSItem
public class RSSItem {
private String title = null;
private String description = null;
private String link = null;
private String pubdate = null;
private String image = null;
private String enclosure = null;
private String author = null;
}
CustomList
public class CustomList extends ArrayAdapter<RSSItem> {
private static Activity context = null;
private final List<RSSItem> web;
public CustomList(Activity context, List<RSSItem> web) {
super(context, R.layout.new_listview, web);
CustomList.context = context;
this.web = web;
}
#SuppressLint("SetTextI18n")
#Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
#SuppressLint({"ViewHolder", "InflateParams"}) final View rowView = inflater.inflate(R.layout.new_listview, null, true);
ImageView imageView = (ImageView)rowView.findViewById(R.id.image);
Picasso.with(context).load(web.get(position).getImage()).into(imageView);
TextView textView = (TextView)rowView.findViewById(R.id.title);
textView.setText(Html.fromHtml(web.get(position).getTitle()));
TextView textView1 = (TextView)rowView.findViewById(R.id.description);
textView1.setText(web.get(position).getDescription());
TextView textView2 = (TextView)rowView.findViewById(R.id.pubdate);
textView2.setText(pubdate);
return rowView;
}
}
RSSHandler
public class RSSHandler extends DefaultHandler {
final int state_unknown = 0;
final int state_title = 1;
final int state_description = 2;
final int state_link = 3;
final int state_pubdate = 4;
final int state_enclosure = 6;
final int state_image = 5;
final int state_author = 7;
int currentState = state_unknown;
String url;
RSSFeed feed;
RSSItem item;
boolean itemFound = false;
RSSHandler(){
}
RSSFeed getFeed(){
return feed;
}
#Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
feed = new RSSFeed();
item = new RSSItem();
}
#Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("item")){
itemFound = true;
item = new RSSItem();
currentState = state_unknown;
}
else if (localName.equals("enclosure")) {
url = attributes.getValue("url");
currentState = state_image;
}
else if (localName.equalsIgnoreCase("title")){
currentState = state_title;
}
else if (localName.equalsIgnoreCase("description")){
currentState = state_description;
}
else if (localName.equalsIgnoreCase("link")){
currentState = state_link;
}
else if (localName.equalsIgnoreCase("pubdate")){
currentState = state_pubdate;
}
else if (localName.equalsIgnoreCase("author")){
currentState = state_author;
}
else{
currentState = state_unknown;
}
}
#Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("item")){
feed.addItem(item);
}
}
#Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String strCharacters = new String(ch,start,length);
if (itemFound){
// "item" tag found, it's item's parameter
switch(currentState){
case state_enclosure:
item.setEnclosure(strCharacters);
break;
case state_title:
item.setTitle(strCharacters);
break;
case state_image:
item.setImage(url);
break;
case state_description:
item.setDescription(strCharacters);
break;
case state_link:
item.setLink(strCharacters);
break;
case state_pubdate:
item.setPubdate(strCharacters);
break;
case state_author:
item.setAuthor(strCharacters);
break;
default:
break;
}
}
else{
// not "item" tag found, it's feed's parameter
switch(currentState){
case state_enclosure:
feed.setEnclosure(strCharacters);
break;
case state_title:
feed.setTitle(strCharacters);
break;
case state_image:
feed.setImage(url);
break;
case state_description:
feed.setDescription(strCharacters);
break;
case state_link:
feed.setLink(strCharacters);
break;
case state_pubdate:
feed.setPubdate(strCharacters);
break;
case state_author:
feed.setAuthor(strCharacters);
break;
default:
break;
}
}
currentState = state_unknown;
}
The solution requires more than one change. The first thing you need to do is make each News task that finishes adds its data to the one big list. That list can then be sorted which requires a Comparator.
You also need a list of the names to show in the placeholder youve called Source n in your image and lastly, so that you can compare them, you need to parse the dates as actual dates.
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private List<RSSItem> totalRssFeed = new ArrayList<>();
String[] urlFeed = {"MyURL", "MyUrl2", "MyUrl3"};
String[] names = {"Name", "Name2", "Name3"}
private CustomList adapter;
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
ListView list = (ListView) findViewById(android.R.id.list);
adapter = new CustomList(MainActivity.this);
list.setAdapter( adapter );
}
#Override
protected void onResume() {
// do this here and you dont need to repeat it in refresh and create
for (int i = 0; i < urlFeed.length; i++) {
News news = new News(i);
news.execute();
}
}
public class CustomList extends ArrayAdapter<RSSItem> {
...
public CustomList(Activity context) { //dont start with default data
super(context, R.layout.new_listview, null);
}
...
}
private class News extends AsyncTask<Object, Void, Void> {
protected int number;
public News(int urlNumber) {
number = urlNumber;
}
#Override
protected Void doInBackground(Object... arg0) {
try {
//no need to do the input stuff you were doing before here?
URL url = new URL(urlFeed[number]);
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler(names[number]);
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(url.openStream());
myXMLReader.parse(myInputSource);
totalRssFeed.addAll(myRSSHandler.getFeed());
Collections.sort(totalRssFeed);
} catch (ParserConfigurationException | IOException | SAXException e) { /* stuff */ }
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
adapter.clear();
adapter.addAll(totalRssFeed);
}
}
public class RSSItem implements Comparable<RSSItem> {
public String tag;
public Date pubdate; //just public so i didnt need setters in this example
public RSSItem(String defaultTagToUse) {
//You should set the default to whatever you want displayed
//when the tag for <category> is missing, and just update it normally otherwise
tag = defaultTagToUse;
}
void setPubDate(Date date) {
pubdate = date;
}
...
#Override
public int compareTo(#NonNull RSSItem another) {
int res;
if (pubdate == null) {
if (another.pubdate == null) {
res = 0;
} else {
res = -1;
}
} else {
if (another.pubdate == null || pubdate.getTime() > another.pubdate.getTime()) {
res = 1;
} else {
res = -1;
}
}
return res;
}
}
public class RSSHandler extends DefaultHandler {
...
List<RSSItem> feed;
RSSItem item;
String mName;
public RssHandler(String name) {
mName = name;
}
List<RSSItem> getFeed() {
return feed;
}
#Override
public void startDocument() throws SAXException {
feed = new ArrayList<>();
}
#Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("item")) {
itemFound = true;
item = new RSSItem( name );
currentState = state_unknown;
} else ...
}
#Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase("item")) {
feed.add(item);
}
}
#Override
public void characters(char[] ch, int start, int length) throws SAXException {
...
case state_pubdate:
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); //make sure you match the format from your XML
item.setPubdate( formatter.parse(stringCharacters) );
break;
...
}
}
}
All the ...'s jsut mean leave everything as it was in those places

Google Places Details search System.err

I'm attempting to locate a restaurant by name using an AutoCompleteTextView which successfully obtains the places id. I've also checked the http request manually in my browser which responds with the correct information. When this code is executed, a system.err is shown in the LogCat console in Eclipse. My code below;
public class AdvancedSearch extends Activity implements OnItemClickListener {
private static final String LOG_TAG = "com.lw276.justdine";
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
// ------------ make your specific key ------------
private static final String API_KEY = "MyAPIKEY";
private Activity context = this;
static HashMap<String, String> place;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_advanced_search);
final AutoCompleteTextView autoCompView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this,
R.layout.list_item));
autoCompView.setOnItemClickListener(this);
Button btnAdvancedSearch = (Button) findViewById(R.id.advanced_search_btn);
btnAdvancedSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = autoCompView.getText().toString();
if(place.containsKey(str)){
String placeId = place.get(str);
Log.d("advancedSearchBtn: placeId = ", placeId);
Log.i("advancedSearchBtn", "Search button has been pressed");
Intent i = new Intent(context, GoogleMap.class);
i.putExtra("advancedSearch", placeId);
startActivity(i);
} else {
Toast.makeText(context, "Please select an item from the autocomplete list", Toast.LENGTH_SHORT).show();
}
}
});
}
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long id) {
// String str = (String) adapterView.getItemAtPosition(position);
// Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
public static ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE
+ TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_KEY);
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
System.out.println("URL: " + url);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "Error processing Places API URL", e);
return resultList;
} catch (IOException e) {
Log.e(LOG_TAG, "Error connecting to Places API", e);
return resultList;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
resultList = new ArrayList<String>(predsJsonArray.length());
place = new HashMap<String, String>();
for (int i = 0; i < predsJsonArray.length(); i++) {
System.out.println(predsJsonArray.getJSONObject(i).getString(
"description"));
System.out
.println("============================================================");
resultList.add(predsJsonArray.getJSONObject(i).getString(
"description"));
String description = predsJsonArray.getJSONObject(i).getString("description");
String placeId = predsJsonArray.getJSONObject(i).getString("place_id");
place.put( description, placeId);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Cannot process JSON results", e);
}
return resultList;
}
class GooglePlacesAutocompleteAdapter extends ArrayAdapter<String>
implements Filterable {
private ArrayList<String> resultList;
public GooglePlacesAutocompleteAdapter(Context context,
int textViewResourceId) {
super(context, textViewResourceId);
}
#Override
public int getCount() {
return resultList.size();
}
// #Override
// public HashMap<String, String> getItem(int index) {
// return resultList.get(index);
// }
#Override
public String getItem(int index){
return resultList.get(index);
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
}
The googlePlaces example =
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJCSWGvY-FdUgRpGdg10FTIIg&key="MY_API_KEY"
Is it obvious to anyone why no markers are being placed on my map fragment?
Errors from LogCat:
http://pastebin.com/T1AFbw3s
Google Maps and Markers class:
http://pastebin.com/hT2XwuE2
The error you are having is due to parsing the Json incorrectly at line 193 of GoogleMap activity:
JSONArray placesArray = resultObject.getJSONArray("results");
I believe the key is result instead of results.
I can give you a better explanation if I could see the json response. Meanwhile,
I would also suggest you to use Gson Library to parse the json responses to objects instead of mapping them manually. It would be alot easier.
Markers need to be added manually:
private void addMarker(String name, double lat, double long){
LatLng latlong = LatLng.newInstance(lat, long);
MarkerOptions markerOptions = MarkerOptions.newInstance();
markerOptions.setIcon(Icon.newInstance("/img/icon.png"));
markerOptions.setTitle(name);
Marker marker = new Marker(latlong, markerOptions);
map.addOverlay(marker);
}
It turns out that because I was using the same code to add markers onto the Map, when the detail search was used to identify a single place, the JSONArray type was incompatible with the result set... If that makes sense.
I had to separate the two by checking if a nearby search was being performed or a details search. Below was the code for the details search only:
resultObject = resultObject.getJSONObject("result"); // <---
places = new MarkerOptions[1];
LatLng placeLL = null;
String placeName = "";
String vicinity = "";
try {
JSONObject placeObject = resultObject;
JSONObject loc = placeObject.getJSONObject(
"geometry").getJSONObject("location");
placeLL = new LatLng(Double.valueOf(loc
.getString("lat")), Double.valueOf(loc
.getString("lng")));
vicinity = placeObject.getString("vicinity");
placeName = placeObject.getString("name");
} catch (JSONException jse) {
missingValue = true;
jse.printStackTrace();
}
if (missingValue){
result = null;
} else {
places[0] = new MarkerOptions().position(placeLL)
.title(placeName).snippet(vicinity);
}

Calling an AsyncTask from another asynctask in doInBackground method?

I have AsyncTask2 called from AsyncTask1... that's my 'scenario':
AsyncTask1 download an rss feed, parse the xml and for every item recognized create&execute AsyncTask2 inside the doInBackground method.
AsyncTask2 in the doInBackground method download the enclosure url attribute of the item passed from AsyncTask1 and in onPostExecute method add the item to the global items array and notify the change of items to the associated adapter.
It works fine and not crashing, why? AsyncTasks are supposed to run from the UI thread (threading rules) and now I'm a little confused about this supposition.
Sorry for bad english, I hope question is clear enough.
EDIT
Here some code...
DownloadRssAsyncTask = AsyncTask2,
RssAsyncTask = AsyncTask1
public class ParseActivity extends Activity {
public class FeedItemAdapter extends ArrayAdapter<FeedItem> {
int resource;
public FeedItemAdapter(Context context, int resource, List<FeedItem> items) {
super(context, resource, items);
this.resource = resource;
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout myView;
FeedItem item = getItem(position);
if (convertView == null) {
myView = new LinearLayout(getContext());
String inflaterService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater) getContext().getSystemService(inflaterService);
li.inflate(resource, myView, true);
} else {
myView = (LinearLayout) convertView;
}
TextView titleFeedItem = (TextView) myView.findViewById(R.id.itemTitle);
TextView dateFeedItem = (TextView) myView.findViewById(R.id.itemDate);
ImageView imageFeedItem = (ImageView) myView.findViewById(R.id.imageThumb);
titleFeedItem.setText(item.mTitle);
dateFeedItem.setText(item.mPubDate);
imageFeedItem.setImageBitmap(item.bitmapEnclosure);
return myView;
}
}
private class DownloadRssAsyncTask extends AsyncTask<FeedItem, Void, FeedItem> {
#Override
protected FeedItem doInBackground(FeedItem... params) {
FeedItem item = params[0];
if (item.mEnclosure == null) {
Log.i("info: ", "no enclosure tag");
item.bitmapEnclosure = null;
return item;
}
try {
URL imageUrl = new URL(item.mEnclosure);
item.bitmapEnclosure = BitmapFactory.decodeStream(imageUrl.openStream());
} catch (IOException e) {
Log.e("error", "download image resource error: "+item.mEnclosure);
item.bitmapEnclosure = null;
}
return item;
}
#Override
protected void onPostExecute(FeedItem result) {
items.add(result);
arrayAdapter.notifyDataSetChanged();
dbHelper.putItem(result.mGuid, result.mTitle, result.mDescription, result.mEnclosure, result.mPubDate);
}
}
private class RssAsyncTask extends AsyncTask<String, Integer, Void> {
#Override
protected Void doInBackground(String... params) {
int dimParams = params.length;
for (int i=0; i<dimParams; i++) {
Log.i("doInBackground", "rss feed num "+ (i+1) + " of "+ dimParams+ ": " + params[i]);
refreshFeed(params[i]);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
Log.i("onPostExecute in RssAsyncTask", "notifyDataSetChanged");
}
}
public static class FeedItem {
public String mAuthor;
public String mCategory;
public String mComments;
public String mDescription; //r
public String mEnclosure;
public Bitmap bitmapEnclosure;
public String mGuid;
public String mLink; //r
public String mPubDate;
public String mSource;
public String mTitle; //r
public FeedItem() {
// TODO Auto-generated constructor stub
}
#Override
public String toString() {
return
"Data: "+mPubDate+
"\nLink:\n"+mLink+
"\nAutore:\n"+mAuthor+
"\nTitolo:\n"+mTitle+
"\nEnclosure:\n"+mEnclosure;
}
}
private FeedReaderDbHelper dbHelper;
private FeedItemAdapter arrayAdapter;
private ArrayList<FeedItem> items;
private ListView myListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parse);
items = new ArrayList<FeedItem>();
new ArrayList<FeedItem>();
myListView = (ListView) findViewById(R.id.myListView);
arrayAdapter = new FeedItemAdapter(this, R.layout.feed_item, items);
myListView.setAdapter(arrayAdapter);
dbHelper = new FeedReaderDbHelper(this);
//RssAsyncTask: download and parsing rss feed
new RssAsyncTask().execute(getString(R.string.my_feed));
}
public void refreshFeed(String feed) {
final String TAG = "refreshFeed";
Log.i(TAG, feed);
URL url = null;
try {
url = new URL(feed);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
int httpCode = httpConnection.getResponseCode();
if (httpCode == HttpURLConnection.HTTP_OK) {
processFeed(httpConnection.getInputStream());
} else {
Log.i(TAG, httpCode + httpConnection.getResponseMessage());
}
} catch (MalformedURLException e1) {
Log.i(TAG, "MalformedUrlException in " + feed);
} catch (IOException e) {
Log.i(TAG, "IOException in " + url.toString());
}
}
private void processFeed(InputStream inputStream ) {
final String TAG = "processFeed";
final String ITEM = "item";
final String AUTHOR ="author";
final String TITLE ="title";
final String CATEGORY ="category";
final String COMMENTS ="comments";
final String DESCRIPTION ="description";
final String GUID ="guid";
final String LINK ="link";
final String PUBDATE="pubDate";
final String SOURCE ="source";
final String ENCLOSURE = "enclosure";
Log.i(TAG, inputStream.toString());
XmlPullParserFactory pullParserFact;
try {
pullParserFact = XmlPullParserFactory.newInstance();
pullParserFact.setNamespaceAware(true);
XmlPullParser pullParser = pullParserFact.newPullParser();
pullParser.setInput(inputStream, null);
int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG && pullParser.getName().equals(ITEM)){
final FeedItem item = new FeedItem();
eventType = pullParser.next();
while ( !(eventType == XmlPullParser.END_TAG && pullParser.getName().equals(ITEM)) ) {
if ( eventType == XmlPullParser.START_TAG ) {
String name = pullParser.getName();
switch (name) {
case AUTHOR:
item.mAuthor = pullParser.nextText();
break;
case TITLE:
item.mTitle = pullParser.nextText();
break;
case CATEGORY:
item.mCategory = pullParser.nextText();
break;
case COMMENTS:
item.mComments = pullParser.nextText();
break;
case DESCRIPTION:
item.mDescription = pullParser.nextText();
break;
case GUID:
item.mGuid = pullParser.nextText();
break;
case LINK:
item.mLink = pullParser.nextText();
break;
case PUBDATE:
item.mPubDate = pullParser.nextText();
break;
case SOURCE:
item.mSource = pullParser.nextText();
break;
case ENCLOSURE:
item.mEnclosure = pullParser.getAttributeValue(null, "url");
default:
break;
}
}
eventType = pullParser.next();
}
//download the optional enclosure resource and update UI
new DownloadRssAsyncTask().execute(item);
}
eventType = pullParser.next();
}
} catch (XmlPullParserException e) {
Log.i(TAG, "XmlPullparserException");
} catch (IOException e) {
Log.i(TAG, "IOException");
}
}
}
Because of the inner workings of AsyncTask.
AsyncTask internally uses a static Handler instance, basically the Android way for thread communication. With a Handler you can send messages and run code on threads; in particular, AsyncTask uses it to run its callbacks such as onPostExecute().
Now, when Handler is initialiazed, it binds on the thread that initializes it. In AsyncTask this is done during class initialization/loading at the line:
private static final InternalHandler sHandler = new InternalHandler();
Since sHandler is also final, it cannot be modified after that, and the callbacks will be always triggered on that thread.
In your case, you create an instance of RssAsyncTask in onCreate(), which is run on the UI thread. This triggers the loading of the AsyncTask class and bind AsyncTask's Handler to the UI thread. Therefore, from that point your onPostExecute()s will always be run on the UI Thread. This is despite you create some AsyncTasks in another background thread.
The Threading Rules want to ensure the class is loaded/initialized on the UI thread (see this) and want to enforce good threading practices.
Also, I recommend IntentService for simple network operations, rather than AsyncTask.

Looper in AsyncTask

I'm learning about HTML and parsing data with XML DOM. For this, I've created an App that reads the Wheater from Yahoo's wheater API.
When executing the app, shows an error in the logcat that says: java.lang.RuntimeException: Can't create handler inside thread that has not caller Looper.prepare().
I don't know what this means, or if the code is right.
This is the link to the XML file of Yahoo's wheater API:
http://weather.yahooapis.com/forecastrss?w=766273&u=c
And this is my code:
public class WeatherActivity extends Activity {
private static final String WEATHER_URL = "http://weather.yahooapis.com/forecastjson?w=";
private static final String MADRID_CODE = "766273";
private static final String LOCATION_NAME = "location";
private static final String CITY_NAME = "city";
private static final String CONDITION_NAME = "condition";
private static final String TEMPERATURE_NAME = "temperature";
private static final String FORECAST_NAME = "forecast";
private static final String DAY_NAME = "day";
private static final String HIGH_TEMPERATURE_NAME = "high_temperature";
private static final String LOW_TEMPERATURE_NAME = "low_temperature";
private static final String TODAY = "Today";
private static final String TOMORROW = "Tomorrow";
private Button mButton;
private TextView mCity;
private TextView mToday;
private TextView mTomorrow;
private class WeatherInfo {
String city;
int temperatureNow;
int lowTemperature;
int highTemperature;
int lowTemperatureTomorrow;
int highTemperatureTomorrow;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCity = (TextView) findViewById(R.id.city);
mToday = (TextView) findViewById(R.id.today);
mTomorrow = (TextView) findViewById(R.id.tomorrow);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
launch();
}
});
}
private void launch(){
try {
new WeatherAsyncTask().execute(MADRID_CODE);
} catch (IllegalArgumentException e) {
e.printStackTrace();
Toast.makeText(WeatherActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private class WeatherAsyncTask extends AsyncTask<String, Void, WeatherInfo>{
#Override
protected WeatherInfo doInBackground(String... params) {
String code = params[0];
if (TextUtils.isEmpty(code))
throw new IllegalArgumentException("Code cannot be empty");
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL(WEATHER_URL + code);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream is = connection.getInputStream();
WeatherInfo info = readWeatherInfo(is);
return info;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(WeatherActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {
if (connection != null)
connection.disconnect();
}
return null;
}
#Override
protected void onPostExecute(WeatherInfo result) {
super.onPostExecute(result);
showResult(result);
}
private WeatherInfo readWeatherInfo(InputStream is){
if (is == null)
return null;
WeatherInfo info = new WeatherInfo();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(is);
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("item");
for (int i=0; i<items.getLength(); i++) {
Node item = items.item(i);
NodeList datos = item.getChildNodes();
for (int j=0; j<datos.getLength(); j++) {
Node dato = datos.item(j);
String etiqueta = dato.getNodeName();
if (etiqueta.equals(LOCATION_NAME)) {
String texto = obtenerTexto(dato);
if (texto.equals(TEMPERATURE_NAME)) {
info.city = texto;
}
}
else if (etiqueta.equals(CONDITION_NAME)) {
String texto = obtenerTexto(dato);
if (texto.equals(CITY_NAME)) {
info.temperatureNow = Integer.parseInt(texto);
}
}
else if (etiqueta.equals(FORECAST_NAME)) {
String texto = obtenerTexto(dato);
String day = null;
int high = -111;
int low = -111;
if (texto.equals(DAY_NAME)){
day = texto;
} else if (texto.equals(HIGH_TEMPERATURE_NAME)){
high = Integer.parseInt(texto);
} else if (texto.equals(LOW_TEMPERATURE_NAME)){
low = Integer.parseInt(texto);
}
if (day.equals(TODAY)){
info.highTemperature = high;
info.lowTemperature = low;
} else if (day.equals(TOMORROW)){
info.highTemperatureTomorrow = high;
info.lowTemperatureTomorrow = low;
}
}
}
}
}
catch (Exception ex)
{
throw new RuntimeException(ex);
}
return info;
}
private String obtenerTexto(Node dato) {
StringBuilder texto = new StringBuilder();
NodeList fragmentos = dato.getChildNodes();
for (int k=0;k<fragmentos.getLength();k++) {
texto.append(fragmentos.item(k).getNodeValue());
}
return texto.toString();
}
}
private void showResult(WeatherInfo info){
mCity.setText("Temperature in " + info.city);
mToday.setText("Today: " + info.temperatureNow + " F (min: " + info.lowTemperature + " F / max: " + info.highTemperature + " F).");
mTomorrow.setText("Tomorrow: min: " + info.lowTemperatureTomorrow + " F / max: " + info.highTemperatureTomorrow + " F.");
}
}
You cannot show a Toast in the doInBackground of an ASyncTask
Try wrapping it in a runOnUIThread() like:
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(WeatherActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
You can use preimplemented method onProgressUpdate() of AsyncTask which allows you to post Toasts. To do this, you have to change your AsyncTask declaration into
private class WeatherAsyncTask extends AsyncTask<String, String, WeatherInfo>
and add method
#Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
Toast.makeText(WeatherActivity.this, values[0], Toast.LENGTH_SHORT).show();
}
and inside your AsyncTask you can catch your exception and do this
catch (IOException e)
{
e.printStackTrace();
publishProgress(e.getMessage());
}
All this is available by default in AsyncTask without the need of creating new runnables.

Categories