main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/linearLayout1"></LinearLayout>
<TextView android:id="@+id/tv_title" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_actId" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_loc" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<ListView android:layout_height="wrap_content" android:id="@+id/lv_activitylist"
android:layout_width="match_parent"></ListView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/linearLayout1"></LinearLayout>
<TextView android:id="@+id/tv_title" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_actId" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/tv_loc" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<ListView android:layout_height="wrap_content" android:id="@+id/lv_activitylist"
android:layout_width="match_parent"></ListView>
</LinearLayout>
Manifest.xml
Give Internet permission only
ActivityFirst.java
package com.ParsingData;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ParsingData.ClientMethod.RequestMethod;
public class ActivityFirst extends Activity {
/** Called when the activity is first created. */
TextView activityId, location, title;
ArrayList<ActivityList> arrayItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activityId = (TextView) findViewById(R.id.tv_actId);
location = (TextView) findViewById(R.id.tv_loc);
title = (TextView) findViewById(R.id.tv_title);
ParseData();
}
public void ParseData() {
SharedPreferences user = getSharedPreferences("SELECTED_LOCATION_PREF",
MODE_PRIVATE);
String locationID = "cz";// user.getString("locationid", "");
ClientMethod client = new ClientMethod(
"http://www.e-traveldirect.com/wsmobile/WsMobile.asmx/GetActivities");
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ParsingData.ClientMethod.RequestMethod;
public class ActivityFirst extends Activity {
/** Called when the activity is first created. */
TextView activityId, location, title;
ArrayList<ActivityList> arrayItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
activityId = (TextView) findViewById(R.id.tv_actId);
location = (TextView) findViewById(R.id.tv_loc);
title = (TextView) findViewById(R.id.tv_title);
ParseData();
}
public void ParseData() {
SharedPreferences user = getSharedPreferences("SELECTED_LOCATION_PREF",
MODE_PRIVATE);
String locationID = "cz";// user.getString("locationid", "");
ClientMethod client = new ClientMethod(
"http://www.e-traveldirect.com/wsmobile/WsMobile.asmx/GetActivities");
// Visit site for ref.
// http://www.e-traveldirect.com/wsmobile/WsMobile.asmx?op=GetActivities
client.AddParam("location", locationID);
try {
client.Execute(RequestMethod.POST);
InputSource inSource = client.getinpSource();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
ActivityXMLhandler campusXMLhl = new ActivityXMLhandler();
xr.setContentHandler(campusXMLhl);
xr.parse(inSource);
arrayItems = ActivityXMLhandler.getArrayData();
Message myMessage = new Message();
myMessage.obj = "";
handler2.sendMessage(myMessage);
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
private Handler handler2 = new Handler() {
@Override
public void handleMessage(Message msg) {
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
for (int t = 0; t < arrayItems.size(); t++) {
/*
* activityId.setText("Activity ID :: " +
* arrayItems.get(0).getActId());
* location.setText("Location :: " +
* arrayItems.get(0).getLoc()); title.setText("Title :: " +
* arrayItems.get(0).getTitle());
*/
TextView textView = new TextView(ActivityFirst .this);
TextView textView1 = new TextView(ActivityFirst .this);
textView.setText("Activity ID ::" +"\n"
+ arrayItems.get(t).getActId());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.VERTICAL);
textView1.setText("\n");
layout.addView(textView, p);
}
/*
* activityId.setText("Activity ID :: " + ActList.getActId());
* location.setText("Location :: " + ActList.getLoc());
* title.setText("Title :: " + ActList.getTitle());
*/
}
};
}
// http://www.e-traveldirect.com/wsmobile/WsMobile.asmx?op=GetActivities
client.AddParam("location", locationID);
try {
client.Execute(RequestMethod.POST);
InputSource inSource = client.getinpSource();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
ActivityXMLhandler campusXMLhl = new ActivityXMLhandler();
xr.setContentHandler(campusXMLhl);
xr.parse(inSource);
arrayItems = ActivityXMLhandler.getArrayData();
Message myMessage = new Message();
myMessage.obj = "";
handler2.sendMessage(myMessage);
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
private Handler handler2 = new Handler() {
@Override
public void handleMessage(Message msg) {
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
for (int t = 0; t < arrayItems.size(); t++) {
/*
* activityId.setText("Activity ID :: " +
* arrayItems.get(0).getActId());
* location.setText("Location :: " +
* arrayItems.get(0).getLoc()); title.setText("Title :: " +
* arrayItems.get(0).getTitle());
*/
TextView textView = new TextView(ActivityFirst .this);
TextView textView1 = new TextView(ActivityFirst .this);
textView.setText("Activity ID ::" +"\n"
+ arrayItems.get(t).getActId());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.VERTICAL);
textView1.setText("\n");
layout.addView(textView, p);
}
/*
* activityId.setText("Activity ID :: " + ActList.getActId());
* location.setText("Location :: " + ActList.getLoc());
* title.setText("Title :: " + ActList.getTitle());
*/
}
};
}
ActivityList.java
package com.ParsingData;
import java.util.ArrayList;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class ActivityList extends BaseAdapter{
private String actId, loc, title;
public ActivityList(ActivityFirst activityFirst,
ArrayList<ActivityList> arrayItems) {
// TODO Auto-generated constructor stub
}
public ActivityList() {
// TODO Auto-generated constructor stub
}
public String getActId() {
return actId;
}
public void setActId(String actId) {
this.actId = actId;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
}
ActivityXMLhandler.java
package com.ParsingData;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ActivityXMLhandler extends DefaultHandler {
private StringBuffer buffer = new StringBuffer();
public static ArrayList<ActivityList> arrayData;
private ActivityList data;
public static ArrayList<ActivityList> getArrayData() {
return arrayData;
}
public static void setArrayData(ArrayList<ActivityList> arrayData) {
ActivityXMLhandler.arrayData = arrayData;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
buffer.setLength(0);
if (localName.equals("activities")) {
arrayData = new ArrayList<ActivityList>();
} else if (localName.equals("activity")) {
data = new ActivityList();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("activityid"))
data.setActId(buffer.toString());
else if (localName.equalsIgnoreCase("location"))
data.setLoc(buffer.toString());
else if (localName.equalsIgnoreCase("title"))
data.setTitle(buffer.toString());
else if (localName.equalsIgnoreCase("activity"))
arrayData.add(data);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
buffer.append(ch, start, length);
}
}
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ActivityXMLhandler extends DefaultHandler {
private StringBuffer buffer = new StringBuffer();
public static ArrayList<ActivityList> arrayData;
private ActivityList data;
public static ArrayList<ActivityList> getArrayData() {
return arrayData;
}
public static void setArrayData(ArrayList<ActivityList> arrayData) {
ActivityXMLhandler.arrayData = arrayData;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
buffer.setLength(0);
if (localName.equals("activities")) {
arrayData = new ArrayList<ActivityList>();
} else if (localName.equals("activity")) {
data = new ActivityList();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("activityid"))
data.setActId(buffer.toString());
else if (localName.equalsIgnoreCase("location"))
data.setLoc(buffer.toString());
else if (localName.equalsIgnoreCase("title"))
data.setTitle(buffer.toString());
else if (localName.equalsIgnoreCase("activity"))
arrayData.add(data);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
buffer.append(ch, start, length);
}
}
ClientMethod.java
package com.ParsingData;
import java.net.URLEncoder;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.xml.sax.InputSource;
public class ClientMethod {
public enum RequestMethod {
GET, POST
}
private String url;
private ArrayList<NameValuePair> params;
private HttpEntity respEntity;
private InputSource inpSource;
public HttpEntity getrespEntity() {
return respEntity;
}
public InputSource getinpSource() {
return inpSource;
}
public void AddParam(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
public ClientMethod(String url) {
this.url = url;
params = new ArrayList<NameValuePair>();
}
public void Execute(RequestMethod method) throws Exception {
switch (method) {
case GET: {
// add parameters
String combinedParams = "";
if (!params.isEmpty()) {
combinedParams += "?";
for (NameValuePair p : params) {
String paramString = p.getName() + "="
+ URLEncoder.encode(p.getValue(), "UTF-8");
if (combinedParams.length() > 1) {
combinedParams += "&" + paramString;
} else {
combinedParams += paramString;
}
}
}
HttpGet request = new HttpGet(url + combinedParams);
executeRequest(request, url);
break;
}
case POST: {
HttpPost request = new HttpPost(url);
if (!params.isEmpty()) {
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
executeRequest(request, url);
}
}
}
private void executeRequest(HttpUriRequest request, String url) {
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
respEntity = httpResponse.getEntity();
inpSource = retrieveInputStream(respEntity);
// Only Print XML
//InputStream aa = respEntity.getContent();
//StringBuilder bb = inputStreamToString(aa);
//Log.i("Response", bb.toString());
// Only Print XML
} catch (Exception e) {
e.printStackTrace();
}
}
// private StringBuilder inputStreamToString(InputStream is) {
// String line = "";
// StringBuilder total = new StringBuilder();
//
// // Wrap a BufferedReader around the InputStream
// BufferedReader rd = new BufferedReader(new InputStreamReader(is));
//
// // Read response until the end
// try {
// while ((line = rd.readLine()) != null) {
// total.append(line);
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// // Return full string
// return total;
// }
private InputSource retrieveInputStream(HttpEntity httpEntity) {
InputSource insrc = null;
try {
insrc = new InputSource(httpEntity.getContent());
} catch (Exception e) {
}
return insrc;
}
}
No comments:
Post a Comment