Titanium Not Loading all HTTPClient XML Requests - android

I have an application that has 5 windows, and when clicking on the tabs above them passes the window the URL of the RSS to load. It's maddening, and it's about to make me jump ship from Appcelerator to PhoneGap.
This is a last ditch effort before jumping ship..........please tell me I'm doing something silly. It works fine if a bit sluggish on the emulator, but on my Dell Streak 5 it's maddeningly inconsistent. Sometimes the XML renders (it always returns with a 4 readystate), somethings it just hangs with the activity indicator spinning. If I rebuild without the activity indicator many times just nothing appears in the windows. No errors to speak of that I can see.
HEre is the offending code. Please tell me I'm doing something horribly wrong to make me happy.
data = [];
//load RSS Feed
Ti.API.info('>>>> loading RSS feed '+url);
//Show Loading Animation
navActInd.show();
var xhr = Ti.Network.createHTTPClient();
xhr.timeout = 10000;
xhr.open('GET', url);
xhr.onload = function()
{
var xml1 = xhr.responseXML;
var items = xml1.documentElement.getElementsByTagName("item");
//Loop Through XML and Build Rows
for (var i = 0; i < items.length; i++) {
this_post_title = items.item(i).getElementsByTagName("title").item(0).text;
this_post_merchant = items.item(i).getElementsByTagName("category").item(0).text;
post_cid = items.item(i).getElementsByTagName("source").item(0).text;
var row = Ti.UI.createTableViewRow();
var post_title = Ti.UI.createLabel({text: this_post_title});
row.add(post_title);
row.thisSource = post_cid;
row.thisMerchant = this_post_merchant;
data.push(row);
}
navActInd.hide();
var tv = Titanium.UI.createTableView({
data: data,
top: 0,
width: 'auto',
height: 'auto'
});
//Add Table to Window
window.add(tv);
navActInd.hide();

first, you shouldn't hide the navActInd twice. you also should add an onerror function to print the status code.
without taking a look at your xml file it's hard to form an opinion.
i understand your frustration about titanium. there are a lot frustration things about it since it's quite complicated to build such a framework. i build a really comprehensive app with titanium and have had a lot of such moments but nether the less i found a solution for all problems;)

I have seen similar issues when the xml is not what I have expected, you should put a try catch block around your xmlparsing block and see if an error is thrown
you should add an onError handler to you code.
from a complexity perspective, you do not need a label to set the title of the row; these lines can be edited
var row = Ti.UI.createTableViewRow();
var post_title = Ti.UI.createLabel({text: this_post_title});
row.add(post_title);
with this line, also use rowClassName whenever possible
var row = Ti.UI.createTableViewRow({title: this_post_title, className:'#row'});
row.add(post_title);
I would also suggest you take a look at the kitchen Sink demo for a complete example. https://github.com/appcelerator/KitchenSink/tree/master/KitchenSink

Related

Focus html page or element from a webview with talckback

I have an hybrid app developed with ionic 1.x. When the app loads I am forcing the webview to take the focus from native side with the hope that after some initialization request a survey dialog appear and take the focus it self(When dialog appear I am forcing it to take focus). I am trying to make it work with talkback
The problem is that when you load the app from scratch the dialog is not focused so it is not read, after navigate through the app and come back to the original page then in works as expected, looks like as the user is in fact inside the page things works ok.
Is there any workaround or strategy to solve this particular situation?
Thanks in advance
I don't know if it helps you,
but we use it to focus on specific elements in the web view.
it works in android and ios,
but in android, before every element it read webview.
(if you found a solution for it please let me know)
function putFocus(elementForFocus) {
var $element = $(elementForFocus);
var focusInterval = 10; // ms, time between function calls
var focusTotalRepetitions = 10; // number of repetitions
$element.attr('tabindex', '0');
$element.blur();
var focusRepetitions = 0;
var interval = window.setTimeout(function () {
$element.focus();
}, focusInterval);
};

CORONA SDK: Create a back to main button stopping the already started scenes.

im developing an APP in corona SDK, it's a serie of tests, so i made every test separately and then i created a main Scene that contains this tests. To handle any click error or misunderstanding i created a "back to intro" button, the problem is, when i start a test (for example test2) i'm in the middle of the test and i use the back to intro button (that takes me to intro, there's no prob with that) the Test2 keeps going and when i try to re-take the test shows an error related to this. I've trying different ways but i can't make it work, here is the code of the button:
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
storyboard.showOverlay( "cerrar sesion", options )
end
end
local button1 = widget.newButton
{
width = 60,
height = 60,
defaultFile = "images/boton_config.png",
overFile = "images/boton_config.png",
label = "",
onEvent = handleButtonEvent
}
-- Center the button
button1.x = display.contentCenterX+550
button1.y = 35
button1.isVisible=true;
storyboard.gotoScene("test0intro")
Plz help :<
I think I may have had this problem. I would advise switching to composer rather than storyboard a lot of these issues are easier to find. I am unsure about storyboard but for composer I know that I had to remove all event listeners, remove the sceneGroup, stop the physics and then remove the scene in the scene:hide method. I'm unsure of what the equivalent of this is in storyboard. Otherwise when I went to restart to the game (as you seem to be doing), the game would crash. Hope that helps!

On Android in Cucumber/Calabash, how do I test with a webview iframe?

I can locate an iframe in an Android webview, but I need to interact with it. Various pages suggest
query("webView css:'iframe'", :stringByEvaluatingJavaScriptFromString => '...')
but that results in "No such method found: stringByEvaluatingJavaScriptFromString([String])" and it looks like an iOS function, so I'm pretty sure this is an iOS-only solution: I need Android.
I know I can pass javascript queries in by opening them in a URL, but I've only experienced getting stuff out of an Android webview with specially-code Chrome objects linked in to the application.
Any ideas for getting data out?
The iOS responses are correct, you should use javascript to get the contents of an iframe. However, you should use evaluate_javascript(query_string, javascript) to evaluate javascript in Android. e.g
evaluate_javascript("webview", "return document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('...').getBoundingClientRect();")
Use the coordinates to tap the view.
If the rect is not parsed correctly, you could do something like this:
var boundingBox = document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('main').getBoundingClientRect();
var rect = {};
rect.width = boundingBox.width;
rect.height = boundingBox.height;
rect.left = boundingBox.left;
rect.top = boundingBox.top;
return rect;
Secondly, query('webView css:#grabby') is not a valid query. query('webView css:"#grabby"') or query("webView css:'#grabby'") is. You are most likely running a (very) old version of Calabash-Android if the test-server is crashing instead of reporting it.
I wrote what amounted to an answer while I was exploring, but I'm posting it here to save someone else time (hopefully).
I can successfully run Javascript in the view:
query("webView index:0", :loadUrl => 'javascript:alert("Hello")')
so I can probably arrange to press buttons plausibly enough for testing:
query("webview index:0", :loadUrl => 'javascript: ifr = document.getElementsByTagName("iframe")[0]; idoc = ifr.contentDocument || ifr.contentWindow.document; idoc.getElementById('myCheck').click(); false')
To get data out again, I can create a DIV and set its title (for example) with some 'return' value:
query("webView index:0", :loadUrl => 'javascript:var idiv = document.createElement("div"); idiv.id="grabby"; document.getElementsByTagName("body")[0].appendChild(idiv); idiv.title="some return value"; false')
(The trailing 'false' in the javascript: command is there to prevent the result of running the javascript: command from replacing the page - that took a while to work out.)
I can look into the iframe too:
query("webView index:0", :loadUrl => 'javascript:var idiv = document.getElementById("grabby"); ifr = document.getElementsByTagName("iframe")[0]; idoc = ifr.contentDocument || ifr.contentWindow.document; idiv.title = idoc.getElementsByTagName("input")[3].id; false')
When I tried to retrieve it with
query('webView css:#grabby')
it hung for a while, then crashed with a timeout:
HTTPClient::ReceiveTimeoutError: execution expired
from /Users/tim./.rvm/gems/ruby-2.1.5/gems/httpclient-2.6.0.1/lib/httpclient/session.rb:876:in `gets'
from /Users/tim./.rvm/gems/ruby-2.1.5/gems/httpclient-2.6.0.1/lib/httpclient/session.rb:876:in `block in parse_header'
from ...
curiously, query('webView css:"*"') mostly seems to work without timeouts, and reports the 'grabby' DIV complete with the title string
query('webView css:"*"').find { |e| e['id'] == 'grabby' }
so that's useful enough. I'll have to write some horrible recursive descent thingy to fish out the iframe's contents - perhaps make the div invisible and just copy the htmlContent there.
I'm really not sure why the direct query crashes.

Why do Images in Android aren't being shown on a TableView?

I'm working on Titanium 2.1.3 and deploying for both iOS and Android.
I'm having trouble displaying images on ImageView on Android in a TableView, if I do something like clicking on the search bar and show the keyboard then the images are shown or if I scroll the TableView the images appear and disappear with no apparent reason. It seems that unless I do something that forces a layout refresh on the TableView, the images aren't rendered.
The images are being loaded the same way in both Android and iOS, which is like this:
var itemIconHolder = Titanium.UI.createView({
width : '100dp',
left : '55dp',
height : "100%"
});
var itemIcon = Titanium.UI.createImageView({
left : '0dp',
height : '100%',
});
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "thumb-" + filename);
itemIcon.image = f;
itemIconHolder.add(itemIcon);
This problem doesn't happen in iOS, on iOS devices/simulator the behaviour is normal and flawless, but on Android devices/emulator it happens.
Do I have to load the images differently in Android? Am I missing something? I hope someone could help me in this one.
Thanks in advance.
EDIT
I tried these approaches:
itemIcon.image = f.read();
and
itemIcon.image = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "thumb-" + filename);
but the images still aren't rendered until I make something that causes the TableView to refresh in some way.
I found a workaround for this particular issue on Android.
Since it takes a layout refresh for the images to be rendered properly, what I did was to animate the table, moving it 1dp in a direction and at the completition of said animation I animated it again to return it to its original position. This is the code I used:
var table_bottom = '-1dp'
var tableAnimation = Ti.UI.createAnimation({
bottom : table_bottom,
duration : 100
});
tableAnimation.addEventListener('complete', function(e){
var table_bottom = '50dp';
if (osname === 'android')
{
table_bottom = '0dp'
}
table.animate({
bottom : table_bottom,
duration : 100
});
});
I've encountered this kind of problem with both Titanium SDK 2.1.3 and Titanium 3.0.

