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

Bitmap scalling and cropping from center

How to Bitmap scalling and cropping from center? public class ScalingUtilities {     /**      * Utility function for decoding an image resource. The decoded bitmap will      * be optimized for further scaling to the requested destination dimensions      * and scaling logic.      *      * @param res      *            The resources object containing the image data      * @param resId      *            The resource id of the image data      * @param dstWidth      *            Width of destination area      * @param dstHeight      *     ...

Custom camera using SurfaceView android with autofocus & auto lights & more

Custom camera using SurfaceView android with autofocus & auto lights & much more /**  * @author Tatyabhau Chavan  *  */ public class Preview extends SurfaceView implements SurfaceHolder.Callback {     private SurfaceHolder mHolder;     private Camera mCamera;     public Camera.Parameters mParameters;     private byte[] mBuffer;     private Activity mActivity;     // this constructor used when requested as an XML resource     public Preview(Context context, AttributeSet attrs) {         super(context, attrs);         init();     }     public Preview(Context context) {         super(context);         init();     }     public Camera getCamera() {        ...

Get Android phone call history/log programmatically

Get Android phone call history/log programmatically To get call history programmatically first add read conact permission in Manifest file : <uses-permission android:name="android.permission.READ_CONTACTS" /> Create xml file. Add the below code in xml file : <Linearlayout android:layout_height="fill_parent"  android:layout_width="fill_parent" android:orientation="vertical"> <Textview android:id="@+id/call" android:layout_height="fill_parent" android:layout_width="fill_parent"> </Textview> </Linearlayout> Now call the getCallDetails() method in java class : private void getCallDetails() { StringBuffer sb = new StringBuffer(); Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null); int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE ); int date = managedCur...