tag:blogger.com,1999:blog-8659233597353832412013-05-07T22:51:40.843-06:00Ccna final exam - java, php, javascript, ios, cshap all in oneCcna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.Usernoreply@blogger.comBlogger3839125tag:blogger.com,1999:blog-865923359735383241.post-66015217464530090342012-06-11T22:19:00.003-06:002012-06-11T22:19:52.534-06:00Increasing the performance and usability of Facebook"s FQL<p> <br /> I try to get some insights from the pages I am administrator on Facebook. <br /></p> <a name='more'></a><br/><p> <br /> <br /> <br /> What my code does, it gets the IDs of the pages I want to work with through mySQL. I did not include that part though. <br /></p> <br /><p> <br /> After this, I get the page_id, name and fan_count of each of those facebook IDs and are saved in <code>fancounts[]</code> . <br /></p> <br /><p> <br /> Using the IDs ( <code>pages[]</code> ) I get two messages max from each page. There may be no messages, there may be 1 or 2 messages max. Possibly I will increase it later. <code>messages[]</code> holds the messages of each page. <br /></p> <br /><p> <br /> I have two problems with it. <br /></p> <br /><ol> <br /> <li>It has a very slow performance <br /> </li> <br /> <li>I can't find a way to echo the data like this: <br /> </li> <br /></ol> <br /><p> <br /> <em>ID - Name of the page - Fan Count</em><br /> <br /> Here goes the first message<br /> <br /> Here goes the second one <br /></p> <br /><p> <br /> <em>//here is a break</em><br /> <br /></p> <br /><p> <br /> <em>ID - Name of the page 2 - Fan Count</em><br /> <br /> Here goes the first message of page 2<br /> <br /> Here goes the second one of page 2 <br /></p> <br /><p> <br /> My questions are, how can the code be modified to increase performance and show the data as above? I read about fql.multiquery. Can it be used here? <br /></p> <br /><p> <br /> Please provide me with code examples. Thank you <br /></p> <br /><pre> <br /><code> <br />$pages = array(); // I get the IDs I want to work with <br /> $pagesIds = implode(',', $pages); <br /> <br /> // fancounts[] holds the page_id, name and fan_count of the Ids I work with <br /> <br /> $fancounts = array(); <br /> $pagesFanCounts = $facebook-&gt;api("/fql", array( <br /> "q" =&gt; "SELECT page_id, name, fan_count FROM page WHERE page_id IN ({$pagesIds})" <br /> )); <br /> <br /> foreach ($pagesFanCounts['data'] as $page){ <br /> $fancounts[] = $page['page_id']."-".$page['name']."-".$page['fan_count']; <br /> } <br /> <br /> <br /> //messages[] holds from 0 to 2 messages from each of the above pages <br /> <br /> $messages = array(); <br /> foreach( $pages as $id) { <br /> $getMessages = $facebook-&gt;api("/fql", array( <br /> "q" =&gt; "SELECT message FROM stream WHERE source_id = '$id' LIMIT 2" <br /> )); <br /> $messages[] = $getMessages['data']; <br /> } <br /> <br /> // this is how I print them now but it does not give me the best output. <br /> //( thanks goes to Mark for providing me this code ) <br /> <br /> $count = min(count($fancounts),count($messages)); <br /> <br /> for($i=0; $i&lt;$count; ++$i) { <br /> echo $fancounts[$i],'&lt;br&gt;'; <br /> foreach($messages[$i] as $msg) { <br /> echo $msg['message'],'&lt;br&gt;'; <br /> } <br />} <br /></code> <br /></pre> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com3tag:blogger.com,1999:blog-865923359735383241.post-65445230433125236422012-06-11T22:19:00.001-06:002012-06-11T22:19:19.816-06:00SVN checkout the contents of a folder, not the folder itself<p> <br /> I'm fairly new to linux and svn. I'm trying to checkout the trunk folder of a project into my public_html directory using this command (while in public_html): <br /></p> <a name='more'></a><br/><pre> <br /><code> <br />svn checkout file:///home/landonwinters/svn/waterproject/trunk <br /></code> <br /></pre> <br /><p> <br /> The waterproject directory contains the files from untarring a base install of drupal. <br /></p> <br /><p> <br /> It checks out fine, except all the files are in <code>public_html/trunk</code> instead of just being in <code>public_html</code> . <br /></p> <br /><p> <br /> I don't know the command to move all the contents of trunk up to public_html and rm trunk, but I think I could figure that out relatively easily. I just want to know if I can just check out the contents of a folder, without the folder itself. <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com4tag:blogger.com,1999:blog-865923359735383241.post-41870239561157619152012-06-11T22:18:00.005-06:002012-06-11T22:18:56.716-06:00What is the purpose of a these #define within an enum?<p> <br /> I found this code in the linux headers, /usr/include/dirent.h: <br /></p> <br /><pre> <br /><code> <br />enum <br /> { <br /> DT_UNKNOWN = 0, <br /># define DT_UNKNOWN DT_UNKNOWN <br /> DT_FIFO = 1, <br /># define DT_FIFO DT_FIFO <br /> DT_CHR = 2, <br /># define DT_CHR DT_CHR <br /> DT_DIR = 4, <br /># define DT_DIR DT_DIR <br /> DT_BLK = 6, <br /># define DT_BLK DT_BLK <br /> DT_REG = 8, <br /># define DT_REG DT_REG <br /> DT_LNK = 10, <br /># define DT_LNK DT_LNK <br /> DT_SOCK = 12, <br /># define DT_SOCK DT_SOCK <br /> DT_WHT = 14 <br /># define DT_WHT DT_WHT <br /> }; <br /></code> <br /></pre> <a name='more'></a><br/><p> <br /> What is this construct for? - why define something with an identical string, which will then compile to the int value? <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com5tag:blogger.com,1999:blog-865923359735383241.post-3926192798917437322012-06-11T22:18:00.003-06:002012-06-11T22:18:55.593-06:00bash: defining a variable with or without export<p> <br /> What is export for? What is the difference between: <br /></p> <br /><pre> <br /><code> <br />export name=value <br /></code> <br /></pre> <a name='more'></a><br/><p> <br /> and <br /></p> <br /><pre> <br /><code> <br />name=value <br /></code> <br /></pre> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com0tag:blogger.com,1999:blog-865923359735383241.post-78725651853363893342012-06-11T22:18:00.001-06:002012-06-11T22:18:07.402-06:00generate a core dump in linux<p> <br /> I have a process in linux that's getting a segmentation fault. How can I tell it to generate a core dump when it fails? <br /></p> <a name='more'></a><br/> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com5tag:blogger.com,1999:blog-865923359735383241.post-53810219985059454582012-06-11T22:17:00.001-06:002012-06-11T22:17:21.246-06:00What is the simplest way to SSH using Python?<p> <br /> What is the absolute simplest way to SSH to a remote server from a local Python (3.0) script, supply a login/password, execute a command and print the output to the Python console? I would rather not use any large external library and not install anything on the remote server. <br /></p> <a name='more'></a><br/> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com6tag:blogger.com,1999:blog-865923359735383241.post-32638021376346712682012-06-11T22:16:00.005-06:002012-06-11T22:16:49.490-06:00jQuery Ajax calls and the Html.AntiForgeryToken()<p> <br /> I have implemented in my app the mitigation to CSRF attacks following the informations that I have read on some blog post around the internet. In particular these post have been the driver of my implementation <br /></p> <a name='more'></a><br/><ul> <br /> <li> <br /> <a href="http://blogs.msdn.com/b/aspnetue/archive/2010/09/17/second_2d00_post.aspx" rel="nofollow">Best Practices for ASP.NET MVC</a> from the ASP.NET and Web Tools Developer Content Team <br /> </li> <br /> <li> <br /> <a href="http://haacked.com/archive/2009/04/02/anatomy-of-csrf-attack.aspx" rel="nofollow">Anatomy of a Cross-site Request Forgery Attack</a> from Phil Haack blog <br /> </li> <br /> <li> <br /> <a href="http://davidhayden.com/blog/dave/archive/2009/04/29/AntiForgeryTokenInMVCFramework.aspx" rel="nofollow">AntiForgeryToken in the ASP.NET MVC Framework - Html.AntiForgeryToken and ValidateAntiForgeryToken Attribute</a> from David Hayden blog <br /> </li> <br /></ul> <br /><p> <br /> Basically those articles and recommendations says that to prevent the CSRF attack anybody should implement the following code: <br /></p> <br /><p> <br /> 1) Add the <code>[ValidateAntiForgeryToken]</code> on every action that accept the POST Http verb <br /></p> <br /><pre> <br /><code> <br />[HttpPost] <br />[ValidateAntiForgeryToken] <br />public ActionResult SomeAction( SomeModel model ) { <br />} <br /></code> <br /></pre> <br /><p> <br /> 2) Add the <code>&lt;%= Html.AntiForgeryToken() %&gt;</code> helper inside forms that submits data to the server <br /></p> <br /><pre> <br /><code> <br />&lt;div style="text-align:right; padding: 8px;"&gt; <br /> &lt;%= Html.AntiForgeryToken() %&gt; <br /> &lt;input type="submit" id="btnSave" value="Save" /&gt; <br />&lt;/div&gt; <br /></code> <br /></pre> <br /><p> <br /> Anyway in some parts of my app I am doing Ajax POSTs with jQuery to the server without having any form at all. This happens for example where I am letting the user to click on an image to do a specific action. <br /></p> <br /><p> <br /> Suppose I have a table with a list of activities. I have an image on a column of the table that says "Mark activity as completed" and when the user click on that activity I am doing the Ajax POST as in the following sample: <br /></p> <br /><pre> <br /><code> <br />$("a.markAsDone").click(function (event) { <br /> event.preventDefault(); <br /> $.ajax({ <br /> type: "post", <br /> dataType: "html", <br /> url: $(this).attr("rel"), <br /> data: {}, <br /> success: function (response) { <br /> // .... <br /> } <br /> }); <br />}); <br /></code> <br /></pre> <br /><p> <br /> How can I use the <code>&lt;%= Html.AntiForgeryToken() %&gt;</code> in these cases? Should I include the helper call inside the data parameter of the Ajax call? <br /></p> <br /><p> <br /> Sorry for the long post and thanks very much for helping out <br /></p> <br /><p> <br /> <strong>EDIT</strong> : <br /></p> <br /><p> <br /> As per <a href="http://stackoverflow.com/questions/4074199/jquery-ajax-calls-and-the-html-antiforgerytoken/4074289#4074289">jayrdub</a> answer I have used in the following way <br /></p> <br /><pre> <br /><code> <br />$("a.markAsDone").click(function (event) { <br /> event.preventDefault(); <br /> $.ajax({ <br /> type: "post", <br /> dataType: "html", <br /> url: $(this).attr("rel"), <br /> data: { <br /> AddAntiForgeryToken({}), <br /> id: parseInt($(this).attr("title")) <br /> }, <br /> success: function (response) { <br /> // .... <br /> } <br /> }); <br />}); <br /></code> <br /></pre> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com6tag:blogger.com,1999:blog-865923359735383241.post-84837850740682756432012-06-11T22:16:00.003-06:002012-06-11T22:16:27.600-06:00How can I convince IE to simply display application/json rather than offer to download it?<p> <br /> While debugging jQuery apps that use AJAX, I often have the need to see the json that is being returned by the service to the browser. So I'll drop the URL for the JSON data into the address bar. <br /></p> <a name='more'></a><br/><p> <br /> This is nice with ASPNET because in the event of a coding error, I Can see the ASPNET diagostic in the browser: <br /></p> <br /><p> <br /> <img src="http://i41.tinypic.com/161z6mg.jpg" alt="alt text" /> <br /></p> <br /><p> <br /> But when the server-side code works correctly and actually returns JSON, IE prompts me to download it, so I can't see the response. <br /></p> <br /><p> <br /> <img src="http://i40.tinypic.com/wt6a1l.jpg" alt="alt text" /> <br /></p> <br /><p> <br /> <strong>Can I get IE to NOT do that, in other words, to just display it as if it were plain text?</strong> <br /></p> <br /><p> <br /> I know I could do this if I set the Content-Type header to be <code>text/plain</code> . <br /></p> <br /><p> <br /> But this is specifically an the context of an ASPNET MVC app, which sets the response automagically when I use JsonResult on one of my action methods. Also I kinda want to keep the appropriate content-type, and not change it just to support debugging efforts. <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com5tag:blogger.com,1999:blog-865923359735383241.post-65771984747870087702012-06-11T22:16:00.001-06:002012-06-11T22:16:08.443-06:00How can you make a vote-up-down button like in Stackoverflow?<p> <br /> <strong>Problems</strong> <br /></p> <br /><ol> <br /> <li>how to make an Ajax buttons (upward and downward arrows) such that the number can increase or decrease <br /> </li> <br /> <li>how to save the action af an user to an variable NumberOfVotesOfQuestionID <br /> </li> <br /></ol> <a name='more'></a><br/><p> <br /> I am not sure whether I should use database or not for the variable. However, I know that there is an easier way too to save the number of votes. <br /></p> <br /><p> <br /> <strong>How can you solve those problems?</strong> <br /></p> <br /><p> <br /> [edit] <br /></p> <br /><p> <br /> The server-side programming language is Python. <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com0tag:blogger.com,1999:blog-865923359735383241.post-69237955199795209102012-06-11T22:15:00.003-06:002012-06-11T22:15:42.649-06:00Chrome: Disable same origin policy<p> <br /> Is there a way to disable the same origin policy on Google's Chrome browser? This is strictly for development, not production, use. <br /></p> <a name='more'></a><br/> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com5tag:blogger.com,1999:blog-865923359735383241.post-41748519135770063262012-06-11T22:15:00.001-06:002012-06-11T22:15:21.480-06:00Do you know of a bleeding-edge HTML5 leveraging, legacy-ignoring JavaScript framework?<p> <br /> What's the best framework (sort of jquery, extjs, etc like) to use if I'd like to intensively use all the freshest technologies of the HTML5 stack provided by modern browsers (Firefox 7, Safari 5, Chrome 14) and have absolutely no need to support any legacy browsers (incl. no need in IE support at all and no need in Firefox or Chrome prior to the latest stable releases)? I'd like to get all the newest available goodness without having (even abstracted by a library layer) a line of code meant just fore legacy compatibility or keeping any legacy-induced things in mind. <br /></p> <a name='more'></a><br/><p> <br /> To soften the filter, taking very humble hope of such an ideally fresh framework to exist, the least (the maximum level of legacy support) I'd like to agree is not supporting IE versions older than IE8, or better just not supporting IE at all. <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com5tag:blogger.com,1999:blog-865923359735383241.post-32678837210293687282012-06-11T22:14:00.003-06:002012-06-11T22:14:57.883-06:00How to sort a NSArray alphabetically?<p> <br /> How can I sort an array filled with [UIFont familyNames] into alphabetical order? <br /></p> <a name='more'></a><br/> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com1tag:blogger.com,1999:blog-865923359735383241.post-11687329874705796062012-06-11T22:14:00.001-06:002012-06-11T22:14:44.657-06:00Convert UTF-8 encoded NSData to NSString<p> <br /> I have UTF-8 encoded nsdata from windows server. I want to convert it to nsstring for iphone. Since data contains characters(like degree symbol) which have different values on both platforms, how do I convert data to string. <br /></p> <a name='more'></a><br/> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com2tag:blogger.com,1999:blog-865923359735383241.post-7423507904775800272012-06-11T22:13:00.005-06:002012-06-11T22:13:41.728-06:00Are you doing iPhone development? How do you learn?<p> <br /> I am looking for more iPhone developers who are actively posting to their blog and/or Twitter. I have been learning a lot from books but the online resources beyond Apple's developer site have been hard to find. <br /></p> <a name='more'></a><br/><p> <br /> Please let me know if you are doing iPhone development. You can see my comment on Twitter here... <br /></p> <br /><p> <br /> <a href="http://twitter.com/offwhitemke/statuses/1090551535" rel="nofollow">http://twitter.com/offwhitemke/statuses/1090551535</a> <br /></p> <br /><p> <br /> My secondary question is, where are you going to learn more about iPhone development? Have you found any good sources of video training material? Apple is charging $500 for their iPhone videos but there has to be free content out there. I found content on YouTube but it is so compressed that the text is not readable. <br /></p> <br /><p> <br /> I have found that StackOverflow.com has been a really good place to get answers. I also find that iPhoneKicks.com (links site) has been helpful in finding content. <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com31tag:blogger.com,1999:blog-865923359735383241.post-4105553391314956182012-06-11T22:13:00.003-06:002012-06-11T22:13:24.407-06:00How do I draw a shadow under a UIView?<p> <br /> I'm trying to draw a shadow under the bottom edge of a UIView in Cocoa Touch. I understand that I should use CGContextSetShadow() to draw the shadow, but the Quartz 2D programming guide is a little vague: <br /></p> <a name='more'></a><br/><ol> <br /> <li>Save the graphics state. <br /> </li> <br /> <li>Call the function CGContextSetShadow, passing the appropriate values. <br /> </li> <br /> <li>Perform all the drawing to which you want to apply shadows. <br /> </li> <br /> <li>Restore the graphics state <br /> </li> <br /></ol> <br /><p> <br /> I've tried the following in a UIView subclass: <br /></p> <br /><pre> <br /><code> <br />- (void)drawRect:(CGRect)rect { <br /> CGContextRef currentContext = UIGraphicsGetCurrentContext(); <br /> CGContextSaveGState(currentContext); <br /> CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5); <br /> CGContextRestoreGState(currentContext); <br /> [super drawRect: rect]; <br />} <br /></code> <br /></pre> <br /><p> <br /> ..but this doesn't work for me and I'm a bit stuck about (a) where to go next and (b) if there's anything I need to do to my UIView to make this work? <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com4tag:blogger.com,1999:blog-865923359735383241.post-46888685328868433242012-06-11T22:13:00.001-06:002012-06-11T22:13:03.906-06:00Programmatically add custom event in the iPhone Calendar<p> <br /> Is there any way to add iCal event to the iPhone Calendar from the custom App? <br /></p> <a name='more'></a><br/> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com5tag:blogger.com,1999:blog-865923359735383241.post-56627715836795179392012-06-11T22:12:00.001-06:002012-06-11T22:12:16.918-06:00iPhone or Android?<p> <br /> According to a recent <a href="http://www.odesk.com/blog/2008/12/android-getting-slaughtered-by-iphone/">article</a> iPhone has gained a better appeal than Android among programmers. I'm quite a newbie to Java and a complete profane to ObjectiveC, in your opinion, in which one of the two could I have a try? which platform is good for me <br /></p> <a name='more'></a><br/><p> <br /> <br /> <br /> because i have knowledge about java but not about objective c. <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com25tag:blogger.com,1999:blog-865923359735383241.post-35751620418240174242012-06-11T22:11:00.005-06:002012-06-11T22:11:57.526-06:00How to implement Android Pull-to-Refresh<p> <br /> In Android applications such as Twitter (official app), when you encounter a ListView, you can pull it down (and it will bounce back when released) to refresh the content. <br /></p> <a name='more'></a><br/><p> <br /> I wonder what is the best way, in your opinion, to implement that? <br /></p> <br /><p> <br /> Some possibilities I could think of: <br /></p> <br /><ol> <br /> <li>An item on top of the ListView - however I don't think scrolling back to item position 1 (0-based) with animation on the ListView is an easy task. <br /> </li> <br /> <li>Another view outside the ListView - but I need to take care of moving the ListView position down when it is pulled, and I'm not sure if we can detect if the drag-touches to the ListView still really scroll the items on the ListView. <br /> </li> <br /></ol> <br /><p> <br /> Any recommendations? <br /></p> <br /><p> <br /> P.S. I wonder when the official Twitter app source code is released. It has been mentioned that it will be released, but 6 months has passed and we haven't heard about it since then. <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com7tag:blogger.com,1999:blog-865923359735383241.post-65675139763487466152012-06-11T22:11:00.003-06:002012-06-11T22:11:42.507-06:00How do I make an http request using cookies on Android?<p> <br /> I'd like to make an http request to a remote server while properly handling cookies (eg. storing cookies sent by the server, and sending those cookies when I make subsequent requests). It'd be nice to preserve any and all cookies, but really the only one I care about is the session cookie. <br /></p> <a name='more'></a><br/><p> <br /> With java.net, it appears that the preferred way to do this is using java.net.CookieHandler (abstract base class) and java.net.CookieManager (concrete implementation). Android has java.net.CookieHandler, but it does not seem to have java.net.CookieManager. <br /></p> <br /><p> <br /> I could code it all by hand by inspecting http headers, but it seems like there must be an easier way. <br /></p> <br /><p> <br /> What is the proper way to make http requests on Android while preserving cookies? <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com3tag:blogger.com,1999:blog-865923359735383241.post-27549008056607515182012-06-11T22:11:00.001-06:002012-06-11T22:11:19.885-06:00Restful API service<p> <br /> I'm looking to make a service which I can use to make calls to a web based rest api. I've spent a couple of days looking through stackoverflow.com, reading books and looking at articles whilst playing about with some code and I can't get anything which I'm happy with. <br /></p> <a name='more'></a><br/><p> <br /> Basically I want to start a service on app init then I want to be able to ask that service to request a url and return the results. In the meantime I want to be able to display a progress window or something similar. <br /></p> <br /><p> <br /> I've created a service currently which uses IDL, I've read somewhere that you only really need this for cross app communication, so think these needs stripping out but unsure how to do callbacks without it. Also when I hit the <code>post(Config.getURL("login"), values)</code> the app seems to pause for a while (seems weird - thought the idea behind a service was that it runs on a different thread!) <br /></p> <br /><p> <br /> Currently I have a service with post and get http methods inside, a couple of AIDL files (for two way communication), a ServiceManager which deals with starting, stopping, binding etc to the service and I'm dynamically creating a Handler with specific code for the callbacks as needed. <br /></p> <br /><p> <br /> I don't want anyone to give me a complete code base to work on, but some pointers would be greatly appreciated; even if it's to say I'm doing it completely wrong. I'm pretty new to Android and Java dev so if there are any blindingly obvious mistakes here - please don't think I'm a rubbish developer, I'm just wet behind the ears and would appreciate being told where I'm going wrong. <br /></p> <br /><p> <br /> Anyway, code in (mostly) full (really didn't want to put this much code here, but I don't know where I'm going wrong - apologies in advance): <br /></p> <br /><pre> <br /><code> <br />public class RestfulAPIService extends Service { <br /> <br />final RemoteCallbackList&lt;IRemoteServiceCallback&gt; mCallbacks = new RemoteCallbackList&lt;IRemoteServiceCallback&gt;(); <br /> <br />public void onStart(Intent intent, int startId) { <br /> super.onStart(intent, startId); <br />} <br />public IBinder onBind(Intent intent) { <br /> return binder; <br />} <br />public void onCreate() { <br /> super.onCreate(); <br />} <br />public void onDestroy() { <br /> super.onDestroy(); <br /> mCallbacks.kill(); <br />} <br />private final IRestfulService.Stub binder = new IRestfulService.Stub() { <br /> public void doLogin(String username, String password) { <br /> <br /> Message msg = new Message(); <br /> Bundle data = new Bundle(); <br /> HashMap&lt;String, String&gt; values = new HashMap&lt;String, String&gt;(); <br /> values.put("username", username); <br /> values.put("password", password); <br /> String result = post(Config.getURL("login"), values); <br /> data.putString("response", result); <br /> msg.setData(data); <br /> msg.what = Config.ACTION_LOGIN; <br /> mHandler.sendMessage(msg); <br /> } <br /> <br /> public void registerCallback(IRemoteServiceCallback cb) { <br /> if (cb != null) <br /> mCallbacks.register(cb); <br /> } <br />}; <br /> <br />private final Handler mHandler = new Handler() { <br /> public void handleMessage(Message msg) { <br /> <br /> // Broadcast to all clients the new value. <br /> final int N = mCallbacks.beginBroadcast(); <br /> for (int i = 0; i &lt; N; i++) { <br /> try { <br /> switch (msg.what) { <br /> case Config.ACTION_LOGIN: <br /> mCallbacks.getBroadcastItem(i).userLogIn( msg.getData().getString("response")); <br /> break; <br /> default: <br /> super.handleMessage(msg); <br /> return; <br /> <br /> } <br /> } catch (RemoteException e) { <br /> } <br /> } <br /> mCallbacks.finishBroadcast(); <br /> } <br /> public String post(String url, HashMap&lt;String, String&gt; namePairs) {...} <br /> public String get(String url) {...} <br />}; <br /></code> <br /></pre> <br /><p> <br /> A couple of AIDL files: <br /></p> <br /><pre> <br /><code> <br />package com.something.android <br /> <br />oneway interface IRemoteServiceCallback { <br /> void userLogIn(String result); <br />} <br /></code> <br /></pre> <br /><p> <br /> and <br /></p> <br /><pre> <br /><code> <br />package com.something.android <br />import com.something.android.IRemoteServiceCallback; <br /> <br />interface IRestfulService { <br /> void doLogin(in String username, in String password); <br /> void registerCallback(IRemoteServiceCallback cb); <br />} <br /></code> <br /></pre> <br /><p> <br /> and the service manager: <br /></p> <br /><pre> <br /><code> <br />public class ServiceManager { <br /> <br /> final RemoteCallbackList&lt;IRemoteServiceCallback&gt; mCallbacks = new RemoteCallbackList&lt;IRemoteServiceCallback&gt;(); <br /> public IRestfulService restfulService; <br /> private RestfulServiceConnection conn; <br /> private boolean started = false; <br /> private Context context; <br /> <br /> public ServiceManager(Context context) { <br /> this.context = context; <br /> } <br /> <br /> public void startService() { <br /> if (started) { <br /> Toast.makeText(context, "Service already started", Toast.LENGTH_SHORT).show(); <br /> } else { <br /> Intent i = new Intent(); <br /> i.setClassName("com.something.android", "com.something.android.RestfulAPIService"); <br /> context.startService(i); <br /> started = true; <br /> } <br /> } <br /> <br /> public void stopService() { <br /> if (!started) { <br /> Toast.makeText(context, "Service not yet started", Toast.LENGTH_SHORT).show(); <br /> } else { <br /> Intent i = new Intent(); <br /> i.setClassName("com.something.android", "com.something.android.RestfulAPIService"); <br /> context.stopService(i); <br /> started = false; <br /> } <br /> } <br /> <br /> public void bindService() { <br /> if (conn == null) { <br /> conn = new RestfulServiceConnection(); <br /> Intent i = new Intent(); <br /> i.setClassName("com.something.android", "com.something.android.RestfulAPIService"); <br /> context.bindService(i, conn, Context.BIND_AUTO_CREATE); <br /> } else { <br /> Toast.makeText(context, "Cannot bind - service already bound", Toast.LENGTH_SHORT).show(); <br /> } <br /> } <br /> <br /> protected void destroy() { <br /> releaseService(); <br /> } <br /> <br /> private void releaseService() { <br /> if (conn != null) { <br /> context.unbindService(conn); <br /> conn = null; <br /> Log.d(LOG_TAG, "unbindService()"); <br /> } else { <br /> Toast.makeText(context, "Cannot unbind - service not bound", Toast.LENGTH_SHORT).show(); <br /> } <br /> } <br /> <br /> class RestfulServiceConnection implements ServiceConnection { <br /> public void onServiceConnected(ComponentName className, IBinder boundService) { <br /> restfulService = IRestfulService.Stub.asInterface((IBinder) boundService); <br /> try { <br /> restfulService.registerCallback(mCallback); <br /> } catch (RemoteException e) {} <br /> } <br /> <br /> public void onServiceDisconnected(ComponentName className) { <br /> restfulService = null; <br /> } <br /> }; <br /> <br /> private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() { <br /> public void userLogIn(String result) throws RemoteException { <br /> mHandler.sendMessage(mHandler.obtainMessage(Config.ACTION_LOGIN, result)); <br /> <br /> } <br /> }; <br /> <br /> private Handler mHandler; <br /> <br /> public void setHandler(Handler handler) { <br /> mHandler = handler; <br /> } <br />} <br /></code> <br /></pre> <br /><p> <br /> Service init and bind: <br /></p> <br /><pre> <br /><code> <br />// this I'm calling on app onCreate <br />servicemanager = new ServiceManager(this); <br />servicemanager.startService(); <br />servicemanager.bindService(); <br />application = (ApplicationState)this.getApplication(); <br />application.setServiceManager(servicemanager); <br /></code> <br /></pre> <br /><p> <br /> service function call: <br /></p> <br /><pre> <br /><code> <br />// this lot i'm calling as required - in this example for login <br />progressDialog = new ProgressDialog(Login.this); <br />progressDialog.setMessage("Logging you in..."); <br />progressDialog.show(); <br /> <br />application = (ApplicationState) getApplication(); <br />servicemanager = application.getServiceManager(); <br />servicemanager.setHandler(mHandler); <br /> <br />try { <br /> servicemanager.restfulService.doLogin(args[0], args[1]); <br />} catch (RemoteException e) { <br /> e.printStackTrace(); <br />} <br /> <br />...later in the same file... <br /> <br />Handler mHandler = new Handler() { <br /> public void handleMessage(Message msg) { <br /> <br /> switch (msg.what) { <br /> case Config.ACTION_LOGIN: <br /> <br /> if (progressDialog.isShowing()) { <br /> progressDialog.dismiss(); <br /> } <br /> <br /> try { <br /> ...process login results... <br /> } <br /> } catch (JSONException e) { <br /> Log.e("JSON", "There was an error parsing the JSON", e); <br /> } <br /> break; <br /> default: <br /> super.handleMessage(msg); <br /> } <br /> <br /> } <br /> <br />}; <br /></code> <br /></pre> <br /><p> <br /> Any and all help is greatly appreciated and I'll even buy you a coffee or a beer if you fancy :D <br /></p> <br /><p> <br /> Martyn <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com7tag:blogger.com,1999:blog-865923359735383241.post-33910283540260273442012-06-11T22:10:00.005-06:002012-06-11T22:10:52.177-06:00Difference between a View"s Padding and Margin<p> <br /> What is the difference between a View's Margin and Padding? <br /></p> <a name='more'></a><br/> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com4tag:blogger.com,1999:blog-865923359735383241.post-20811084136435550772012-06-11T22:10:00.003-06:002012-06-11T22:10:26.855-06:00Getting the ID of the element that fired an event using JQuery<p> <br /> Is there any way to get the ID of the element that fires an event? <br /></p> <a name='more'></a><br/><p> <br /> I'm thinking something like: <br /></p> <br /><pre> <br /><code> <br />&lt;html&gt; <br /> &lt;head&gt; <br /> &lt;script type="text/javascript" src="starterkit/jquery.js"&gt;&lt;/script&gt; <br /> &lt;script type="text/javascript"&gt; <br /> $(document).ready(function(){ <br /> $("a").click( <br /> function(){ <br /> var test = caller.id; <br /> alert(test.val()); <br /> } <br /> ); <br /> }); <br /> <br /> &lt;/script&gt; <br />&lt;/head&gt; <br /> &lt;body&gt; <br /> &lt;form class="item" id="aaa"&gt;&lt;input class="title"&gt;&lt;/input&gt;&lt;/form&gt; <br /> &lt;form class="item" id="bbb"&gt;&lt;input class="title"&gt;&lt;/input&gt;&lt;/form&gt; <br /> &lt;/body&gt; <br />&lt;/html&gt; <br /></code> <br /></pre> <br /><p> <br /> Except of course that the var <code>test</code> should contain the id <code>"aaa"</code> , if the event is fired from the first form, and <code>"bbb"</code> , if the event is fired from the second form. <br /></p> <br /><p> <br /> Can anyone help with this? <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com5tag:blogger.com,1999:blog-865923359735383241.post-53136565806177110242012-06-11T22:10:00.001-06:002012-06-11T22:10:07.701-06:00Which keycode for escape key with jQuery<p> <br /> I have two functions. When enter is pressed the functions runs correctly but when escape is pressed it doesn't. What's the correct number for the escape key? <br /></p> <a name='more'></a><br/><pre> <br /><code> <br />$(document).keypress(function(e) { <br /> if (e.which == 13) { $('.save').click(); } // enter (works as expected) <br /> if (e.which == 27) { $('.cancel').click(); } // esc (does not work) <br />}); <br /></code> <br /></pre> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com9tag:blogger.com,1999:blog-865923359735383241.post-59376364694165624242012-06-11T22:09:00.003-06:002012-06-11T22:09:48.575-06:00jQuery lose focus event<p> <br /> I'm trying to show up a container if a input field gets the focus and - that's the actual problem - hide the container if focus is lost. Is there an opposite event for jQuery's focus? <br /></p> <a name='more'></a><br/><p> <br /> Some example code: <br /></p> <br /><pre> <br /><code> <br />&lt;input type="text" value="" name="filter" id="filter"/&gt; <br /> <br />&lt;div id="options"&gt;some cool options&lt;/div&gt; <br /> <br />&lt;script type="text/javascript"&gt; <br /> $('#options').hide(); <br /> <br /> $('#filter').focus(function() { <br /> $('#options').appear(); <br /> }); <br />&lt;/script&gt; <br /></code> <br /></pre> <br /><p> <br /> And what I'd like to do is something like this: <br /></p> <br /><pre> <br /><code> <br />$('#filter').focus_lost(function() { <br /> $('#options').hide(); <br />}); <br /></code> <br /></pre> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com3tag:blogger.com,1999:blog-865923359735383241.post-17658257102620605592012-06-11T22:09:00.001-06:002012-06-11T22:09:17.029-06:00Online Tool to Unminify / Decompress JavaScript<p> <br /> Are there any scripts and/or online tools that can reverse the effects of minification similar to how Tidy can clean up horrific HTML? <br /></p> <a name='more'></a><br/><p> <br /> I'm specifically looking to unminify a minified JavaScript file, so variable renaming (like with compression or packing) should theoretically not be an issue. <br /></p> <br/><a href='http://tips4all.net' title='This is a collaboratively edited question and answer site for professional and enthusiast programmers. It is 100% free, no registration required.'>Source: <strong><em>Tips4all</em></strong></a>Usernoreply@blogger.com11