Phonegap breaks jQuery Mobile pages when reloaded - visiting the same page again

I have been working on a project using PhoneGap and jQuery Mobile. My setup uses multiple pages inside a single html file.
I am facing a problem and I haven't found anything similar anywhere:
When I revisit a page, which means I visited it, then navigated to another page, and now returned to the first page, there is some padding between the header and the content, and also between the footer and the content of the page.
As screenshots show below:
http://i.imgur.com/neBwZYx.png
Below you can see the padding added, red background, when returned to the page above afterwards (this happens with every page)
http://i.imgur.com/u1whW9b.png
The code is very large to post here so if anyone has a suggestion please tell me how to fix this or where to look for the problem.
It should be noted that the problem exists only if the app runs on Android tablets, and not when viewed through the browser on my laptop.
Thank you
You can force correct content height with this function:
function getRealContentHeight() {
var header = $.mobile.activePage.find("div[data-role='header']:visible");
var footer = $.mobile.activePage.find("div[data-role='footer']:visible");
var content = $.mobile.activePage.find("div[data-role='content']:visible:visible");
var viewport_height = $(window).height();
var content_height = viewport_height - header.outerHeight() - footer.outerHeight();
if((content.outerHeight() - header.outerHeight() - footer.outerHeight()) <= viewport_height) {
content_height -= (content.outerHeight() - content.height());
}
return content_height;
}
It must be activated during the pageshow event because only at that point page height is correct:
$(document).on('pageshow', '#index', function(){
$.mobile.activePage.find('.ui-content').height(getRealContentHeight());
});
Working example: http://jsfiddle.net/Gajotres/nVs9J/
If you want to find out more about this function read my other article: https://stackoverflow.com/a/14550417/1848600

Categories

Resources