Step 1> activity_main.xml:
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="407dp" >
<LinearLayout/>
<ScrollView/>
Step 2> download and Configure ksoap2 library.jar file
Step 3> paste this code in MainActivity.java:
package com.example.rajutaskwebservice;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
Toast.makeText(getApplicationContext(),"welcome", Toast.LENGTH_LONG).show();
//first downlode web service in xml format
/*
new Thread(){
public void run(){
try{
DefaultHttpClient client=new DefaultHttpClient();
//Toast.makeText(getApplicationContext(),"welcome1", Toast.LENGTH_LONG).show();
HttpGet get=new HttpGet(" give ur web service .php");
HttpResponse response=client.execute(get);
// Toast.makeText(getApplicationContext(),"welcome2", Toast.LENGTH_LONG).show();
//data reading
InputStream isr=response.getEntity().getContent();
String out_path=Environment.getExternalStorageDirectory()+"/out_put_file.xml";
//data writing
FileOutputStream fos=new FileOutputStream(out_path);
int i=isr.read();
String msg="";
// Toast.makeText(getApplicationContext(),"welcome3", Toast.LENGTH_LONG).show();
while(i!=-1){
msg=msg+(char)i;
fos.write(i);
i=isr.read();
}
if(i==-1){
displayResult(msg);
}
// Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
// Toast.makeText(getApplicationContext(),"fdfsdds", Toast.LENGTH_LONG).show();
}
}
}.start();
}
public void displayResult(String res){
Toast.makeText(getApplicationContext(), res, 2000).show();
}*/
// this is for binding particular tag to list view
try
{
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parse=factory.newSAXParser();
String path=Environment.getExternalStorageDirectory()+"/out_put_file.xml";
File f=new File(path);
// String[] values=f.list();
final ArrayList values=new ArrayList();
parse.parse(f,new DefaultHandler()
{
boolean value=false;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if(value)
{
values.add(new String(ch,start,length));
}
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
if(qName.equalsIgnoreCase("source"))
{
//here "source" is nothing but required tag which is to bind to listview
value=true;
}
};
public void endElement(String uri, String localName, String qName) throws SAXException
{
if(qName.equalsIgnoreCase("source"))
{
value=false;
}
};
}
);
ArrayAdapter adapter=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,values);
lv.setAdapter(adapter);
}
catch (Exception e)
{
// TODO: handle exception
}
}
}
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="407dp" >
<ScrollView/>
Step 2> download and Configure ksoap2 library.jar file
Step 3> paste this code in MainActivity.java:
package com.example.rajutaskwebservice;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
Toast.makeText(getApplicationContext(),"welcome", Toast.LENGTH_LONG).show();
//first downlode web service in xml format
/*
new Thread(){
public void run(){
try{
DefaultHttpClient client=new DefaultHttpClient();
//Toast.makeText(getApplicationContext(),"welcome1", Toast.LENGTH_LONG).show();
HttpGet get=new HttpGet(" give ur web service .php");
HttpResponse response=client.execute(get);
// Toast.makeText(getApplicationContext(),"welcome2", Toast.LENGTH_LONG).show();
//data reading
InputStream isr=response.getEntity().getContent();
String out_path=Environment.getExternalStorageDirectory()+"/out_put_file.xml";
//data writing
FileOutputStream fos=new FileOutputStream(out_path);
int i=isr.read();
String msg="";
// Toast.makeText(getApplicationContext(),"welcome3", Toast.LENGTH_LONG).show();
while(i!=-1){
msg=msg+(char)i;
fos.write(i);
i=isr.read();
}
if(i==-1){
displayResult(msg);
}
// Toast.makeText(getApplicationContext(),msg, Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
// Toast.makeText(getApplicationContext(),"fdfsdds", Toast.LENGTH_LONG).show();
}
}
}.start();
}
public void displayResult(String res){
Toast.makeText(getApplicationContext(), res, 2000).show();
}*/
// this is for binding particular tag to list view
try
{
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parse=factory.newSAXParser();
String path=Environment.getExternalStorageDirectory()+"/out_put_file.xml";
File f=new File(path);
// String[] values=f.list();
final ArrayList
parse.parse(f,new DefaultHandler()
{
boolean value=false;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if(value)
{
values.add(new String(ch,start,length));
}
}
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
if(qName.equalsIgnoreCase("source"))
{
//here "source" is nothing but required tag which is to bind to listview
value=true;
}
};
public void endElement(String uri, String localName, String qName) throws SAXException
{
if(qName.equalsIgnoreCase("source"))
{
value=false;
}
};
}
);
ArrayAdapter
lv.setAdapter(adapter);
}
catch (Exception e)
{
// TODO: handle exception
}
}
}
No comments:
Post a Comment