Java tutorial
package com.ppshein.sevendaynews; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; public class common { public static final String PREFS_NAME = "GetIDPref"; public static final String PREF_NEWSCATEGORY = "PrefNewsCategory"; public static SharedPreferences getPrefs(Context context) { return context.getSharedPreferences(PREFS_NAME, 0); } public static void setMyStringPref(Context context, String value) { getPrefs(context).edit().putString(PREF_NEWSCATEGORY, value).commit(); } public static String getInputStreamFromUrl(String url) { String content = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet(url)); HttpEntity resEntity = response.getEntity(); content = EntityUtils.toString(resEntity); content = content.trim(); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } return content; } public static Boolean isNetAvailable(Context con) { try { ConnectivityManager connectivityManager = (ConnectivityManager) con .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (wifiInfo.isConnected() || mobileInfo.isConnected()) { return true; } } catch (Exception e) { e.printStackTrace(); } return false; } public static Intent reDirect(String tabName, Context ctx) { Intent intent = null; if (tabName.equals("Home")) { common.setMyStringPref(ctx, "NULL"); intent = new Intent(); intent.setClass(ctx, MainActivity.class); return intent; } else if (tabName.equals("Business")) { common.setMyStringPref(ctx, tabName); intent = new Intent(); intent.setClass(ctx, MainActivity.class); return intent; } else if (tabName.equals("Entertainment")) { common.setMyStringPref(ctx, tabName); intent = new Intent(); intent.setClass(ctx, MainActivity.class); return intent; } else if (tabName.equals("Health")) { common.setMyStringPref(ctx, tabName); intent = new Intent(); intent.setClass(ctx, MainActivity.class); return intent; } else if (tabName.equals("Science")) { common.setMyStringPref(ctx, tabName); intent = new Intent(); intent.setClass(ctx, MainActivity.class); return intent; } else if (tabName.equals("Sports")) { common.setMyStringPref(ctx, tabName); intent = new Intent(); intent.setClass(ctx, MainActivity.class); return intent; } else if (tabName.equals("World")) { common.setMyStringPref(ctx, tabName); intent = new Intent(); intent.setClass(ctx, MainActivity.class); return intent; } else if (tabName.equals("Sync")) { intent = new Intent(); intent.setClass(ctx, Sync.class); return intent; } else if (tabName.equals("About")) { intent = new Intent(); intent.setClass(ctx, About.class); return intent; } return intent; } public static JSONObject getJSONfromURL(String url, String param1) { InputStream is = null; String result = ""; JSONObject jArray = null; //http post try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); List<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("newsid", param1)); httppost.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } //convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); result = result.replace( "<META NAME=\"ColdFusionMXEdition\" CONTENT=\"ColdFusion DevNet Edition - Not for Production Use.\">", ""); result = result.replace("{\"item\":", ""); result = result.replace("}}", "}"); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } try { jArray = new JSONObject(result); } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } return jArray; } static ArrayList<common> GetSearchResults() { ArrayList<common> results = new ArrayList<common>(); common item_details = new common(); item_details.setName("Home"); item_details.setImageNumber(1); results.add(item_details); item_details = new common(); item_details.setName("Business"); item_details.setImageNumber(2); results.add(item_details); item_details = new common(); item_details.setName("Entertainment"); item_details.setImageNumber(3); results.add(item_details); item_details = new common(); item_details.setName("Health"); item_details.setImageNumber(4); results.add(item_details); item_details = new common(); item_details.setName("Science"); item_details.setImageNumber(5); results.add(item_details); item_details = new common(); item_details.setName("Sports"); item_details.setImageNumber(6); results.add(item_details); item_details = new common(); item_details.setName("World"); item_details.setImageNumber(7); results.add(item_details); item_details = new common(); item_details.setName("Sync"); item_details.setImageNumber(8); results.add(item_details); item_details = new common(); item_details.setName("About"); item_details.setImageNumber(9); results.add(item_details); return results; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getImageNumber() { return imageNumber; } public void setImageNumber(int imageNumber) { this.imageNumber = imageNumber; } private String name; private int imageNumber; public static String getMessage() { final String rank[] = { "Face facts, you are a hilarious drunk.", "The cleaning lady is spitting in your sock drawer.", "The person in the stall before you had untreated genital warts. Good luck!", "The body under the couch is starting to smell.", "You will buy some minors alcohol today.", "Your appearance provides amusement to hundreds every day.", "Your next shower will be interrupted by a large mildly poisonous insect.", "The spider that bit you in your sleep was radioactive and you now have super powers. Go try them out!", "It's time to tell your family about your second head.", "Sell the porn before she finds it.", "The world is your oyster. Gross.", "Your current life-threatening condition was entirely preventable. Isn't that funny?", "A man with a beak will attempt to bill you.", "Your cable company hates you.", "Try to answer every phone call before the first ring finishes. Success will follow", "Your life is a series of ups and downs. Like a toilet plunger.", "There's no time like the present, so have that unsightly growth removed.", "It might be skin cancer or it might just be some leftover BBQ sauce.", "It's Time to dive into a steaming pile of YOU!", "It's time to leap into a jagged canyon of YOU!", "It's time to fall into a fetid pond of YOU!", "It's time to plunge into a rancid lake of YOU!", "It's time to hurl yourself into a rotting swamp of YOU!", "It's been years. Go ahead, expose yourself in public again.", "A fart plays a pivitol role in your relationship.", "You are not as stupid as your friends say you are.", "Your misgivings are grounded in paranoia.", "You will be eaten by coyotes.", "Don't order the miso.", "Tighen the caps on all your medications.", "Your pet humps your pillow when you're at work.", "Your mailman is sleeping with your mother.", "You have the athleticism of a mullosk.", "That smell IS you. Seriously.", "Bathing isn't overrated.", "That hot tip you got will make you look foolish.", "Sell!", "Give up on your dreams, everyone else has.", "A drinking problem would make you more interesting.", "Do it!", "Don't do it!", "The priest is lying about what happened.", "Your parents wanted a boy.", "Your father is not an astronaut.", "Chewing gum will save your life this week.", "Your seats for the show will be abysmal.", "Think twice.", "Don't think, just do.", "Yeah, she was talking about you.", "The receptionist is reading your mail.", "Don't go to the company party.", "Duck!", "Less TV, more porn.", "Your best friend just called you a loser.", "Your boss is plotting against you. You have to stop him. Now!", "Take a lower paying position.", "Your refrigerator is filthy.", "Give up on your dream of being a master debater.", "Being unique just means you don't fit in.", "There's nothing good on tonight.", "Put yourself out there.", "Keep all the receipts, even the embarassing ones.", "Don't worry, you can get to the mailbox before they find out.", "The only thing to fear is fear itself.", "Yep, that's a panda.", "Go with the waffles instead of the pancakes.", "You won't have enough to cover your tab. Wear running shoes.", "A fart will follow you into the car, ruining your date.", "Look over your your tax forms again. Boy are you gonna shit.", "Your curtains are not opaque.", "Check your voicemail.", "Your neighbor harbors a deep resentment.", "Take the long way home. In fact, just give home a miss today.", "You will come into a nice automobile. Make sure to clean up before the owner finds out.", "Yep, it's contagious.", "Truth may be perception, but you're just a freaking liar.", "Time heals all rug burns.", "The bad news is that you're actually related.", "You were adopted.", "Tread lightly.", "You will be the butt of a hilarious joke.", "Against all odds, you will accomplish a basic task.", "You will reach new heights of banality today.", "You will find something you lost and lose it again a short time later.", "You will surrender to mediocrity.", "You will wish you had stayed in bed today.", "You will suddenly realize you're naked on public transportation.", "You will get caught surfing job sites at work.", "You will receive a job offer from a prostitute.", "You will get lost while driving.", "You will suddenly understand both particle physics and empathy.", "You will step in something in a few minutes.", "Your dandruff will increase exponentially and then suddenly stop.", "You will meet a six-foot midget.", "It's clear your future lies in the church. But they won't have you.", "Tell that person you love them. You know who we're talking about. Wait! No, not him! Stop you fool!", "Your coworkers are planning your birthday party. But they have the wrong date.", "Today will be full of hits, misses, and gun jams.", "The milk is going bad as you read this.", "Stick to tea. Coffee is making the tumor bigger.", "You can accomplish anything, as long as it's painfully easy.", "You will get a phone call that will change your life as it relates to pizza.", "You will receive a phone call with an incredible offer about long distance. Take it!", "You will receive a phone call from a Norwegian.", "You will receive a phone call from yourself in the future.", "You will receive a phone call tonight. Wait for six rings before answering.", "Your answering machine is full.", "Your car has a flat.", "Pigeons are living in your wheel wells.", "Replace your tires after 10,000 miles or two years, whichever comes first.", "Your car will begin pulling slightly to the center.", "Three words: Learn how to count.", "Throw the baby out with the bathwater.", "You will contract an illness that's more irritating than dangerous.", "You'll discover your eyes are different sizes.", "Big success and crushing failure this week!", "Ride the rollercoaster. Surf the wave. Basically, give up.", "Eat more fruit.", "Stop stressing over your unfortunate looks.", "Today will bring you great joy. Feel free to run in place like a child.", "Face facts, you have a dreadful singing voice.", "Learn how to drive jerk!", "There's a squirrel in a nearby tree thinking about crapping in your hair.", "The bats in your attic are organizing.", "There is someone living in your walls. And he's pissed.", "The bump on your back is actually the head of your unformed twin.", "There's a video of you on YouTube that you should probably investigate.", "Two words: Head Lice", "Two words: Blunt Trauma", "Two words: Evil Dwarf", "Two words: Alien Contact", "Watch more TV and pay less attention to your family.", "Get Religion. Quick!", "Your workouts would be more effective if you stopped eating between sets.", "Hide from those who mean to tickle you.", "You aren't as attractive as your driver's license indicates.", "Your weight problem is not why people don't like you.", "People like you make me sick.", "The stars say shut up.", "Quit whining about the pain, it only makes it worse for everyone else.", "Take that trip sooner rather than later.", "Get out of the way.", "Wash your hands more often.", "You will fart in front of a large group tomorrow.", "Your zipper is down.", "Your clothes all need to be replaced.", "Things are about to get interesting, but not for you.", "You're not the only one who thinks you're inadequate.", "You have a gay sibling. Shhhh.", "Believe everything you read.", "It's OK, it happens to everyone. Just not as often.", "Your tires were just slashed.", "Your dinner will make you pray for death.", "Today is what life's all about. Go get 'em tiger!", "You are so dead.", "Hide it, no one saw!", "Money isn't everything.", "You win some, you lose some.", "The hardest part is ahead. Stay home and rest.", "Call in sick. You'll know why.", "It's probably just a big freckle. No worries.", "Seriously? You're wearing that?", "Stay away from the parrot.", "A kangaroo will play a pivotal role in the weekend.", "Sure, you're tough. Yikes!", "You have something in your teeth.", "Wipe the seat, pig.", "You will lose at pool this weekend.", "Someone will spit in your taco.", "You will contract a previously unheard of strain of the flu.", "You will scratch yourself at the absolute wrong time.", "Your paranoia is justified.", "You will be mistaken for a celebrity you detest.", "Quit your job. Who needs that crap?", "The Feds want to talk about your surfing habits.", "You will find yourself nervous and gassy this evening.", "You will come up with a great name for your genitals.", "Spend all your money on the lottery. This time it's gonna happen.", "Your poker face needs work.", "Try a new deodorant.", "Your socks don't match.", "Have you changed your diet recently? It smells like it.", "Today is the last day of the rest of your life.", "Money is your friend today. Borrow heavily and play the lottery.", "You will have a chance encounter with romance today so take a bath for god's sake.", "A stranger may mean you harm this week so greet everyone with a blood-curdling shriek.", "Your aura is dim. Have it checked.", "You will be asked to dance by a stranger wearing a sandwich board.", "Your sudden sweet tooth is probably a sign of impending doom.", "Your carpet will stop matching your curtains.", "Keep an eye out for rabid squirrels this month.", "You will join a club and be elected president. Submit immediately to graft and corruption.", "Your missing sock is in a parallel dimension.", "You will find a turtle in your laundry.", "You're no athlete. Stop kidding yourself.", "You miss the toilet more than you think.", "Your bones are slowly turning to dust. Won't be long now.", "Your bank is stealing from you.", "You're not a cannibal, you're just curious.", "The termites in your skull are making more noise than ever this week.", "Know that you're in trouble when your significant other refers to your manhood as a *skin tag*.", "While you were away from home, a stranger showered in your bathroom.", "Every time you lay your head on the pillow, a luminous red dot appears on your forehead.", "It's okay to isolate, as long as you don't do it alone.", "A large windfall is coming to you. Only it's meteorological and not financial.", "Do us a favor, get the surgery. Please.", "To repeat, a prolapsed anus is NOT normal.", "Your organs will be harvested and marketed as souvenirs to the Xeaibobia Tribe of New Guinea.", "That adorable sun freckle on your nose will leave you one year later on a ventilator while your dermatologist lectures you about SPF.", "Tired of the constant accusations, you will buy a dog to take the heat for farting.", "Spend less time worrying about appearances and more time worrying about how people see you.", "You were visited by aliens last night and, based on you, they found our species not worth investigating further.", "You will become illiterate as soon as you finish this sentence.", "Your smile scares children.", "Your new sex move is not pleasurable for anyone. Just stop it.", "The police are listening.", "Your favorite aunt is actually your favorite uncle.", "Baked goods are in abundance.", "They know about the hamsters.", "You need a pet. Go ask someone to pet you.", "Hug your pharmacist.", "Walk it off.", "Stop touching yourself.", "It's not a tumor.", "Breathe less.", "Work out. Then again, why bother?", "All is not lost. But most of it is.", "You'll find it in the last place you look for it.", "Your heroes are all cowards.", "They wouldn't have said it if they didn't mean it.", "Do something for yourself for once. Do it! What did I just say?", "Go ahead and hide from the world. Not that the world will notice.", "Good things come in ones.", "Good things come in tiny packages.", "It's as bad as you think.", "Your next Career: Mime", "Your next Career: Chimney Sweep", "Your next Career: Foxy Boxer", "Your next Career: Ventriloquist", "Your next Career: Powerless Superhero", "Your next Career: Veterinarian", "Your next Career: Horoscope Writer", "Your next Career: Suicide Hotline Operator", "Your next Career: Sears Photographer", "Your next Career: Skeptic", "Your next Career: Seat Filler", "Your next Career: Mortician", "Your next Career: Drunk", "Your next Career: Pet Therapist", "Your next Career: Sketch Artist", "Your next Career: Train Conductor", "Your next Career: Unknown Graphic Novelist", "Your next Career: Square Dance Caller", "Your next Career: Serial Temp", "Your next Career: Ferret Groomer", "Your next Career: Park Security", "Your next Career: Wal-Mart Greeter", "Your next Career: Pubic Stylist", "Your next Career: Salad Bar Security", "Your next Career: Roadkill Cleanup Enforcement", "Your next Career: Taxidermist", "Your next career: Gangster Florist", "Your next career: Master Level Ferris Wheel Operator", "Next Career: Satanic Guidance Counselor", "Your next career: Spit Polisher", "Your Next Career: Gay Prison Guard", "Your next career: Sad Clown Painter.", "The transit of the sun into Cancer is cryptic.", "Friendships and social circles will turn on you in the coming days.", "Your fun side comes out in obscene ways.", "Youll be more inclined to relax almost to the point of coma.", "Tomorrow brings more soup.", "Keep a close eye on your bank balance today, which shouldn't be hard given that you're nearly broke.", "You need to spend some time thinking about your future.", "Romance is very definitely in the air...wait, that's doughnuts.", "Someone who you least expect to be interested...isn't.", "Give encouragement to the hopeless dregs that idolize you.", "The planetary influences will irritate your bowels this week.", "Your exciting plans for the weekend bore the planets.", "Keep your mood swings under control.", "Venus is making itself felt in your pants today.", "Your usually dominant manner is replaced by a coy submissiveness today. Go with it.", "You find your new feelings rather confusing. Speak to the police.", "You have a stepsister living in Mobile, Alabama. She's coming over for dinner tonight.", "You've always been committed to being yourself. Stop it.", "Why be yourself when you can be someone more interesting?", "Your every fleeting thought is a pearl.", "No one makes eating noises quite like you.", "Failure is certainly an option.", "Run! For God's sake, RUN!", "There's no substitute for unbridled laziness.", "That wasn't his finger.", "Your gas problem is not going unnoticed.", "There are bodies in your trunk.", "Don't leave the house for at least a month.", "You are being watched but they're really bored.", "Take a chance on that hottie down in the mailroom with the odd limp.", "It will rain tomorrow. Guaranteed.", "A windfall of lunchmeat awaits.", "There's no one else like you, thank God.", "What you see as a talent, others see as a sign of weakness.", "Stop being late all the time.", "You're no fun. No fun at all.", "Your best friend is sleeping with your pet.", "That favorite coworker of yours secretly hates you.", "Sleep on the floor.", "Stay away from dairy products.", "Your dementia finally gets you somewhere.", "Just go sleep on the couch tonight. You don't want to know why.", "Buy a cat and name it Chris. It will get you out of a jam.", "Clean towels will be key.", "Hide the porn.", "Clear your browser history.", "Don't worry, it doesn't make you gay.", "The goat will become increasingly important.", "That outfit makes you look fat, OK? Get over it.", "You have a great singing voice, stop hoarding it!", "You thought everyone forgot about it, but they didn't.", "Yeah, you're probably gay.", "Name your sexual organs after old time TV characters.", "Your keys are missing.", "Stop smoking. Or start. Whichever you're not doing.", "Kill two birds with one stone.", "Eat out more, but eat less when you're out.", "Eat in more and eat more when you eat.", "Don't wear sunscreen this month.", "Something will slip out of your hand and break your foot tomorrow.", "Bring extra Kleenex.", "You'll throw out your back eating a taco this week.", "The sun is shining up your ass. Enjoy it.", "Clean the gutters, so to speak.", "You will disover a new and apalling skill.", "Shave your head.", "Adopt a chicken.", "Defrost your freezer before it's too late.", "Buy a box of fake eyeballs. You'll thanks us later.", "Repent!", "You will get a worthless coupon in the mail.", "Don't sleep, shriners want to eat you.", "Your lucky number is pi.", "Your lucky number is -6.", "Your lucky number is imaginary.", "Your lucky number is 0.", "Your lucky number is .437.", "Your lucky number is 11.", "Your lucky number is 37.", "Your lucky number is a bakers dozen.", "You will be taken in by the glamour and glitz of online poker and develop carpal tunnel syndrome.", "You will discuss a matter of grave importance with someone you mistake for a coworker.", "You will develop an attractive skin condition.", "You will opt for plastic surgery this year. Label the right part before you go under or they'll have no clue where to start.", "All your furniture has moved one inch to the left.", "Your hair is plotting against you again.", "God will tell you the meaning of your existence, but in a thick, unintelligible accent.", "You're not the only one depressed by your failures.", "Succeeding at sucking is not really success.", "You'll get 'The Devil Went Down to Georgia' stuck in your head for the next week.", "Aaaaah! There's a bug on your shoulder!", "Hold out for a better deal from your boss when he offers to fire you.", "You will fall asleep in a meeting and snore like a leaf blower.", "A sneeze will nearly cripple you tomorrow.", "Your enemies are massing at the borders.", "Nothing tastes as sweet as winning. Except for winning at the expense of others. Remember that.", "You will inherit debt this month.", "An orphaned hillbilly boy will begin living in your fireplace.", "You will inadvertantly shoplift several times this week.", "Your prison record WILL come up. No point hiding it anymore.", "An argument over a parking ticket will lead to romantic entanglement.", "Your favorite cousin wants to sleep with you." }; Random randInt = new Random(); String b = rank[randInt.nextInt(rank.length)]; return b; } }