tag:blogger.com,1999:blog-60887584472123596242024-03-12T17:11:57.677-07:00AndroidBharathihttp://www.blogger.com/profile/03288932038970432575noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-6088758447212359624.post-15535552119023401512015-05-19T04:34:00.001-07:002015-05-19T04:34:48.309-07:00Top 14 Online Jobs from Home – No Investment for Any Online Job<a href="http://surejob.in/online-jobs-from-home.html">Top 14 Online Jobs from Home – No Investment for Any Online Job</a>Bharathihttp://www.blogger.com/profile/03288932038970432575noreply@blogger.com2tag:blogger.com,1999:blog-6088758447212359624.post-21754721192937885672015-05-19T04:30:00.001-07:002015-05-19T04:30:34.507-07:0014 Free Online Jobs from Home to Earn 20K - No Investment<a href="http://surejob.in/online-jobs-from-home.html">14 Free Online Jobs from Home to Earn 20K - No Investment</a>Bharathihttp://www.blogger.com/profile/03288932038970432575noreply@blogger.com2tag:blogger.com,1999:blog-6088758447212359624.post-50495705406180079852012-06-07T03:05:00.002-07:002012-06-07T03:06:07.078-07:00Send e-mail using the JavaMail API using Gmail authentication in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"></span><br />
<div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
</div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">First you have to download three jar files.</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> 1. </span><a href="http://javamail-android.googlecode.com/files/mail.jar" style="border: 0px; color: #4a6b82; cursor: pointer; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">mail.jar</a></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> 2. </span><a href="http://javamail-android.googlecode.com/files/activation.jar" style="border: 0px; color: #4a6b82; cursor: pointer; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">activation.jar</a></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> 3. </span><a href="http://javamail-android.googlecode.com/files/additionnal.jar" style="border: 0px; color: #4a6b82; cursor: pointer; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">additionnal.jar</a></div>
<div style="font-size: 14px; line-height: 18px;">
</div>
<ul style="text-align: left;">
<li><span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">Then Right Click on your project,Select Build Path. From that select Configure Build Path.</span></li>
<li><span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">Then in Libraries Tab select Add External Jars and then select the path where you downloaded jar files.
</span></li>
<li><span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">Add all the three jar files and click ok.</span></li>
</ul>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><b>AutomaticMailActivity.class</b></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">package com.mail;</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">import android.app.Activity;</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">import android.os.Bundle;</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">import android.util.Log;</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">import android.view.View;</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">import android.widget.Button;</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">public class AutomaticMailActivity extends Activity {</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> /** Called when the activity is first created. */</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> @Override</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> public void onCreate(Bundle savedInstanceState) {</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> super.onCreate(savedInstanceState);</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> try { </span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> GMailSender sender = new GMailSender("xxxxxxxxxxxx@gmail.com", "xxxxxxxx");</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> sender.sendMail("This is Subject", </span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> "This is Body", </span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> " xxxxxxxxxxxx@gmail.com "," xxxxxxxxxxxx@gmail.com "); </span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> } catch (Exception e) { </span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> Log.e("SendMail", e.getMessage(), e); </span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> }</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"> }</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;">}</span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><b>GMailSender.class</b></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">package com.mail;</span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import javax.activation.DataHandler; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import javax.activation.DataSource; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import javax.mail.Message; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import javax.mail.PasswordAuthentication; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import javax.mail.Session; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import javax.mail.Transport; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import javax.mail.internet.InternetAddress; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import javax.mail.internet.MimeMessage; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import java.io.ByteArrayInputStream; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import java.io.IOException; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import java.io.InputStream; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import java.io.OutputStream; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import java.security.Security; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import java.util.Properties; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">public class GMailSender extends javax.mail.Authenticator { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> private String mailhost = "smtp.gmail.com"; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> private String user; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> private String password; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> private Session session; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> static { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> Security.addProvider(new com.provider.JSSEProvider()); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public GMailSender(String user, String password) { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> this.user = user; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> this.password = password; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> Properties props = new Properties(); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> props.setProperty("mail.transport.protocol", "smtp"); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> props.setProperty("mail.host", mailhost); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> props.put("mail.smtp.auth", "true"); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> props.put("mail.smtp.port", "465"); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> props.put("mail.smtp.socketFactory.port", "465"); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> props.put("mail.smtp.socketFactory.class", </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> "javax.net.ssl.SSLSocketFactory"); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> props.put("mail.smtp.socketFactory.fallback", "false"); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> props.setProperty("mail.smtp.quitwait", "false"); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> session = Session.getDefaultInstance(props, this); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> protected PasswordAuthentication getPasswordAuthentication() { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> return new PasswordAuthentication(user, password); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> try{</span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> MimeMessage message = new MimeMessage(session); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> message.setSender(new InternetAddress(sender)); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> message.setSubject(subject); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> message.setDataHandler(handler); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> if (recipients.indexOf(',') > 0) </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients)); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> else </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients)); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> Transport.send(message); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> }catch(Exception e){</span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> }</span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public class ByteArrayDataSource implements DataSource { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> private byte[] data; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> private String type; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public ByteArrayDataSource(byte[] data, String type) { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> super(); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> this.data = data; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> this.type = type; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public ByteArrayDataSource(byte[] data) { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> super(); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> this.data = data; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public void setType(String type) { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> this.type = type; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public String getContentType() { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> if (type == null) </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> return "application/octet-stream"; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> else </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> return type; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public InputStream getInputStream() throws IOException { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> return new ByteArrayInputStream(data); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public String getName() { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> return "ByteArrayDataSource"; </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public OutputStream getOutputStream() throws IOException { </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> throw new IOException("Not Supported"); </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> } </span></span><br />
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">} </span></span><br />
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><b>JSSEProvider.class</b></span></div>
<div style="font-size: 14px; line-height: 18px;">
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><br /></span></div>
<div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">package com.provider;</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import java.security.AccessController;</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">import java.security.Provider;</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">public final class JSSEProvider extends Provider {</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public JSSEProvider() {</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> public Void run() {</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> put("SSLContext.TLS",</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> put("Alg.Alias.SSLContext.TLSv1", "TLS");</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> put("KeyManagerFactory.X509",</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> put("TrustManagerFactory.X509",</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> return null;</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> }</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> });</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> }</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">}</span></span></div>
</div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><b>main.xml</b></span>
</span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><b><br /></b></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;">No need to add anything here .leave the default one.</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><b>AndroidManifest.xml</b></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><?xml version="1.0" encoding="utf-8"?></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><manifest xmlns:android="http://schemas.android.com/apk/res/android"</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> package="com.mail"</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> android:versionCode="1"</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> android:versionName="1.0" ></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> <uses-sdk android:minSdkVersion="8" /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> <application</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> android:icon="@drawable/ic_launcher"</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> android:label="@string/app_name" ></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> <activity</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> android:name=".AutomaticMailActivity"</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> android:label="@string/app_name" ></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> <intent-filter></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> <action android:name="android.intent.action.MAIN" /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><br /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> <category android:name="android.intent.category.LAUNCHER" /></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> </intent-filter></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> </activity></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"> </application></span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"><uses-permission android:name="android.permission.INTERNET"/>"</span></span></div>
<div>
<span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"><span style="font-size: 14px; line-height: 18px;"></manifest></span></span></div>
</div>
<br /></div>Bharathihttp://www.blogger.com/profile/03288932038970432575noreply@blogger.com30tag:blogger.com,1999:blog-6088758447212359624.post-1295307400815418352012-06-05T01:38:00.001-07:002012-06-05T02:07:42.153-07:00Facebook Integration in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
First you have to register your app in
<a href="https://developers.facebook.com/">https://developers.facebook.com/</a> and get app Id<br />
<b>Login.class</b><br />
<br />
package com.kmiller.facebookintegration;<br />
<br />
import org.json.JSONObject;<br />
import com.facebook.android.DialogError;<br />
import com.facebook.android.Facebook;<br />
import com.facebook.android.FacebookError;<br />
import com.facebook.android.Facebook.DialogListener;<br />
import android.app.Activity;<br />
import android.content.Intent;<br />
import android.os.Bundle;<br />
import android.view.View;<br />
import android.view.View.OnClickListener;<br />
import android.widget.Button;<br />
import android.widget.Toast;<br />
<br />
public class Login extends Activity {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public static final String mAPP_ID = "<b><your_app_id_here></b>";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public Facebook mFacebook = new Facebook(mAPP_ID);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
@Override<br />
public void onCreate(Bundle savedInstanceState) {<br />
super.onCreate(savedInstanceState);<br />
setContentView(R.layout.main);<br />
((Button)findViewById(R.id.LoginButton)).setOnClickListener( loginButtonListener );<br />
SessionStore.restore(mFacebook, this);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
protected void onActivityResult(int requestCode, int resultCode, Intent data) {<br />
mFacebook.authorizeCallback(requestCode, resultCode, data);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
private OnClickListener loginButtonListener = new OnClickListener() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onClick( View v ) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if( !mFacebook.isSessionValid() ) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText(Login.this, "Authorizing", Toast.LENGTH_SHORT).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mFacebook.authorize(Login.this, new String[] { "" }, new LoginDialogListener());<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, "Has valid session", Toast.LENGTH_SHORT).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>JSONObject json = Util.parseJson(mFacebook.request("me"));<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>String facebookID = json.getString("id");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>String firstName = json.getString("first_name");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>String lastName = json.getString("last_name");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText(Login.this, "You already have a valid session, " + firstName + " " + lastName + ". No need to re-authorize.", Toast.LENGTH_SHORT).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch( Exception error ) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, error.toString(), Toast.LENGTH_SHORT).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch( FacebookError error ) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, error.toString(), Toast.LENGTH_SHORT).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>};<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public final class LoginDialogListener implements DialogListener {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onComplete(Bundle values) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>JSONObject json = Util.parseJson(mFacebook.request("me"));<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String facebookID = json.getString("id");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String firstName = json.getString("first_name");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String lastName = json.getString("last_name");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, "Thank you for Logging In, " + firstName + " " + lastName + "!", Toast.LENGTH_SHORT).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>SessionStore.save(mFacebook, Login.this);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch( Exception error ) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, error.toString(), Toast.LENGTH_SHORT).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch( FacebookError error ) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, error.toString(), Toast.LENGTH_SHORT).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onFacebookError(FacebookError error) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> }<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onError(DialogError error) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
public void onCancel() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText( Login.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
<b>SessionStore.class </b><br />
<b><br /></b><br />
<br />
package com.kmiller.facebookintegration;<br />
<br />
import com.facebook.android.Facebook;<br />
import android.content.Context;<br />
import android.content.SharedPreferences;<br />
import android.content.SharedPreferences.Editor;<br />
<br />
public class SessionStore {<br />
<br />
private static final String TOKEN = "access_token";<br />
private static final String EXPIRES = "expires_in";<br />
private static final String KEY = "facebook-session";<br />
<br />
public static boolean save(Facebook session, Context context) {<br />
Editor editor =<br />
context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();<br />
editor.putString(TOKEN, session.getAccessToken());<br />
editor.putLong(EXPIRES, session.getAccessExpires());<br />
return editor.commit();<br />
}<br />
<br />
public static boolean restore(Facebook session, Context context) {<br />
SharedPreferences savedSession =<br />
context.getSharedPreferences(KEY, Context.MODE_PRIVATE);<br />
session.setAccessToken(savedSession.getString(TOKEN, null));<br />
session.setAccessExpires(savedSession.getLong(EXPIRES, 0));<br />
return session.isSessionValid();<br />
}<br />
<br />
public static void clear(Context context) {<br />
Editor editor =<br />
context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();<br />
editor.clear();<br />
editor.commit();<br />
}<br />
<br />
}<br />
<div style="font-weight: bold;">
<br /></div>
<div>
<b>Util.class</b></div>
<div>
<div>
<br /></div>
<div>
package com.kmiller.facebookintegration;</div>
<div>
<br /></div>
<div>
import java.io.BufferedOutputStream;</div>
<div>
import java.io.BufferedReader;</div>
<div>
import java.io.FileNotFoundException;</div>
<div>
import java.io.IOException;</div>
<div>
import java.io.InputStream;</div>
<div>
import java.io.InputStreamReader;</div>
<div>
import java.io.OutputStream;</div>
<div>
import java.net.HttpURLConnection;</div>
<div>
import java.net.MalformedURLException;</div>
<div>
import java.net.URL;</div>
<div>
import java.net.URLDecoder;</div>
<div>
import java.net.URLEncoder;</div>
<div>
import org.json.JSONException;</div>
<div>
import org.json.JSONObject;</div>
<div>
import com.facebook.android.FacebookError;</div>
<div>
import android.app.AlertDialog.Builder;</div>
<div>
import android.content.Context;</div>
<div>
import android.os.Bundle;</div>
<div>
import android.util.Log;</div>
<div>
import android.webkit.CookieManager;</div>
<div>
import android.webkit.CookieSyncManager;</div>
<div>
<br /></div>
<div>
public final class Util {</div>
<div>
<br /></div>
<div>
public static String encodePostBody(Bundle parameters, String boundary) {</div>
<div>
if (parameters == null) return "";</div>
<div>
StringBuilder sb = new StringBuilder();</div>
<div>
<br /></div>
<div>
for (String key : parameters.keySet()) {</div>
<div>
if (parameters.getByteArray(key) != null) {</div>
<div>
continue;</div>
<div>
}</div>
<div>
<br /></div>
<div>
sb.append("Content-Disposition: form-data; name=\"" + key +</div>
<div>
"\"\r\n\r\n" + parameters.getString(key));</div>
<div>
sb.append("\r\n" + "--" + boundary + "\r\n");</div>
<div>
}</div>
<div>
<br /></div>
<div>
return sb.toString();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static String encodeUrl(Bundle parameters) {</div>
<div>
if (parameters == null) {</div>
<div>
return "";</div>
<div>
}</div>
<div>
<br /></div>
<div>
StringBuilder sb = new StringBuilder();</div>
<div>
boolean first = true;</div>
<div>
for (String key : parameters.keySet()) {</div>
<div>
if (first) first = false; else sb.append("&");</div>
<div>
sb.append(URLEncoder.encode(key) + "=" +</div>
<div>
URLEncoder.encode(parameters.getString(key)));</div>
<div>
}</div>
<div>
return sb.toString();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static Bundle decodeUrl(String s) {</div>
<div>
Bundle params = new Bundle();</div>
<div>
if (s != null) {</div>
<div>
String array[] = s.split("&");</div>
<div>
for (String parameter : array) {</div>
<div>
String v[] = parameter.split("=");</div>
<div>
params.putString(URLDecoder.decode(v[0]),</div>
<div>
URLDecoder.decode(v[1]));</div>
<div>
}</div>
<div>
}</div>
<div>
return params;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static Bundle parseUrl(String url) {</div>
<div>
url = url.replace("fbconnect", "http");</div>
<div>
try {</div>
<div>
URL u = new URL(url);</div>
<div>
Bundle b = decodeUrl(u.getQuery());</div>
<div>
b.putAll(decodeUrl(u.getRef()));</div>
<div>
return b;</div>
<div>
} catch (MalformedURLException e) {</div>
<div>
return new Bundle();</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static String openUrl(String url, String method, Bundle params)</div>
<div>
throws MalformedURLException, IOException {</div>
<div>
String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";</div>
<div>
String endLine = "\r\n";</div>
<div>
OutputStream os;</div>
<div>
<br /></div>
<div>
if (method.equals("GET")) {</div>
<div>
url = url + "?" + encodeUrl(params);</div>
<div>
}</div>
<div>
Log.d("Facebook-Util", method + " URL: " + url);</div>
<div>
HttpURLConnection conn =</div>
<div>
(HttpURLConnection) new URL(url).openConnection();</div>
<div>
conn.setRequestProperty("User-Agent", System.getProperties().</div>
<div>
getProperty("http.agent") + " FacebookAndroidSDK");</div>
<div>
if (!method.equals("GET")) {</div>
<div>
Bundle dataparams = new Bundle();</div>
<div>
for (String key : params.keySet()) {</div>
<div>
if (params.getByteArray(key) != null) {</div>
<div>
dataparams.putByteArray(key, params.getByteArray(key));</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
if (!params.containsKey("method")) {</div>
<div>
params.putString("method", method);</div>
<div>
}</div>
<div>
<br /></div>
<div>
if (params.containsKey("access_token")) {</div>
<div>
String decoded_token =</div>
<div>
URLDecoder.decode(params.getString("access_token"));</div>
<div>
params.putString("access_token", decoded_token);</div>
<div>
}</div>
<div>
<br /></div>
<div>
conn.setRequestMethod("POST");</div>
<div>
conn.setRequestProperty(</div>
<div>
"Content-Type",</div>
<div>
"multipart/form-data;boundary="+strBoundary);</div>
<div>
conn.setDoOutput(true);</div>
<div>
conn.setDoInput(true);</div>
<div>
conn.setRequestProperty("Connection", "Keep-Alive");</div>
<div>
conn.connect();</div>
<div>
os = new BufferedOutputStream(conn.getOutputStream());</div>
<div>
<br /></div>
<div>
os.write(("--" + strBoundary +endLine).getBytes());</div>
<div>
os.write((encodePostBody(params, strBoundary)).getBytes());</div>
<div>
os.write((endLine + "--" + strBoundary + endLine).getBytes());</div>
<div>
<br /></div>
<div>
if (!dataparams.isEmpty()) {</div>
<div>
<br /></div>
<div>
for (String key: dataparams.keySet()){</div>
<div>
os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());</div>
<div>
os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());</div>
<div>
os.write(dataparams.getByteArray(key));</div>
<div>
os.write((endLine + "--" + strBoundary + endLine).getBytes());</div>
<div>
<br /></div>
<div>
}</div>
<div>
}</div>
<div>
os.flush();</div>
<div>
}</div>
<div>
<br /></div>
<div>
String response = "";</div>
<div>
try {</div>
<div>
response = read(conn.getInputStream());</div>
<div>
} catch (FileNotFoundException e) {</div>
<div>
response = read(conn.getErrorStream());</div>
<div>
}</div>
<div>
return response;</div>
<div>
}</div>
<div>
<br /></div>
<div>
private static String read(InputStream in) throws IOException {</div>
<div>
StringBuilder sb = new StringBuilder();</div>
<div>
BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);</div>
<div>
for (String line = r.readLine(); line != null; line = r.readLine()) {</div>
<div>
sb.append(line);</div>
<div>
}</div>
<div>
in.close();</div>
<div>
return sb.toString();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static void clearCookies(Context context) {</div>
<div>
@SuppressWarnings("unused")</div>
<div>
CookieSyncManager cookieSyncMngr =</div>
<div>
CookieSyncManager.createInstance(context);</div>
<div>
CookieManager cookieManager = CookieManager.getInstance();</div>
<div>
cookieManager.removeAllCookie();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static JSONObject parseJson(String response)</div>
<div>
throws JSONException, FacebookError {</div>
<div>
if (response.equals("false")) {</div>
<div>
throw new FacebookError("request failed");</div>
<div>
}</div>
<div>
if (response.equals("true")) {</div>
<div>
response = "{value : true}";</div>
<div>
}</div>
<div>
JSONObject json = new JSONObject(response);</div>
<div>
if (json.has("error")) {</div>
<div>
JSONObject error = json.getJSONObject("error");</div>
<div>
throw new FacebookError(</div>
<div>
error.getString("message"), error.getString("type"), 0);</div>
<div>
}</div>
<div>
if (json.has("error_code") && json.has("error_msg")) {</div>
<div>
throw new FacebookError(json.getString("error_msg"), "",</div>
<div>
Integer.parseInt(json.getString("error_code")));</div>
<div>
}</div>
<div>
if (json.has("error_code")) {</div>
<div>
throw new FacebookError("request failed", "",</div>
<div>
Integer.parseInt(json.getString("error_code")));</div>
<div>
}</div>
<div>
if (json.has("error_msg")) {</div>
<div>
throw new FacebookError(json.getString("error_msg"));</div>
<div>
}</div>
<div>
if (json.has("error_reason")) {</div>
<div>
throw new FacebookError(json.getString("error_reason"));</div>
<div>
}</div>
<div>
return json;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static void showAlert(Context context, String title, String text) {</div>
<div>
Builder alertBuilder = new Builder(context);</div>
<div>
alertBuilder.setTitle(title);</div>
<div>
alertBuilder.setMessage(text);</div>
<div>
alertBuilder.create().show();</div>
<div>
}</div>
<div>
<br /></div>
<div>
}</div>
<div style="font-weight: bold;">
<br /></div>
</div>
<div>
<b>AsyncFacebookRunner .class</b></div>
<div>
<b><br /></b></div>
<div>
<div>
package com.facebook.android;</div>
<div>
<br /></div>
<div>
import java.io.FileNotFoundException;</div>
<div>
import java.io.IOException;</div>
<div>
import java.net.MalformedURLException;</div>
<div>
import android.content.Context;</div>
<div>
import android.os.Bundle;</div>
<div>
<br /></div>
<div>
public class AsyncFacebookRunner {</div>
<div>
<br /></div>
<div>
Facebook fb;</div>
<div>
<br /></div>
<div>
public AsyncFacebookRunner(Facebook fb) {</div>
<div>
this.fb = fb;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void logout(final Context context, final RequestListener listener) {</div>
<div>
new Thread() {</div>
<div>
@Override public void run() {</div>
<div>
try {</div>
<div>
String response = fb.logout(context);</div>
<div>
if (response.length() == 0 || response.equals("false")){</div>
<div>
listener.onFacebookError(new FacebookError(</div>
<div>
"auth.expireSession failed"));</div>
<div>
return;</div>
<div>
}</div>
<div>
listener.onComplete(response);</div>
<div>
} catch (FileNotFoundException e) {</div>
<div>
listener.onFileNotFoundException(e);</div>
<div>
} catch (MalformedURLException e) {</div>
<div>
listener.onMalformedURLException(e);</div>
<div>
} catch (IOException e) {</div>
<div>
listener.onIOException(e);</div>
<div>
}</div>
<div>
}</div>
<div>
}.start();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void request(Bundle parameters,</div>
<div>
RequestListener listener) {</div>
<div>
request(null, parameters, "GET", listener);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void request(String graphPath,</div>
<div>
RequestListener listener) {</div>
<div>
request(graphPath, new Bundle(), "GET", listener);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void request(String graphPath,</div>
<div>
Bundle parameters,</div>
<div>
RequestListener listener) {</div>
<div>
request(graphPath, parameters, "GET", listener);</div>
<div>
}</div>
<div>
</div>
<div>
public void request(final String graphPath,</div>
<div>
final Bundle parameters,</div>
<div>
final String httpMethod,</div>
<div>
final RequestListener listener) {</div>
<div>
new Thread() {</div>
<div>
@Override public void run() {</div>
<div>
try {</div>
<div>
String resp = fb.request(graphPath, parameters, httpMethod);</div>
<div>
listener.onComplete(resp);</div>
<div>
} catch (FileNotFoundException e) {</div>
<div>
listener.onFileNotFoundException(e);</div>
<div>
} catch (MalformedURLException e) {</div>
<div>
listener.onMalformedURLException(e);</div>
<div>
} catch (IOException e) {</div>
<div>
listener.onIOException(e);</div>
<div>
}</div>
<div>
}</div>
<div>
}.start();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static interface RequestListener {</div>
<div>
<br /></div>
<div>
public void onComplete(String response);</div>
<div>
public void onIOException(IOException e);</div>
<div>
<br /></div>
<div>
public void onFileNotFoundException(FileNotFoundException e);</div>
<div>
<br /></div>
<div>
public void onMalformedURLException(MalformedURLException e);</div>
<div>
<br /></div>
<div>
public void onFacebookError(FacebookError e);</div>
<div>
<br /></div>
<div>
}</div>
<div>
<br /></div>
<div>
}</div>
<div style="font-weight: bold;">
<br /></div>
</div>
<div>
<b>DialogError.class</b></div>
<div>
<b><br /></b></div>
<div>
<div>
package com.facebook.android;</div>
<div>
<br /></div>
<div>
public class DialogError extends Throwable {</div>
<div>
<br /></div>
<div>
private static final long serialVersionUID = 1L;</div>
<div>
<br /></div>
<div>
private int mErrorCode;</div>
<div>
<br /></div>
<div>
private String mFailingUrl;</div>
<div>
<br /></div>
<div>
public DialogError(String message, int errorCode, String failingUrl) {</div>
<div>
super(message);</div>
<div>
mErrorCode = errorCode;</div>
<div>
mFailingUrl = failingUrl;</div>
<div>
}</div>
<div>
<br /></div>
<div>
int getErrorCode() {</div>
<div>
return mErrorCode;</div>
<div>
}</div>
<div>
<br /></div>
<div>
String getFailingUrl() {</div>
<div>
return mFailingUrl;</div>
<div>
}</div>
<div>
<br /></div>
<div>
}</div>
<div style="font-weight: bold;">
<br /></div>
</div>
<div>
<b>Facebook.class</b></div>
<div>
<div>
<br /></div>
<div>
package com.facebook.android;</div>
<div>
<br /></div>
<div>
import java.io.FileNotFoundException;</div>
<div>
import java.io.IOException;</div>
<div>
import java.net.MalformedURLException;</div>
<div>
import android.Manifest;</div>
<div>
import android.app.Activity;</div>
<div>
import android.content.ActivityNotFoundException;</div>
<div>
import android.content.Context;</div>
<div>
import android.content.Intent;</div>
<div>
import android.content.pm.PackageInfo;</div>
<div>
import android.content.pm.PackageManager;</div>
<div>
import android.content.pm.PackageManager.NameNotFoundException;</div>
<div>
import android.content.pm.ResolveInfo;</div>
<div>
import android.content.pm.Signature;</div>
<div>
import android.os.Bundle;</div>
<div>
import android.text.TextUtils;</div>
<div>
import android.util.Log;</div>
<div>
import android.webkit.CookieSyncManager;</div>
<div>
<br /></div>
<div>
public class Facebook {</div>
<div>
<br /></div>
<div>
public static final String REDIRECT_URI = "fbconnect://success";</div>
<div>
public static final String CANCEL_URI = "fbconnect://cancel";</div>
<div>
public static final String TOKEN = "access_token";</div>
<div>
public static final String EXPIRES = "expires_in";</div>
<div>
public static final String SINGLE_SIGN_ON_DISABLED = "service_disabled";</div>
<div>
<br /></div>
<div>
public static final int FORCE_DIALOG_AUTH = -1;</div>
<div>
<br /></div>
<div>
private static final String LOGIN = "oauth";</div>
<div>
<br /></div>
<div>
private static final int DEFAULT_AUTH_ACTIVITY_CODE = 32665;</div>
<div>
<br /></div>
<div>
protected static String DIALOG_BASE_URL =</div>
<div>
"https://m.facebook.com/dialog/";</div>
<div>
protected static String GRAPH_BASE_URL =</div>
<div>
"https://graph.facebook.com/";</div>
<div>
protected static String RESTSERVER_URL =</div>
<div>
"https://api.facebook.com/restserver.php";</div>
<div>
<br /></div>
<div>
private String mAccessToken = null;</div>
<div>
private long mAccessExpires = 0;</div>
<div>
private String mAppId;</div>
<div>
<br /></div>
<div>
private Activity mAuthActivity;</div>
<div>
private String[] mAuthPermissions;</div>
<div>
private int mAuthActivityCode;</div>
<div>
private DialogListener mAuthDialogListener;</div>
<div>
<br /></div>
<div>
public Facebook(String appId) {</div>
<div>
if (appId == null) {</div>
<div>
throw new IllegalArgumentException(</div>
<div>
"You must specify your application ID when instantiating " +</div>
<div>
"a Facebook object. See README for details.");</div>
<div>
}</div>
<div>
mAppId = appId;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void authorize(Activity activity, final DialogListener listener) {</div>
<div>
authorize(activity, new String[] {}, DEFAULT_AUTH_ACTIVITY_CODE,</div>
<div>
listener);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void authorize(Activity activity, String[] permissions,</div>
<div>
final DialogListener listener) {</div>
<div>
authorize(activity, permissions, DEFAULT_AUTH_ACTIVITY_CODE, listener);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void authorize(Activity activity, String[] permissions,</div>
<div>
int activityCode, final DialogListener listener) {</div>
<div>
<br /></div>
<div>
boolean singleSignOnStarted = false;</div>
<div>
<br /></div>
<div>
mAuthDialogListener = listener;</div>
<div>
<br /></div>
<div>
if (activityCode >= 0) {</div>
<div>
singleSignOnStarted = startSingleSignOn(activity, mAppId,</div>
<div>
permissions, activityCode);</div>
<div>
}</div>
<div>
if (!singleSignOnStarted) {</div>
<div>
startDialogAuth(activity, permissions);</div>
<div>
}</div>
<div>
}</div>
<div>
private boolean startSingleSignOn(Activity activity, String applicationId,</div>
<div>
String[] permissions, int activityCode) {</div>
<div>
boolean didSucceed = true;</div>
<div>
Intent intent = new Intent();</div>
<div>
<br /></div>
<div>
intent.setClassName("com.facebook.katana",</div>
<div>
"com.facebook.katana.ProxyAuth");</div>
<div>
intent.putExtra("client_id", applicationId);</div>
<div>
if (permissions.length > 0) {</div>
<div>
intent.putExtra("scope", TextUtils.join(",", permissions));</div>
<div>
}</div>
<div>
<br /></div>
<div>
if (!validateAppSignatureForIntent(activity, intent)) {</div>
<div>
return false;</div>
<div>
}</div>
<div>
<br /></div>
<div>
mAuthActivity = activity;</div>
<div>
mAuthPermissions = permissions;</div>
<div>
mAuthActivityCode = activityCode;</div>
<div>
try {</div>
<div>
activity.startActivityForResult(intent, activityCode);</div>
<div>
} catch (ActivityNotFoundException e) {</div>
<div>
didSucceed = false;</div>
<div>
}</div>
<div>
<br /></div>
<div>
return didSucceed;</div>
<div>
}</div>
<div>
<br /></div>
<div>
private boolean validateAppSignatureForIntent(Activity activity,</div>
<div>
Intent intent) {</div>
<div>
<br /></div>
<div>
ResolveInfo resolveInfo =</div>
<div>
activity.getPackageManager().resolveActivity(intent, 0);</div>
<div>
if (resolveInfo == null) {</div>
<div>
return false;</div>
<div>
}</div>
<div>
<br /></div>
<div>
String packageName = resolveInfo.activityInfo.packageName;</div>
<div>
PackageInfo packageInfo;</div>
<div>
try {</div>
<div>
packageInfo = activity.getPackageManager().getPackageInfo(</div>
<div>
packageName, PackageManager.GET_SIGNATURES);</div>
<div>
} catch (NameNotFoundException e) {</div>
<div>
return false;</div>
<div>
}</div>
<div>
<br /></div>
<div>
for (Signature signature : packageInfo.signatures) {</div>
<div>
if (signature.toCharsString().equals(FB_APP_SIGNATURE)) {</div>
<div>
return true;</div>
<div>
}</div>
<div>
}</div>
<div>
return false;</div>
<div>
}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>private void startDialogAuth(Activity activity, String[] permissions) {</div>
<div>
Bundle params = new Bundle();</div>
<div>
if (permissions.length > 0) {</div>
<div>
params.putString("scope", TextUtils.join(",", permissions));</div>
<div>
}</div>
<div>
CookieSyncManager.createInstance(activity);</div>
<div>
dialog(activity, LOGIN, params, new DialogListener() {</div>
<div>
<br /></div>
<div>
public void onComplete(Bundle values) {</div>
<div>
CookieSyncManager.getInstance().sync();</div>
<div>
setAccessToken(values.getString(TOKEN));</div>
<div>
setAccessExpiresIn(values.getString(EXPIRES));</div>
<div>
if (isSessionValid()) {</div>
<div>
Log.d("Facebook-authorize", "Login Success! access_token="</div>
<div>
+ getAccessToken() + " expires="</div>
<div>
+ getAccessExpires());</div>
<div>
mAuthDialogListener.onComplete(values);</div>
<div>
} else {</div>
<div>
mAuthDialogListener.onFacebookError(new FacebookError(</div>
<div>
"Failed to receive access token."));</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void onError(DialogError error) {</div>
<div>
Log.d("Facebook-authorize", "Login failed: " + error);</div>
<div>
mAuthDialogListener.onError(error);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void onFacebookError(FacebookError error) {</div>
<div>
Log.d("Facebook-authorize", "Login failed: " + error);</div>
<div>
mAuthDialogListener.onFacebookError(error);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void onCancel() {</div>
<div>
Log.d("Facebook-authorize", "Login canceled");</div>
<div>
mAuthDialogListener.onCancel();</div>
<div>
}</div>
<div>
});</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void authorizeCallback(int requestCode, int resultCode, Intent data) {</div>
<div>
if (requestCode == mAuthActivityCode) {</div>
<div>
<br /></div>
<div>
if (resultCode == Activity.RESULT_OK) {</div>
<div>
<br /></div>
<div>
String error = data.getStringExtra("error");</div>
<div>
if (error == null) {</div>
<div>
error = data.getStringExtra("error_type");</div>
<div>
}</div>
<div>
<br /></div>
<div>
if (error != null) {</div>
<div>
if (error.equals(SINGLE_SIGN_ON_DISABLED)</div>
<div>
|| error.equals("AndroidAuthKillSwitchException")) {</div>
<div>
Log.d("Facebook-authorize", "Hosted auth currently "</div>
<div>
+ "disabled. Retrying dialog auth...");</div>
<div>
startDialogAuth(mAuthActivity, mAuthPermissions);</div>
<div>
} else if (error.equals("access_denied")</div>
<div>
|| error.equals("OAuthAccessDeniedException")) {</div>
<div>
Log.d("Facebook-authorize", "Login canceled by user.");</div>
<div>
mAuthDialogListener.onCancel();</div>
<div>
} else {</div>
<div>
Log.d("Facebook-authorize", "Login failed: " + error);</div>
<div>
mAuthDialogListener.onFacebookError(</div>
<div>
new FacebookError(error));</div>
<div>
}</div>
<div>
<br /></div>
<div>
} else {</div>
<div>
setAccessToken(data.getStringExtra(TOKEN));</div>
<div>
setAccessExpiresIn(data.getStringExtra(EXPIRES));</div>
<div>
if (isSessionValid()) {</div>
<div>
Log.d("Facebook-authorize",</div>
<div>
"Login Success! access_token="</div>
<div>
+ getAccessToken() + " expires="</div>
<div>
+ getAccessExpires());</div>
<div>
mAuthDialogListener.onComplete(data.getExtras());</div>
<div>
} else {</div>
<div>
mAuthDialogListener.onFacebookError(new FacebookError(</div>
<div>
"Failed to receive access token."));</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
} else if (resultCode == Activity.RESULT_CANCELED) {</div>
<div>
<br /></div>
<div>
if (data != null) {</div>
<div>
Log.d("Facebook-authorize",</div>
<div>
"Login failed: " + data.getStringExtra("error"));</div>
<div>
mAuthDialogListener.onError(</div>
<div>
new DialogError(</div>
<div>
data.getStringExtra("error"),</div>
<div>
data.getIntExtra("error_code", -1),</div>
<div>
data.getStringExtra("failing_url")));</div>
<div>
<br /></div>
<div>
} else {</div>
<div>
Log.d("Facebook-authorize", "Login canceled by user.");</div>
<div>
mAuthDialogListener.onCancel();</div>
<div>
}</div>
<div>
}</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
public String logout(Context context)</div>
<div>
throws MalformedURLException, IOException {</div>
<div>
Util.clearCookies(context);</div>
<div>
Bundle b = new Bundle();</div>
<div>
b.putString("method", "auth.expireSession");</div>
<div>
String response = request(b);</div>
<div>
setAccessToken(null);</div>
<div>
setAccessExpires(0);</div>
<div>
return response;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public String request(Bundle parameters)</div>
<div>
throws MalformedURLException, IOException {</div>
<div>
if (!parameters.containsKey("method")) {</div>
<div>
throw new IllegalArgumentException("API method must be specified. "</div>
<div>
+ "(parameters must contain key \"method\" and value). See"</div>
<div>
+ " http://developers.facebook.com/docs/reference/rest/");</div>
<div>
}</div>
<div>
return request(null, parameters, "GET");</div>
<div>
}</div>
<div>
<br /></div>
<div>
public String request(String graphPath)</div>
<div>
throws MalformedURLException, IOException {</div>
<div>
return request(graphPath, new Bundle(), "GET");</div>
<div>
}</div>
<div>
<br /></div>
<div>
public String request(String graphPath, Bundle parameters)</div>
<div>
throws MalformedURLException, IOException {</div>
<div>
return request(graphPath, parameters, "GET");</div>
<div>
}</div>
<div>
<br /></div>
<div>
public String request(String graphPath, Bundle params, String httpMethod)</div>
<div>
throws FileNotFoundException, MalformedURLException, IOException {</div>
<div>
params.putString("format", "json");</div>
<div>
if (isSessionValid()) {</div>
<div>
params.putString(TOKEN, getAccessToken());</div>
<div>
}</div>
<div>
String url = (graphPath != null) ? GRAPH_BASE_URL + graphPath</div>
<div>
: RESTSERVER_URL;</div>
<div>
return Util.openUrl(url, httpMethod, params);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void dialog(Context context, String action,</div>
<div>
DialogListener listener) {</div>
<div>
dialog(context, action, new Bundle(), listener);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void dialog(Context context, String action, Bundle parameters,</div>
<div>
final DialogListener listener) {</div>
<div>
<br /></div>
<div>
String endpoint = DIALOG_BASE_URL + action;</div>
<div>
parameters.putString("display", "touch");</div>
<div>
parameters.putString("redirect_uri", REDIRECT_URI);</div>
<div>
<br /></div>
<div>
if (action.equals(LOGIN)) {</div>
<div>
parameters.putString("type", "user_agent");</div>
<div>
parameters.putString("client_id", mAppId);</div>
<div>
} else {</div>
<div>
parameters.putString("app_id", mAppId);</div>
<div>
}</div>
<div>
<br /></div>
<div>
if (isSessionValid()) {</div>
<div>
parameters.putString(TOKEN, getAccessToken());</div>
<div>
}</div>
<div>
String url = endpoint + "?" + Util.encodeUrl(parameters);</div>
<div>
if (context.checkCallingOrSelfPermission(Manifest.permission.INTERNET)</div>
<div>
!= PackageManager.PERMISSION_GRANTED) {</div>
<div>
Util.showAlert(context, "Error",</div>
<div>
"Application requires permission to access the Internet");</div>
<div>
} else {</div>
<div>
new FbDialog(context, url, listener).show();</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
public boolean isSessionValid() {</div>
<div>
return (getAccessToken() != null) &&</div>
<div>
((getAccessExpires() == 0) ||</div>
<div>
(System.currentTimeMillis() < getAccessExpires()));</div>
<div>
}</div>
<div>
<br /></div>
<div>
public String getAccessToken() {</div>
<div>
return mAccessToken;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public long getAccessExpires() {</div>
<div>
return mAccessExpires;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void setAccessToken(String token) {</div>
<div>
mAccessToken = token;</div>
<div>
}</div>
<div>
public void setAccessExpires(long time) {</div>
<div>
mAccessExpires = time;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void setAccessExpiresIn(String expiresIn) {</div>
<div>
if (expiresIn != null && !expiresIn.equals("0")) {</div>
<div>
setAccessExpires(System.currentTimeMillis()</div>
<div>
+ Integer.parseInt(expiresIn) * 1000);</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
public String getAppId() {</div>
<div>
return mAppId;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public void setAppId(String appId) {</div>
<div>
mAppId = appId;</div>
<div>
}</div>
<div>
public static interface DialogListener {</div>
<div>
<br /></div>
<div>
public void onComplete(Bundle values);</div>
<div>
<br /></div>
<div>
public void onFacebookError(FacebookError e);</div>
<div>
<br /></div>
<div>
public void onError(DialogError e);</div>
<div>
<br /></div>
<div>
public void onCancel();</div>
<div>
<br /></div>
<div>
}</div>
<div>
<br /></div>
<div>
public static final String FB_APP_SIGNATURE =</div>
<div>
"30820268308201d102044a9c4610300d06092a864886f70d0101040500307a310"</div>
<div>
+ "b3009060355040613025553310b30090603550408130243413112301006035504"</div>
<div>
+ "07130950616c6f20416c746f31183016060355040a130f46616365626f6f6b204"</div>
<div>
+ "d6f62696c653111300f060355040b130846616365626f6f6b311d301b06035504"</div>
<div>
+ "03131446616365626f6f6b20436f72706f726174696f6e3020170d30393038333"</div>
<div>
+ "13231353231365a180f32303530303932353231353231365a307a310b30090603"</div>
<div>
+ "55040613025553310b30090603550408130243413112301006035504071309506"</div>
<div>
+ "16c6f20416c746f31183016060355040a130f46616365626f6f6b204d6f62696c"</div>
<div>
+ "653111300f060355040b130846616365626f6f6b311d301b06035504031314466"</div>
<div>
+ "16365626f6f6b20436f72706f726174696f6e30819f300d06092a864886f70d01"</div>
<div>
+ "0101050003818d0030818902818100c207d51df8eb8c97d93ba0c8c1002c928fa"</div>
<div>
+ "b00dc1b42fca5e66e99cc3023ed2d214d822bc59e8e35ddcf5f44c7ae8ade50d7"</div>
<div>
+ "e0c434f500e6c131f4a2834f987fc46406115de2018ebbb0d5a3c261bd97581cc"</div>
<div>
+ "fef76afc7135a6d59e8855ecd7eacc8f8737e794c60a761c536b72b11fac8e603"</div>
<div>
+ "f5da1a2d54aa103b8a13c0dbc10203010001300d06092a864886f70d010104050"</div>
<div>
+ "0038181005ee9be8bcbb250648d3b741290a82a1c9dc2e76a0af2f2228f1d9f9c"</div>
<div>
+ "4007529c446a70175c5a900d5141812866db46be6559e2141616483998211f4a6"</div>
<div>
+ "73149fb2232a10d247663b26a9031e15f84bc1c74d141ff98a02d76f85b2c8ab2"</div>
<div>
+ "571b6469b232d8e768a7f7ca04f7abe4a775615916c07940656b58717457b42bd"</div>
<div>
+ "928a2";</div>
<div>
<br /></div>
<div>
}</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<b>FacebookError.class</b></div>
<div>
<div>
<br /></div>
<div>
package com.facebook.android;</div>
<div>
<br /></div>
<div>
public class FacebookError extends Throwable {</div>
<div>
<br /></div>
<div>
private static final long serialVersionUID = 1L;</div>
<div>
<br /></div>
<div>
private int mErrorCode = 0;</div>
<div>
private String mErrorType;</div>
<div>
<br /></div>
<div>
public FacebookError(String message) {</div>
<div>
super(message);</div>
<div>
}</div>
<div>
<br /></div>
<div>
public FacebookError(String message, String type, int code) {</div>
<div>
super(message);</div>
<div>
mErrorType = type;</div>
<div>
mErrorCode = code;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public int getErrorCode() {</div>
<div>
return mErrorCode;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public String getErrorType() {</div>
<div>
return mErrorType;</div>
<div>
}</div>
<div>
<br /></div>
<div>
}</div>
<div style="font-weight: bold;">
<br /></div>
</div>
<div>
<b>FbDialog.class</b></div>
</div>
<div>
<b><br /></b></div>
<div>
<div>
package com.facebook.android;</div>
<div>
<br /></div>
<div>
import android.app.Dialog;</div>
<div>
import android.app.ProgressDialog;</div>
<div>
import android.content.Context;</div>
<div>
import android.content.Intent;</div>
<div>
import android.graphics.Bitmap;</div>
<div>
import android.graphics.Color;</div>
<div>
import android.graphics.Typeface;</div>
<div>
import android.graphics.drawable.Drawable;</div>
<div>
import android.net.Uri;</div>
<div>
import android.os.Bundle;</div>
<div>
import android.util.Log;</div>
<div>
import android.view.Display;</div>
<div>
import android.view.ViewGroup;</div>
<div>
import android.view.Window;</div>
<div>
import android.webkit.WebView;</div>
<div>
import android.webkit.WebViewClient;</div>
<div>
import android.widget.FrameLayout;</div>
<div>
import android.widget.LinearLayout;</div>
<div>
import android.widget.TextView;</div>
<div>
import com.kmiller.facebookintegration.R;</div>
<div>
import com.facebook.android.Facebook.DialogListener;</div>
<div>
<br /></div>
<div>
public class FbDialog extends Dialog {</div>
<div>
<br /></div>
<div>
static final int FB_BLUE = 0xFF6D84B4;</div>
<div>
static final float[] DIMENSIONS_LANDSCAPE = {460, 260};</div>
<div>
static final float[] DIMENSIONS_PORTRAIT = {280, 420};</div>
<div>
static final FrameLayout.LayoutParams FILL =</div>
<div>
new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,</div>
<div>
ViewGroup.LayoutParams.FILL_PARENT);</div>
<div>
static final int MARGIN = 4;</div>
<div>
static final int PADDING = 2;</div>
<div>
static final String DISPLAY_STRING = "touch";</div>
<div>
static final String FB_ICON = "icon.png";</div>
<div>
<br /></div>
<div>
private String mUrl;</div>
<div>
private DialogListener mListener;</div>
<div>
private ProgressDialog mSpinner;</div>
<div>
private WebView mWebView;</div>
<div>
private LinearLayout mContent;</div>
<div>
private TextView mTitle;</div>
<div>
<br /></div>
<div>
public FbDialog(Context context, String url, DialogListener listener) {</div>
<div>
super(context);</div>
<div>
mUrl = url;</div>
<div>
mListener = listener;</div>
<div>
}</div>
<div>
<br /></div>
<div>
@Override</div>
<div>
protected void onCreate(Bundle savedInstanceState) {</div>
<div>
super.onCreate(savedInstanceState);</div>
<div>
mSpinner = new ProgressDialog(getContext());</div>
<div>
mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);</div>
<div>
mSpinner.setMessage("Loading...");</div>
<div>
<br /></div>
<div>
mContent = new LinearLayout(getContext());</div>
<div>
mContent.setOrientation(LinearLayout.VERTICAL);</div>
<div>
setUpTitle();</div>
<div>
setUpWebView();</div>
<div>
Display display = getWindow().getWindowManager().getDefaultDisplay();</div>
<div>
final float scale = getContext().getResources().getDisplayMetrics().density;</div>
<div>
float[] dimensions =</div>
<div>
(display.getWidth() < display.getHeight())</div>
<div>
? DIMENSIONS_PORTRAIT : DIMENSIONS_LANDSCAPE;</div>
<div>
addContentView(mContent, new FrameLayout.LayoutParams(</div>
<div>
(int) (dimensions[0] * scale + 0.5f),</div>
<div>
(int) (dimensions[1] * scale + 0.5f)));</div>
<div>
}</div>
<div>
<br /></div>
<div>
private void setUpTitle() {</div>
<div>
requestWindowFeature(Window.FEATURE_NO_TITLE);</div>
<div>
Drawable icon = getContext().getResources().getDrawable(</div>
<div>
R.drawable.facebook_icon);</div>
<div>
mTitle = new TextView(getContext());</div>
<div>
mTitle.setText("Facebook");</div>
<div>
mTitle.setTextColor(Color.WHITE);</div>
<div>
mTitle.setTypeface(Typeface.DEFAULT_BOLD);</div>
<div>
mTitle.setBackgroundColor(FB_BLUE);</div>
<div>
mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);</div>
<div>
mTitle.setCompoundDrawablePadding(MARGIN + PADDING);</div>
<div>
mTitle.setCompoundDrawablesWithIntrinsicBounds(</div>
<div>
icon, null, null, null);</div>
<div>
mContent.addView(mTitle);</div>
<div>
}</div>
<div>
<br /></div>
<div>
private void setUpWebView() {</div>
<div>
mWebView = new WebView(getContext());</div>
<div>
mWebView.setVerticalScrollBarEnabled(false);</div>
<div>
mWebView.setHorizontalScrollBarEnabled(false);</div>
<div>
mWebView.setWebViewClient(new FbDialog.FbWebViewClient());</div>
<div>
mWebView.getSettings().setJavaScriptEnabled(true);</div>
<div>
mWebView.loadUrl(mUrl);</div>
<div>
mWebView.setLayoutParams(FILL);</div>
<div>
mContent.addView(mWebView);</div>
<div>
}</div>
<div>
<br /></div>
<div>
private class FbWebViewClient extends WebViewClient {</div>
<div>
<br /></div>
<div>
@Override</div>
<div>
public boolean shouldOverrideUrlLoading(WebView view, String url) {</div>
<div>
Log.d("Facebook-WebView", "Redirect URL: " + url);</div>
<div>
if (url.startsWith(Facebook.REDIRECT_URI)) {</div>
<div>
Bundle values = Util.parseUrl(url);</div>
<div>
<br /></div>
<div>
String error = values.getString("error");</div>
<div>
if (error == null) {</div>
<div>
error = values.getString("error_type");</div>
<div>
}</div>
<div>
<br /></div>
<div>
if (error == null) {</div>
<div>
mListener.onComplete(values);</div>
<div>
} else if (error.equals("access_denied") ||</div>
<div>
error.equals("OAuthAccessDeniedException")) {</div>
<div>
mListener.onCancel();</div>
<div>
} else {</div>
<div>
mListener.onFacebookError(new FacebookError(error));</div>
<div>
}</div>
<div>
<br /></div>
<div>
FbDialog.this.dismiss();</div>
<div>
return true;</div>
<div>
} else if (url.startsWith(Facebook.CANCEL_URI)) {</div>
<div>
mListener.onCancel();</div>
<div>
FbDialog.this.dismiss();</div>
<div>
return true;</div>
<div>
} else if (url.contains(DISPLAY_STRING)) {</div>
<div>
return false;</div>
<div>
}</div>
<div>
</div>
<div>
getContext().startActivity(</div>
<div>
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));</div>
<div>
return true;</div>
<div>
}</div>
<div>
<br /></div>
<div>
@Override</div>
<div>
public void onReceivedError(WebView view, int errorCode,</div>
<div>
String description, String failingUrl) {</div>
<div>
super.onReceivedError(view, errorCode, description, failingUrl);</div>
<div>
mListener.onError(</div>
<div>
new DialogError(description, errorCode, failingUrl));</div>
<div>
FbDialog.this.dismiss();</div>
<div>
}</div>
<div>
<br /></div>
<div>
@Override</div>
<div>
public void onPageStarted(WebView view, String url, Bitmap favicon) {</div>
<div>
Log.d("Facebook-WebView", "Webview loading URL: " + url);</div>
<div>
super.onPageStarted(view, url, favicon);</div>
<div>
mSpinner.show();</div>
<div>
}</div>
<div>
<br /></div>
<div>
@Override</div>
<div>
public void onPageFinished(WebView view, String url) {</div>
<div>
super.onPageFinished(view, url);</div>
<div>
String title = mWebView.getTitle();</div>
<div>
if (title != null && title.length() > 0) {</div>
<div>
mTitle.setText(title);</div>
<div>
}</div>
<div>
mSpinner.dismiss();</div>
<div>
}</div>
<div>
<br /></div>
<div>
}</div>
<div>
}</div>
<div style="font-weight: bold;">
<br /></div>
</div>
<div>
<b>Util.class</b></div>
<div>
<b><br /></b></div>
<div>
<div>
package com.facebook.android;</div>
<div>
<br /></div>
<div>
import java.io.BufferedOutputStream;</div>
<div>
import java.io.BufferedReader;</div>
<div>
import java.io.FileNotFoundException;</div>
<div>
import java.io.IOException;</div>
<div>
import java.io.InputStream;</div>
<div>
import java.io.InputStreamReader;</div>
<div>
import java.io.OutputStream;</div>
<div>
import java.net.HttpURLConnection;</div>
<div>
import java.net.MalformedURLException;</div>
<div>
import java.net.URL;</div>
<div>
import java.net.URLDecoder;</div>
<div>
import java.net.URLEncoder;</div>
<div>
import org.json.JSONException;</div>
<div>
import org.json.JSONObject;</div>
<div>
import android.app.AlertDialog.Builder;</div>
<div>
import android.content.Context;</div>
<div>
import android.os.Bundle;</div>
<div>
import android.util.Log;</div>
<div>
import android.webkit.CookieManager;</div>
<div>
import android.webkit.CookieSyncManager;</div>
<div>
<br /></div>
<div>
public final class Util {</div>
<div>
<br /></div>
<div>
public static String encodePostBody(Bundle parameters, String boundary) {</div>
<div>
if (parameters == null) return "";</div>
<div>
StringBuilder sb = new StringBuilder();</div>
<div>
<br /></div>
<div>
for (String key : parameters.keySet()) {</div>
<div>
if (parameters.getByteArray(key) != null) {</div>
<div>
continue;</div>
<div>
}</div>
<div>
<br /></div>
<div>
sb.append("Content-Disposition: form-data; name=\"" + key +</div>
<div>
"\"\r\n\r\n" + parameters.getString(key));</div>
<div>
sb.append("\r\n" + "--" + boundary + "\r\n");</div>
<div>
}</div>
<div>
<br /></div>
<div>
return sb.toString();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static String encodeUrl(Bundle parameters) {</div>
<div>
if (parameters == null) {</div>
<div>
return "";</div>
<div>
}</div>
<div>
<br /></div>
<div>
StringBuilder sb = new StringBuilder();</div>
<div>
boolean first = true;</div>
<div>
for (String key : parameters.keySet()) {</div>
<div>
if (first) first = false; else sb.append("&");</div>
<div>
sb.append(URLEncoder.encode(key) + "=" +</div>
<div>
URLEncoder.encode(parameters.getString(key)));</div>
<div>
}</div>
<div>
return sb.toString();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static Bundle decodeUrl(String s) {</div>
<div>
Bundle params = new Bundle();</div>
<div>
if (s != null) {</div>
<div>
String array[] = s.split("&");</div>
<div>
for (String parameter : array) {</div>
<div>
String v[] = parameter.split("=");</div>
<div>
params.putString(URLDecoder.decode(v[0]),</div>
<div>
URLDecoder.decode(v[1]));</div>
<div>
}</div>
<div>
}</div>
<div>
return params;</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static Bundle parseUrl(String url) {</div>
<div>
// hack to prevent MalformedURLException</div>
<div>
url = url.replace("fbconnect", "http");</div>
<div>
try {</div>
<div>
URL u = new URL(url);</div>
<div>
Bundle b = decodeUrl(u.getQuery());</div>
<div>
b.putAll(decodeUrl(u.getRef()));</div>
<div>
return b;</div>
<div>
} catch (MalformedURLException e) {</div>
<div>
return new Bundle();</div>
<div>
}</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static String openUrl(String url, String method, Bundle params)</div>
<div>
throws MalformedURLException, IOException {</div>
<div>
String strBoundary = "3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";</div>
<div>
String endLine = "\r\n";</div>
<div>
<br /></div>
<div>
OutputStream os;</div>
<div>
<br /></div>
<div>
if (method.equals("GET")) {</div>
<div>
url = url + "?" + encodeUrl(params);</div>
<div>
}</div>
<div>
Log.d("Facebook-Util", method + " URL: " + url);</div>
<div>
HttpURLConnection conn =</div>
<div>
(HttpURLConnection) new URL(url).openConnection();</div>
<div>
conn.setRequestProperty("User-Agent", System.getProperties().</div>
<div>
getProperty("http.agent") + " FacebookAndroidSDK");</div>
<div>
if (!method.equals("GET")) {</div>
<div>
Bundle dataparams = new Bundle();</div>
<div>
for (String key : params.keySet()) {</div>
<div>
if (params.getByteArray(key) != null) {</div>
<div>
dataparams.putByteArray(key, params.getByteArray(key));</div>
<div>
}</div>
<div>
}</div>
<div>
if (!params.containsKey("method")) {</div>
<div>
params.putString("method", method);</div>
<div>
}</div>
<div>
<br /></div>
<div>
if (params.containsKey("access_token")) {</div>
<div>
String decoded_token =</div>
<div>
URLDecoder.decode(params.getString("access_token"));</div>
<div>
params.putString("access_token", decoded_token);</div>
<div>
}</div>
<div>
<br /></div>
<div>
conn.setRequestMethod("POST");</div>
<div>
conn.setRequestProperty(</div>
<div>
"Content-Type",</div>
<div>
"multipart/form-data;boundary="+strBoundary);</div>
<div>
conn.setDoOutput(true);</div>
<div>
conn.setDoInput(true);</div>
<div>
conn.setRequestProperty("Connection", "Keep-Alive");</div>
<div>
conn.connect();</div>
<div>
os = new BufferedOutputStream(conn.getOutputStream());</div>
<div>
<br /></div>
<div>
os.write(("--" + strBoundary +endLine).getBytes());</div>
<div>
os.write((encodePostBody(params, strBoundary)).getBytes());</div>
<div>
os.write((endLine + "--" + strBoundary + endLine).getBytes());</div>
<div>
<br /></div>
<div>
if (!dataparams.isEmpty()) {</div>
<div>
<br /></div>
<div>
for (String key: dataparams.keySet()){</div>
<div>
os.write(("Content-Disposition: form-data; filename=\"" + key + "\"" + endLine).getBytes());</div>
<div>
os.write(("Content-Type: content/unknown" + endLine + endLine).getBytes());</div>
<div>
os.write(dataparams.getByteArray(key));</div>
<div>
os.write((endLine + "--" + strBoundary + endLine).getBytes());</div>
<div>
<br /></div>
<div>
}</div>
<div>
}</div>
<div>
os.flush();</div>
<div>
}</div>
<div>
<br /></div>
<div>
String response = "";</div>
<div>
try {</div>
<div>
response = read(conn.getInputStream());</div>
<div>
} catch (FileNotFoundException e) {</div>
<div>
response = read(conn.getErrorStream());</div>
<div>
}</div>
<div>
return response;</div>
<div>
}</div>
<div>
<br /></div>
<div>
private static String read(InputStream in) throws IOException {</div>
<div>
StringBuilder sb = new StringBuilder();</div>
<div>
BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);</div>
<div>
for (String line = r.readLine(); line != null; line = r.readLine()) {</div>
<div>
sb.append(line);</div>
<div>
}</div>
<div>
in.close();</div>
<div>
return sb.toString();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static void clearCookies(Context context) {</div>
<div>
</div>
<div>
@SuppressWarnings("unused")</div>
<div>
CookieSyncManager cookieSyncMngr =</div>
<div>
CookieSyncManager.createInstance(context);</div>
<div>
CookieManager cookieManager = CookieManager.getInstance();</div>
<div>
cookieManager.removeAllCookie();</div>
<div>
}</div>
<div>
<br /></div>
<div>
public static JSONObject parseJson(String response)</div>
<div>
throws JSONException, FacebookError {</div>
<div>
// Edge case: when sending a POST request to /[post_id]/likes</div>
<div>
// the return value is 'true' or 'false'. Unfortunately</div>
<div>
// these values cause the JSONObject constructor to throw</div>
<div>
// an exception.</div>
<div>
if (response.equals("false")) {</div>
<div>
throw new FacebookError("request failed");</div>
<div>
}</div>
<div>
if (response.equals("true")) {</div>
<div>
response = "{value : true}";</div>
<div>
}</div>
<div>
JSONObject json = new JSONObject(response);</div>
<div>
<br /></div>
<div>
if (json.has("error")) {</div>
<div>
JSONObject error = json.getJSONObject("error");</div>
<div>
throw new FacebookError(</div>
<div>
error.getString("message"), error.getString("type"), 0);</div>
<div>
}</div>
<div>
if (json.has("error_code") && json.has("error_msg")) {</div>
<div>
throw new FacebookError(json.getString("error_msg"), "",</div>
<div>
Integer.parseInt(json.getString("error_code")));</div>
<div>
}</div>
<div>
if (json.has("error_code")) {</div>
<div>
throw new FacebookError("request failed", "",</div>
<div>
Integer.parseInt(json.getString("error_code")));</div>
<div>
}</div>
<div>
if (json.has("error_msg")) {</div>
<div>
throw new FacebookError(json.getString("error_msg"));</div>
<div>
}</div>
<div>
if (json.has("error_reason")) {</div>
<div>
throw new FacebookError(json.getString("error_reason"));</div>
<div>
}</div>
<div>
return json;</div>
<div>
}</div>
<div>
public static void showAlert(Context context, String title, String text) {</div>
<div>
Builder alertBuilder = new Builder(context);</div>
<div>
alertBuilder.setTitle(title);</div>
<div>
alertBuilder.setMessage(text);</div>
<div>
alertBuilder.create().show();</div>
<div>
}</div>
<div>
<br /></div>
<div>
}</div>
<div style="font-weight: bold;">
<br /></div>
</div>
<div style="font-weight: bold;">
main.xml</div>
<div style="font-weight: bold;">
<br /></div>
<div>
<div>
<?xml version="1.0" encoding="utf-8"?></div>
<div>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</div>
<div>
android:orientation="vertical"</div>
<div>
android:layout_width="fill_parent"</div>
<div>
android:layout_height="fill_parent"</div>
<div>
android:padding="20dip"</div>
<div>
android:gravity="center_horizontal" ></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><TextView</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_width="wrap_content"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_height="wrap_content"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:text="Facebook Integration Demo"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:textStyle="bold"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:textSize="24dip"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:textColor="#ffffff"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_marginBottom="20dip"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:gravity="center" ></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></TextView></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><Button android:id="@+id/LoginButton"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_width="wrap_content"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_height="wrap_content"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:text="Login to Facebook" ></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></Button></div>
<div>
</LinearLayout></div>
<div>
<br /></div>
<div>
<b>AndroidManifest.xml</b></div>
<div>
<b><br /></b></div>
<div>
<div>
<?xml version="1.0" encoding="utf-8"?></div>
<div>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"</div>
<div>
package="com.kmiller.facebookintegration"</div>
<div>
android:versionCode="1"</div>
<div>
android:versionName="1.0"></div>
<div>
<uses-sdk android:minSdkVersion="8" /></div>
<div>
<br /></div>
<div>
<application android:icon="@drawable/icon" android:label="@string/app_name"></div>
<div>
<activity android:name=".Login"</div>
<div>
android:label="@string/app_name"></div>
<div>
<intent-filter></div>
<div>
<action android:name="android.intent.action.MAIN" /></div>
<div>
<category android:name="android.intent.category.LAUNCHER" /></div>
<div>
</intent-filter></div>
<div>
</activity></div>
<div>
</application></div>
<div>
<uses-permission android:name="android.permission.INTERNET" /></div>
<div>
</manifest></div>
</div>
<div style="font-weight: bold;">
<br /></div>
</div>
</div>Bharathihttp://www.blogger.com/profile/03288932038970432575noreply@blogger.com1tag:blogger.com,1999:blog-6088758447212359624.post-52500620622066885422012-05-30T00:30:00.002-07:002012-06-07T03:09:59.329-07:00Twitter Integration in Android<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
</div>
<div style="text-align: left;">
First you have to register your application in <a href="https://dev.twitter.com/">https://dev.twitter.com/</a> and have to get consumer key and secret key.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
In order to integrate twitter in your application you need four jar files.</div>
<div style="text-align: left;">
1. signpost-commonshttp4-1.2.1.1.jar</div>
<div style="text-align: left;">
2. signpost-core-1.2.1.1.jar</div>
<div style="text-align: left;">
3. signpost-jetty6-1.2.1.1.jar</div>
<div style="text-align: left;">
4. twitter4j-core-android-2.2.5.jar</div>
<div style="text-align: left;">
</div>
<div style="text-align: left;">
</div>
<ul style="text-align: left;">
<li>First Download these four jar files</li>
<li>Right Click on your project. Select "Build Path". From that select "Configure Build Path".</li>
<li>Then in Libraries Tab select "Add External Jars" then select the path where you downloaded jar files.Add all the four jar files and click ok.</li>
</ul>
<br />
<div style="text-align: left;">
</div>
<div style="text-align: left;">
<b>TwitterActivity.class</b></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
package com.twitter;</div>
<div style="text-align: left;">
import com.twitter.TwitterApp.TwDialogListener;</div>
<div style="text-align: left;">
import android.widget.CheckBox;</div>
<div style="text-align: left;">
import android.widget.Toast;</div>
<div style="text-align: left;">
import android.app.Activity;</div>
<div style="text-align: left;">
import android.view.View;</div>
<div style="text-align: left;">
import android.view.View.OnClickListener;</div>
<div style="text-align: left;">
import android.os.Bundle;</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
public class TwitterActivity extends Activity {</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>private TwitterApp mTwitter;</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>private CheckBox mTwitterBtn;</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static final String twitter_consumer_key = "Ur Consumer Key";</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static final String twitter_secret_key = "Ur Secret Key";</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onCreate(Bundle savedInstanceState) {</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>super.onCreate(savedInstanceState);</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>setContentView(R.layout.main);</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitterBtn<span class="Apple-tab-span" style="white-space: pre;"> </span>= (CheckBox) findViewById(R.id.twitterCheck);</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitterBtn.setOnClickListener(new OnClickListener() {</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onClick(View v) {</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitterBtn.setChecked(false);</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitter.authorize();</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>});</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitter <span class="Apple-tab-span" style="white-space: pre;"> </span>= new TwitterApp(this, twitter_consumer_key,twitter_secret_key);</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitter.setListener(mTwLoginDialogListener);</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (mTwitter.hasAccessToken()) {</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitterBtn.setVisibility(View.GONE);</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText(TwitterActivity.this, "Connected to Twitter" , Toast.LENGTH_LONG).show();</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onComplete(String value) {</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText(TwitterActivity.this, "Connected to Twitter" , Toast.LENGTH_LONG).show();</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onError(String value) {</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitterBtn.setChecked(false);</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Toast.makeText(TwitterActivity.this, "Twitter connection failed", Toast.LENGTH_LONG).show();</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div style="text-align: left;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>};</div>
<div style="text-align: left;">
}</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Now you have to create three more classes.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
</div>
<b>TwitterApp .class</b><br />
<br />
package com.twitter;<br />
<br />
import java.net.MalformedURLException;<br />
import java.net.URLDecoder;<br />
<br />
import oauth.signpost.OAuthProvider;<br />
import oauth.signpost.basic.DefaultOAuthProvider;<br />
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;<br />
<br />
import twitter4j.Twitter;<br />
import twitter4j.TwitterException;<br />
import twitter4j.TwitterFactory;<br />
import twitter4j.User;<br />
import twitter4j.auth.AccessToken;<br />
<br />
import android.os.Handler;<br />
import android.os.Message;<br />
<br />
import android.app.ProgressDialog;<br />
import android.content.Context;<br />
import android.util.Log;<br />
import android.view.Window;<br />
<br />
import java.net.URL;<br />
<br />
public class TwitterApp {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private Twitter mTwitter;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private TwitterSession mSession;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private AccessToken mAccessToken;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private CommonsHttpOAuthConsumer mHttpOauthConsumer;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private OAuthProvider mHttpOauthprovider;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private String mConsumerKey;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private String mSecretKey;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private ProgressDialog mProgressDlg;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private TwDialogListener mListener;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private Context context;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private boolean mInit = true;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public static final String CALLBACK_URL = "twitterapp://connect";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static final String TAG = "TwitterApp";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public TwitterApp(Context context, String consumerKey, String secretKey) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>this.context<span class="Apple-tab-span" style="white-space: pre;"> </span>= context;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitter <span class="Apple-tab-span" style="white-space: pre;"> </span>= new TwitterFactory().getInstance();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mSession<span class="Apple-tab-span" style="white-space: pre;"> </span>= new TwitterSession(context);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mProgressDlg<span class="Apple-tab-span" style="white-space: pre;"> </span>= new ProgressDialog(context);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mProgressDlg.requestWindowFeature(Window.FEATURE_NO_TITLE);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mConsumerKey <span class="Apple-tab-span" style="white-space: pre;"> </span>= consumerKey;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mSecretKey<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span>= secretKey;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey, mSecretKey);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mHttpOauthprovider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> "http://twitter.com/oauth/access_token",<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> "http://twitter.com/oauth/authorize");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mAccessToken<span class="Apple-tab-span" style="white-space: pre;"> </span>= mSession.getAccessToken();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>configureToken();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void setListener(TwDialogListener listener) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mListener = listener;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@SuppressWarnings("deprecation")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private void configureToken() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (mAccessToken != null) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (mInit) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitter.setOAuthConsumer(mConsumerKey, mSecretKey);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mInit = false;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitter.setOAuthAccessToken(mAccessToken);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public boolean hasAccessToken() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return (mAccessToken == null) ? false : true;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void resetAccessToken() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (mAccessToken != null) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mSession.resetAccessToken();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mAccessToken = null;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public String getUsername() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return mSession.getUsername();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void updateStatus(String status) throws Exception {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mTwitter.updateStatus(status);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (TwitterException e) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>throw e;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void authorize() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mProgressDlg.setMessage("Initializing ...");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mProgressDlg.show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>new Thread() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void run() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String authUrl = "";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>int what = 1;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL);<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>what = 0;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Log.d(TAG, "Request token url " + authUrl);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Log.d(TAG, "Failed to get request token");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mHandler.sendMessage(mHandler.obtainMessage(what, 1, 0, authUrl));<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}.start();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void processToken(String callbackUrl) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mProgressDlg.setMessage("Finalizing ...");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mProgressDlg.show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>final String verifier = getVerifier(callbackUrl);<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>new Thread() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void run() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>int what = 1;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret());<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>configureToken();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>User user = mTwitter.verifyCredentials();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> mSession.storeAccessToken(mAccessToken, user.getName());<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> what = 0;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (Exception e){<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Log.d(TAG, "Error getting access token");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}.start();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private String getVerifier(String callbackUrl) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String verifier<span class="Apple-tab-span" style="white-space: pre;"> </span> = "";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>callbackUrl = callbackUrl.replace("twitterapp", "http");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>URL url <span class="Apple-tab-span" style="white-space: pre;"> </span>= new URL(callbackUrl);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String query <span class="Apple-tab-span" style="white-space: pre;"> </span>= url.getQuery();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String array[]<span class="Apple-tab-span" style="white-space: pre;"> </span>= query.split("&");<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>for (String parameter : array) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> String v[] = parameter.split("=");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> verifier = URLDecoder.decode(v[1]);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> <span class="Apple-tab-span" style="white-space: pre;"> </span> break;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> }<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> }<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>} catch (MalformedURLException e) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>e.printStackTrace();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return verifier;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private void showLoginDialog(String url) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>final TwDialogListener listener = new TwDialogListener() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onComplete(String value) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>processToken(value);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onError(String value) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mListener.onError("Failed opening authorization page");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>};<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>new TwitterDialog(context, url, listener).show();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>private Handler mHandler = new Handler() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>@Override<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void handleMessage(Message msg) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mProgressDlg.dismiss();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (msg.what == 1) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (msg.arg1 == 1)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mListener.onError("Error getting request token");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mListener.onError("Error getting access token");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>} else {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (msg.arg1 == 1)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>showLoginDialog((String) msg.obj);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>else<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mListener.onComplete("");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>};<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public interface TwDialogListener {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onComplete(String value);<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void onError(String value);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
}<br />
<br />
<b>TwitterDialog.class</b><br />
<br />
<br />
package com.twitter;<br />
<br />
import com.twitter.TwitterApp.TwDialogListener;<br />
import android.app.Dialog;<br />
import android.app.ProgressDialog;<br />
import android.graphics.Bitmap;<br />
import android.graphics.Color;<br />
import android.graphics.Typeface;<br />
import android.os.Bundle;<br />
import android.util.Log;<br />
import android.content.Context;<br />
import android.view.Display;<br />
import android.view.ViewGroup;<br />
import android.view.Window;<br />
import android.webkit.WebView;<br />
import android.webkit.WebViewClient;<br />
import android.widget.FrameLayout;<br />
import android.widget.LinearLayout;<br />
import android.widget.TextView;<br />
import android.webkit.CookieManager;<br />
import android.webkit.CookieSyncManager;<br />
<br />
public class TwitterDialog extends Dialog {<br />
static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>ViewGroup.LayoutParams.FILL_PARENT);<br />
static final int MARGIN = 4;<br />
static final int PADDING = 2;<br />
<br />
private String mUrl;<br />
private TwDialogListener mListener;<br />
private ProgressDialog mSpinner;<br />
private WebView mWebView;<br />
private LinearLayout mContent;<br />
private TextView mTitle;<br />
<br />
private static final String TAG = "Twitter-WebView";<br />
<br />
public TwitterDialog(Context context, String url, TwDialogListener listener) {<br />
super(context);<br />
<br />
mUrl <span class="Apple-tab-span" style="white-space: pre;"> </span>= url;<br />
mListener <span class="Apple-tab-span" style="white-space: pre;"> </span>= listener;<br />
}<br />
<br />
@Override<br />
protected void onCreate(Bundle savedInstanceState) {<br />
super.onCreate(savedInstanceState);<br />
<br />
mSpinner = new ProgressDialog(getContext());<br />
<br />
mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);<br />
mSpinner.setMessage("Loading...");<br />
<br />
mContent = new LinearLayout(getContext());<br />
<br />
mContent.setOrientation(LinearLayout.VERTICAL);<br />
<br />
setUpTitle();<br />
setUpWebView();<br />
<br />
Display display = getWindow().getWindowManager().getDefaultDisplay();<br />
double[] dimensions = new double[2];<br />
<br />
if (display.getWidth() < display.getHeight()) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>dimensions[0]<span class="Apple-tab-span" style="white-space: pre;"> </span>= 0.87 * display.getWidth();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>dimensions[1]<span class="Apple-tab-span" style="white-space: pre;"> </span>= 0.82 * display.getHeight();<br />
} else {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>dimensions[0]<span class="Apple-tab-span" style="white-space: pre;"> </span>= 0.75 * display.getWidth();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>dimensions[1]<span class="Apple-tab-span" style="white-space: pre;"> </span>= 0.75 * display.getHeight(); <br />
}<br />
<br />
addContentView(mContent, new FrameLayout.LayoutParams((int) dimensions[0], (int) dimensions[1]));<br />
}<br />
<br />
private void setUpTitle() {<br />
requestWindowFeature(Window.FEATURE_NO_TITLE);<br />
<br />
mTitle = new TextView(getContext());<br />
<br />
mTitle.setText("Twitter");<br />
mTitle.setTextColor(Color.WHITE);<br />
mTitle.setTypeface(Typeface.DEFAULT_BOLD);<br />
mTitle.setBackgroundColor(0xFFbbd7e9);<br />
mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);<br />
mTitle.setCompoundDrawablePadding(MARGIN + PADDING); <br />
mContent.addView(mTitle);<br />
}<br />
<br />
private void setUpWebView() {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>CookieSyncManager.createInstance(getContext());<br />
CookieManager cookieManager = CookieManager.getInstance();<br />
cookieManager.removeAllCookie();<br />
<br />
mWebView = new WebView(getContext());<br />
<br />
mWebView.setVerticalScrollBarEnabled(false);<br />
mWebView.setHorizontalScrollBarEnabled(false);<br />
mWebView.setWebViewClient(new TwitterWebViewClient());<br />
mWebView.getSettings().setJavaScriptEnabled(true);<br />
mWebView.loadUrl(mUrl);<br />
mWebView.setLayoutParams(FILL);<br />
<br />
mContent.addView(mWebView);<br />
}<br />
<br />
private class TwitterWebViewClient extends WebViewClient {<br />
<br />
@Override<br />
public boolean shouldOverrideUrlLoading(WebView view, String url) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Log.d(TAG, "Redirecting URL " + url);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (url.startsWith(TwitterApp.CALLBACK_URL)) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>mListener.onComplete(url);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>TwitterDialog.this.dismiss();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return true;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>} else if (url.startsWith("authorize")) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>return false;<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
return true;<br />
}<br />
<br />
@Override<br />
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Log.d(TAG, "Page error: " + description);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
super.onReceivedError(view, errorCode, description, failingUrl);<br />
<br />
mListener.onError(description);<br />
<br />
TwitterDialog.this.dismiss();<br />
}<br />
<br />
@Override<br />
public void onPageStarted(WebView view, String url, Bitmap favicon) {<br />
Log.d(TAG, "Loading URL: " + url);<br />
super.onPageStarted(view, url, favicon);<br />
mSpinner.show();<br />
}<br />
<br />
@Override<br />
public void onPageFinished(WebView view, String url) {<br />
super.onPageFinished(view, url);<br />
String title = mWebView.getTitle();<br />
if (title != null && title.length() > 0) {<br />
mTitle.setText(title);<br />
}<br />
mSpinner.dismiss();<br />
}<br />
<br />
}<br />
}<br />
<div>
<br /></div>
<div>
<br /></div>
<div>
<b>TwitterSession.class</b></div>
<div>
<div>
<br /></div>
<div>
package com.twitter;</div>
<div>
<br /></div>
<div>
import twitter4j.auth.AccessToken;</div>
<div>
import android.content.SharedPreferences;</div>
<div>
import android.content.SharedPreferences.Editor;</div>
<div>
import android.content.Context;</div>
<div>
<br /></div>
<div>
public class TwitterSession {</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>private SharedPreferences sharedPref;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>private Editor editor;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static final String TWEET_AUTH_KEY = "auth_key";</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static final String TWEET_AUTH_SECRET_KEY = "auth_secret_key";</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static final String TWEET_USER_NAME = "user_name";</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>private static final String SHARED = "Twitter_Preferences";</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public TwitterSession(Context context) {</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>sharedPref <span class="Apple-tab-span" style="white-space: pre;"> </span> = context.getSharedPreferences(SHARED, Context.MODE_PRIVATE);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor <span class="Apple-tab-span" style="white-space: pre;"> </span> = sharedPref.edit();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void storeAccessToken(AccessToken accessToken, String username) {</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor.putString(TWEET_AUTH_KEY, accessToken.getToken());</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor.putString(TWEET_AUTH_SECRET_KEY, accessToken.getTokenSecret());</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor.putString(TWEET_USER_NAME, username);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor.commit();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public void resetAccessToken() {</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor.putString(TWEET_AUTH_KEY, null);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor.putString(TWEET_AUTH_SECRET_KEY, null);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor.putString(TWEET_USER_NAME, null);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>editor.commit();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public String getUsername() {</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>return sharedPref.getString(TWEET_USER_NAME, "");</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>public AccessToken getAccessToken() {</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>String token <span class="Apple-tab-span" style="white-space: pre;"> </span>= sharedPref.getString(TWEET_AUTH_KEY, null);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>String tokenSecret <span class="Apple-tab-span" style="white-space: pre;"> </span>= sharedPref.getString(TWEET_AUTH_SECRET_KEY, null);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (token != null && tokenSecret != null) </div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>return new AccessToken(token, tokenSecret);</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>else</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>return null;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
}</div>
</div>
<div>
<br /></div>
<div>
<b>main.xml</b></div>
<div>
<b><br /></b></div>
<div>
<div>
<?xml version="1.0" encoding="UTF-8"?></div>
<div>
<LinearLayout</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>xmlns:android="http://schemas.android.com/apk/res/android"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:orientation="vertical"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_width="fill_parent"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_height="fill_parent"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:padding="10dp"></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><CheckBox</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:id="@+id/twitterCheck"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_width="wrap_content"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_height="wrap_content"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_marginTop="10dp"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:layout_marginLeft="5dp"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:text=" Twitter (Not connected) "</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:textSize="14sp"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:textStyle="bold"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:textColor="#ccc"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:clickable="true"</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>android:focusable="true" /></div>
<div>
</LinearLayout></div>
</div>
<div>
<br /></div>
<div>
<b>AndroidManifest.xml</b></div>
<div>
<b><br /></b></div>
<div>
add</div>
<div>
<uses-permission android:name="android.permission.INTERNET"/></div>
<br />
<br />
<div style="text-align: left;">
</div>
</div>Bharathihttp://www.blogger.com/profile/03288932038970432575noreply@blogger.com2