MySql Database
------------------
+----------+------------------+-----------------+------------+----------+----------+
| event_id | event_name | event_desc | e_date | e_time | e_img |
+----------+------------------+-----------------+------------+----------+----------+
| 101 | fifa world cup | postponed | 2014/06/03 | 02:00 PM | img1.jpg |
| 102 | Durand CUP | Coming Soon | NULL | NULL | img2.jpg |
| 103 | champions trophy | played in japan | 2014/02/05 | 03:00 PM | img3.jpg |
+----------+------------------+-----------------+------------+----------+----------+
Images
---------
WebContent->img
img1.jpg
img2.jpg
img3.jpg
Server Side Servlet
==============
Qfa_Events.java
-------------------
package com.mangium.qfa;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSON;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mysql.jdbc.Connection;
public class Qfa_Events extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String name=null;
String des=null;
String date=null;
String time=null;
String img = null;
String[] line=null;
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
/* out.println("Hello Servlet !!!!");
out.println("<html>");
out.println("<head><title>events</title></head>");
out.println("<body>");
out.println("<h1>Database Results</h1>");
out.println("</body></html>");*/
// connecting to database
try {
Class.forName("com.mysql.jdbc.Driver");
con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/qfa", "root", "root");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT event_id,event_name,event_desc,e_date,e_time,e_img FROM events");
// displaying records
int i=0;
JSONArray jArray = new JSONArray();
while(rs.next()){
int id=rs.getInt("event_id");
name=rs.getString("event_name");
des=rs.getString("event_desc");
date=rs.getString("e_date");
time=rs.getString("e_time");
img=rs.getString("e_img");
/* out.println("<img src='img/"+img+"' width='60' height='60'/>"+"</br>");
out.println("Image:"+ img +"</br>");
out.println("Id:"+ id +"</br>");
out.println("Name:"+ name +"</br>");
out.println("Description:"+ des +"</br>");
out.println("Date:"+ date +"</br>");
out.println("Time:"+ time +"</br>");
out.println("--------------------------------------"+"</br>");*/
JSONObject json=new JSONObject();
json.put("name", name);
json.put("description", des);
json.put("date", date);
json.put("time", time);
json.put("image", img);
jArray.put(i,json);
i++;
}
JSONObject jobj=new JSONObject();
jobj.put("events", jArray);
out.println(jobj.toString());
} catch (SQLException e) {
throw new ServletException("Servlet Could not display records.", e);
} catch (ClassNotFoundException e) {
throw new ServletException("JDBC Driver not found.", e);
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {}
}
out.close();
}
}
Extra jars
------------
json-lib-2.4-jdk15.jar
mysql-connector-java-5.1.25-bin.jar
json-rpc-1.0.jar
web.xml
----------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>QFA_Server</display-name>
<servlet>
<servlet-name>events</servlet-name>
<servlet-class>com.mangium.qfa.Qfa_Events</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>events</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html
------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Refresh" content="2;URL=*.do" >
<title>Insert title here</title>
</head>
<body>
<h1>Loading...</h1>
</body>
</html>
Client Side Android
===============
activity_main.xml
---------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Servlet" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event_name"
android:textColor="#C71585"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Display ListView" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
image_view.xml
---------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginTop="20dip"
android:layout_marginLeft="20dip"
android:textColor="#C71585"/>
</LinearLayout>
------------------
+----------+------------------+-----------------+------------+----------+----------+
| event_id | event_name | event_desc | e_date | e_time | e_img |
+----------+------------------+-----------------+------------+----------+----------+
| 101 | fifa world cup | postponed | 2014/06/03 | 02:00 PM | img1.jpg |
| 102 | Durand CUP | Coming Soon | NULL | NULL | img2.jpg |
| 103 | champions trophy | played in japan | 2014/02/05 | 03:00 PM | img3.jpg |
+----------+------------------+-----------------+------------+----------+----------+
Images
---------
WebContent->img
img1.jpg
img2.jpg
img3.jpg
Server Side Servlet
==============
Qfa_Events.java
-------------------
package com.mangium.qfa;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSON;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mysql.jdbc.Connection;
public class Qfa_Events extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String name=null;
String des=null;
String date=null;
String time=null;
String img = null;
String[] line=null;
public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
/* out.println("Hello Servlet !!!!");
out.println("<html>");
out.println("<head><title>events</title></head>");
out.println("<body>");
out.println("<h1>Database Results</h1>");
out.println("</body></html>");*/
// connecting to database
try {
Class.forName("com.mysql.jdbc.Driver");
con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/qfa", "root", "root");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT event_id,event_name,event_desc,e_date,e_time,e_img FROM events");
// displaying records
int i=0;
JSONArray jArray = new JSONArray();
while(rs.next()){
int id=rs.getInt("event_id");
name=rs.getString("event_name");
des=rs.getString("event_desc");
date=rs.getString("e_date");
time=rs.getString("e_time");
img=rs.getString("e_img");
/* out.println("<img src='img/"+img+"' width='60' height='60'/>"+"</br>");
out.println("Image:"+ img +"</br>");
out.println("Id:"+ id +"</br>");
out.println("Name:"+ name +"</br>");
out.println("Description:"+ des +"</br>");
out.println("Date:"+ date +"</br>");
out.println("Time:"+ time +"</br>");
out.println("--------------------------------------"+"</br>");*/
JSONObject json=new JSONObject();
json.put("name", name);
json.put("description", des);
json.put("date", date);
json.put("time", time);
json.put("image", img);
jArray.put(i,json);
i++;
}
JSONObject jobj=new JSONObject();
jobj.put("events", jArray);
out.println(jobj.toString());
} catch (SQLException e) {
throw new ServletException("Servlet Could not display records.", e);
} catch (ClassNotFoundException e) {
throw new ServletException("JDBC Driver not found.", e);
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {}
}
out.close();
}
}
Extra jars
------------
json-lib-2.4-jdk15.jar
mysql-connector-java-5.1.25-bin.jar
json-rpc-1.0.jar
web.xml
----------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>QFA_Server</display-name>
<servlet>
<servlet-name>events</servlet-name>
<servlet-class>com.mangium.qfa.Qfa_Events</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>events</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html
------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Refresh" content="2;URL=*.do" >
<title>Insert title here</title>
</head>
<body>
<h1>Loading...</h1>
</body>
</html>
Client Side Android
===============
activity_main.xml
---------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Servlet" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event_name"
android:textColor="#C71585"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Display ListView" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
image_view.xml
---------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginTop="20dip"
android:layout_marginLeft="20dip"
android:textColor="#C71585"/>
</LinearLayout>
MainActivity.java
--------------------
package com.mangium.qfa_project;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn,btn2;
static TextView tv;
static ImageView iv;
static ListView lv;
public static final String URL="http://192.168.1.129:8080/QFA_Server/*.do";
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView1);
iv=(ImageView) findViewById(R.id.imageView1);
lv=(ListView) findViewById(R.id.listView1);
btn=(Button) findViewById(R.id.button1);
btn2=(Button) findViewById(R.id.button2);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GetJSONData task = new GetJSONData();
task.execute(new String[] { URL });
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lv.setAdapter(new MyImageAdapter(getApplicationContext(),GetJSONData.nameID,GetJSONData.imageID));
}
});
}
}
GetJSONData.java
-----------------------
package com.mangium.qfa_project;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint.Join;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
public class GetJSONData extends AsyncTask<String, Void, String> {
String[] str=null;
String Enent_name=null;
String Enent_img=null;
String Image_path="http://192.168.1.129:8080/QFA_Server/img/";
static Bitmap mIcon=null;
static Bitmap mIcon1=null;
static Bitmap mIcon2=null;
static String[] nameID;
static String[] imageID;
@Override
protected String doInBackground(String... urls) {
String output = null;
for (String url : urls) {
output = getOutputFromUrl(url);
}
try {
InputStream in = new java.net.URL(Image_path+imageID[0]).openStream();
mIcon = BitmapFactory.decodeStream(in);
InputStream in1 = new java.net.URL(Image_path+imageID[1]).openStream();
mIcon1 = BitmapFactory.decodeStream(in1);
InputStream in2 = new java.net.URL(Image_path+imageID[2]).openStream();
mIcon2 = BitmapFactory.decodeStream(in2);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return output;
}
private String getOutputFromUrl(String url) {
String output = null;
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
output = EntityUtils.toString(httpEntity);
System.out.println(output);
try {
JSONObject jobj=new JSONObject(output);
JSONArray jarray=jobj.getJSONArray("events");
nameID=new String[jarray.length()];
imageID=new String[jarray.length()];
JSONObject jobject=null;
for(int i=0;i<=jarray.length();i++){
jobject=jarray.getJSONObject(i);
Enent_name=jobject.getString("name");
Enent_img=jobject.getString("image");
nameID[i]=jobject.getString("name");
imageID[i]=jobject.getString("image");
}
System.out.println("*****"+Enent_name);
System.out.println("*****"+imageID[1]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
@Override
protected void onPostExecute(String output) {
MainActivity.tv.setText("Event Name: " + nameID[0]);
MainActivity.iv.setImageBitmap(mIcon);
}
}
MyImageAdapter.java
-------------------------
package com.mangium.qfa_project;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyImageAdapter extends BaseAdapter {
Context context;
String[] nameID;
String[] imageID;
public MyImageAdapter(Context context, String[] nameID, String[] imageID) {
// TODO Auto-generated constructor stub
this.context=context;
this.nameID=nameID;
this.imageID=imageID;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return nameID.length;
}
@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 position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.image_view, parent,false);
TextView name=(TextView) rowView.findViewById(R.id.nameView);
ImageView iv=(ImageView) rowView.findViewById(R.id.imageView1);
name.setText(nameID[position]);
String str1=nameID[position];
if(str1.startsWith("fifa world cup")){
//iv.setImageResource(R.drawable.ic_launcher);
iv.setImageBitmap(GetJSONData.mIcon);
}
if(str1.startsWith("Durand CUP")){
//iv.setImageResource(R.drawable.ic_launcher);
iv.setImageBitmap(GetJSONData.mIcon1);
}
if(str1.startsWith("champions trophy")){
//iv.setImageResource(R.drawable.ic_launcher);
iv.setImageBitmap(GetJSONData.mIcon2);
}
return rowView;
}
}
No comments:
Post a Comment