Skip to main content

Twitter feed (Twitter user posts)

Hi,

Today I am explaning the example of twitter which will give you user posts in twitter and all other things related to post.For that first we need to use three classes which i am explaning below:





MainActivity.java



import java.io.IOException;

import java.net.URL;



import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;



import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;



import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.widget.Toast;



public class MainActivity extends Activity {



MyXMLHandler myXMLHandler;



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

new LongOperation().execute();

}




private class LongOperation extends AsyncTask<String, Void, String> {



@Override

protected String doInBackground(String... params) {




try

{

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

XMLReader xr = sp.getXMLReader();


/** Create handler to handle XML Tags ( extends DefaultHandler ) */

myXMLHandler = new MyXMLHandler();

xr.setContentHandler(myXMLHandler);



URL _url = new URL("http://www.twfeed.com/feed/hmittal90");

// ByteArrayInputStream is = new ByteArrayInputStream(new InputSource(_url.openStream()));

xr.parse(new InputSource(_url.openStream()));


} catch (ParserConfigurationException pce) {

Log.e("SAX XML", "sax parse error", pce);

} catch (SAXException se) {

Log.e("SAX XML", "sax error", se);

} catch (IOException e) {

e.printStackTrace();

}

return "Executed";

}



@Override

protected void onPostExecute(String result) {

// txt.setText(result);

//might want to change "executed" for the returned string passed into onPostExecute() but that is upto you


ItemList itemsList = myXMLHandler.itemList;




Toast.makeText(getApplicationContext(), itemsList.getItem().size()+"", Toast.LENGTH_LONG).show();

if (null != itemsList && itemsList.getItem().size()>0) {

for (int index = 0; index <itemsList.getItem().size(); index++) {




System.out.println(">>>>>>>>>>>>>>>" + index);

System.out.println("ID :: " + itemsList.getItem().get(index));

System.out.println("TITLE :: " +itemsList.getCost().get(index));

System.out.println("DESC :: " + itemsList.getManufacturer().get(index));

System.out.println("PUBDATE :: " + itemsList.getModel().get(index));

// System.out.println("LINK :: " + objBean.getLink());

}


}

}



@Override

protected void onPreExecute() {

}



@Override

protected void onProgressUpdate(Void... values) {

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}



}





MyXMLHandler.java



import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;


public class MyXMLHandler extends DefaultHandler

{

public static ItemList itemList;

public boolean current = false;

public String currentValue = null;


@Override

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

// TODO Auto-generated method stub


current = true;


if (localName.equals("twitter"))

{

/** Start */

itemList = new ItemList();


}

}


@Override

public void endElement(String uri, String localName, String qName)

throws SAXException {

// TODO Auto-generated method stub

current = false;


if(localName.equals("title"))

{

itemList.setItem(currentValue);

}

else if(localName.equals("id"))

{

itemList.setManufacturer(currentValue);

}

else if(localName.equals("updated"))

{

itemList.setModel(currentValue);

}

else if(localName.equals("published"))

{

itemList.setCost(currentValue);

}

}


@Override

public void characters(char[] ch, int start, int length)

throws SAXException {

// TODO Auto-generated method stub


if(current)

{

currentValue = new String(ch, start, length);

current=false;

}

}

}





ItemList.java



import java.util.ArrayList;



public class ItemList

{

ArrayList item = new ArrayList();

ArrayList manufacturer = new ArrayList();

ArrayList model = new ArrayList();

ArrayList cost = new ArrayList();


public ArrayList getItem() {

return item;

}

public void setItem(String item) {

this.item.add(item);

}

public ArrayList getManufacturer() {

return manufacturer;

}

public void setManufacturer(String manufacturer) {

this.manufacturer.add(manufacturer);

}

public ArrayList getModel() {

return model;

}

public void setModel(String model) {

this.model.add(model);

}

public ArrayList getCost() {

return cost;

}

public void setCost(String cost) {

this.cost.add(cost);

}

}





activity_main.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >



<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />



</RelativeLayout>

AndroidManifest.xml



<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.twitterfeed"

android:versionCode="1"

android:versionName="1.0" >



<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.INTERNET"/>



<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.twitterfeed.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />



<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>



</manifest>




 
 


 
 


 

Comments

Popular posts from this blog

Volley for client and server interaction (Android)

Volley provides client and server interaction with QUEUE basis, each and every request process separately, we can cancel our request from QUEUE. HERE is a code sample for Volley with json request format. GenerateRequest.java -------------------------------------------------------------------- package com.entrata.maintenance.network_communication.networkutils; import android.content.Context; import com.android.volley.AuthFailureError; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import com.entrata.maintenance.application.ERPUserDefaults; import com.entrata.maintenance.network_communication.networkconstant.NetworkConstant; import com.entrata.maintenance.network_communication.networkexception.Error; import com.entrata.maintenance.network_communication.networkutil.SubdomainChange; import com.entrata.maintenance.utils.Logger; import com.google.gson.Gson; import org.json.JSONException; import org.js...
Java Code Examples for javax.net.ssl.HttpsURLConnection The following code examples are extracted from open source projects. You can click to vote up the examples you like. Your votes will be used in an intelligent system to get more and better code examples. Thanks for your votes! Code Example 1:   9  From project ADFS , under directory /adfs-hdfs-project/adfs-hdfs/src/main/java/org/apache/hadoop/hdfs/ . Source HsftpFileSystem.java @Override protected HttpURLConnection openConnection ( String path , String query ) throws IOException { query = addDelegationTokenParam ( query ); final URL url = new URL ( "https" , nnAddr . getHostName (), nnAddr . getPort (), path + '?' + query ); HttpsURLConnection conn =( HttpsURLConnection ) url . openConnection (); conn . setHostnameVerifier ( new DummyHostnameVerifier ()); return ( HttpURLConnection ) conn ; } Code Example 2:  ...

Improving Layout Performance

Improving Layout Performance Layouts are a key part of Android applications that directly affect the user experience. If implemented poorly, your layout can lead to a memory hungry application with slow UIs. The Android SDK includes tools to help you identify problems in your layout performance, which when combined the lessons here, you will be able to implement smooth scrolling interfaces with a minimum memory footprint. Lessons Optimizing Layout Hierarchies In the same way a complex web page can slow down load time, your layout hierarchy if too complex can also cause performance problems. This lesson shows how you can use SDK tools to inspect your layout and discover performance bottlenecks. Re-using Layouts with <include/> If your application UI repeats certain layout constructs in multiple places, this lesson shows you how to create efficient, re-usable layout constructs, then include them in the appropriate UI layouts. Loading Views On Demand Be...