Develop an android Application that performs the
following to promote the Swachhata Abhiyan in Surat
1) User type : End User
a) Give facility of registration to citizen who are
willing to join the Swachhata Abhiyan
b) Login facility to registered user only
c) Allow user to modify the details when
they successfully logged in
2) User type : Official
a) Login
b) List the registered people in listview areawise
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
AndroidManifest.xml
-----------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ouhvs.que14">
<uses-permission android:name=
"android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".euser" />
<activity android:name=".official" />
<activity android:name=".userdetail" />
<activity android:name=".register" />
<activity android:name=".uedit" />
<activity android:name=".display">
</activity>
</application>
</manifest>
------------------------
activity_main.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Swachhata Abhiyan App"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End User"
android:textSize="20sp"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="350dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Official User"
android:textSize="20sp"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:src="@drawable/img1" />
</RelativeLayout>
-----------------------
MainActivity.java
-----------------------
package com.example.ouhvs.que14;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button user,official;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user=(Button)findViewById(R.id.button);
official=(Button)findViewById(R.id.button2);
user.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,euser.class);
startActivity(i);
}
});
official.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,official.class);
startActivity(i);
}
});
}
}
---------------------------
activity_official.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.official">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter username : "
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="Enter password : "
android:textSize="20sp"
android:inputType="textPassword"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"
android:id="@+id/button3"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp" />
</RelativeLayout>
----------------
official.java
----------------
package com.example.ouhvs.que14;
import android.app.MediaRouteActionProvider;
import android.content.Intent;
import android.support.annotation.MainThread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class official extends AppCompatActivity {
EditText name,pass1;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_official);
name=(EditText)findViewById(R.id.editText);
pass1=(EditText)findViewById(R.id.editText2);
login=(Button)findViewById(R.id.button3);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/login.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
if(jobj.getInt("success")==1)
{
Toast.makeText(official.this, jobj.getString("msg").
toString(), Toast.LENGTH_SHORT).show();
Intent i=new Intent(official.this,display.class);
startActivity(i);
}
else if(jobj.getInt("success")==0)
{
Toast.makeText(official.this, jobj.getString("msg").
toString(), Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
Toast.makeText(official.this, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(official.this, error.getMessage(),
Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("uname",name.getText().toString());
param.put("pass",pass1.getText().toString());
return param;
}
};
RequestQueue rq= Volley.newRequestQueue(official.this);
rq.add(sr);
}
});
}
}
----------------
listview.xml
----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:id="@+id/row1"
android:textSize="15sp"
android:width="50sp"/>
</TableRow>
</TableLayout>
</LinearLayout>
---------------------------
activity_display.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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="com.example.ouhvs.que14.display">
<TableRow>
<TextView
android:layout_width="150sp"
android:layout_height="wrap_content"
android:text="Select area : "
android:textSize="20sp"
android:id="@+id/textView3"
android:layout_column="0" />
<Spinner
android:layout_width="200sp"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_column="1" />
</TableRow>
<ListView android:id="@+id/listView"
android:layout_column="12" />
</TableLayout>
----------------
display.java
----------------
package com.example.ouhvs.que14;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class display extends AppCompatActivity {
ListView lv;
ArrayList<String> alist,info;
ArrayAdapter<String> ad;
Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
lv=(ListView)findViewById(R.id.listView);
sp=(Spinner)findViewById(R.id.spinner);
alist=new ArrayList();
info=new ArrayList();
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/area.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jobj = new JSONObject(response);
if(jobj.getInt("success")==1)
{
JSONArray area=jobj.getJSONArray("areas");
for(int i=0;i<area.length();i++)
{
JSONObject obj=area.getJSONObject(i);
alist.add(obj.getString("area"));
}
ad=new ArrayAdapter<String>(display.this,
android.R.layout.simple_list_item_1,alist);
sp.setAdapter(ad);
}
else {
Toast.makeText(display.this, "Couldnt fetch data",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
Toast.makeText(display.this, "Problem : "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(display.this, "Error : "+error.getMessage(),
Toast.LENGTH_SHORT).show();
}
})
{
};
RequestQueue rq= Volley.newRequestQueue(display.this);
rq.add(sr);
sp.setOnItemSelectedListener(new AdapterView.
OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String str=alist.get(position);
loaddata(str);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void loaddata(final String str){
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/display.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
if(jobj.getInt("success")==1)
{
info.clear();
JSONArray jarr=jobj.getJSONArray("data");
for(int i=0;i<jarr.length();i++) {
JSONObject j = jarr.getJSONObject(i);
String str = j.getString("uname").toString() + " " +
j.getString("mno");
info.add(str);
}
lv.setAdapter(new ArrayAdapter<String>(display.this,
android.R.layout.simple_list_item_1,info));
}
else
{
Toast.makeText(display.this, "Problem loading data",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(display.this, "Error : "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("area",str);
return param;
}
};
RequestQueue rq=Volley.newRequestQueue(display.this);
rq.add(sr);
}
}
-------------------------
activity_euser.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.euser">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:hint="Enter username : "
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText4"
android:hint="Enter password : "
android:inputType="textPassword"
android:textSize="20sp"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"
android:id="@+id/button4"
android:layout_below="@+id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="20sp"
android:id="@+id/button5"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</RelativeLayout>
---------------
euser.java
---------------
package com.example.ouhvs.que14;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class euser extends AppCompatActivity {
Button login,register;
EditText uname,pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_euser);
login=(Button)findViewById(R.id.button4);
register=(Button)findViewById(R.id.button5);
uname=(EditText)findViewById(R.id.editText3);
pass=(EditText)findViewById(R.id.editText4);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/elogin.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
if(jobj.getInt("success")==1)
{
Toast.makeText(euser.this, jobj.getString("msg"),
Toast.LENGTH_SHORT).show();
Intent i=new Intent(euser.this,uedit.class);
i.putExtra("uname",uname.getText().toString());
i.putExtra("pass",pass.getText().toString());
i.putExtra("id",jobj.getString("id1"));
startActivity(i);
}
else if(jobj.getInt("success")==0)
{
Toast.makeText(euser.this, jobj.getString("msg"),
Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
Toast.makeText(euser.this, "Error "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(euser.this, "Problem "+error.getMessage(),
Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("uname",uname.getText().toString());
param.put("pass",pass.getText().toString());
return param;
}
};
RequestQueue rq= Volley.newRequestQueue(euser.this);
rq.add(sr);
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(euser.this,register.class);
startActivity(i);
}
});
}
}
----------------------------
activity_register.xml
----------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.register">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText5"
android:hint="Enter username : "
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textSize="20sp"
android:hint="Enter password : "
android:ems="10"
android:id="@+id/editText6"
android:layout_below="@+id/editText5"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText7"
android:hint="Enter area : "
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter mobile no. : "
android:textSize="20sp"
android:ems="10"
android:id="@+id/editText8"
android:layout_below="@+id/editText7"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="20sp"
android:id="@+id/button6"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
------------------
register.java
------------------
package com.example.ouhvs.que14;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class register extends AppCompatActivity {
EditText uname,pass,area,mno;
Button register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
uname=(EditText)findViewById(R.id.editText5);
pass=(EditText)findViewById(R.id.editText6);
area=(EditText)findViewById(R.id.editText7);
mno=(EditText)findViewById(R.id.editText8);
register=(Button)findViewById(R.id.button6);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/register.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jobj = new JSONObject(response);
if(jobj.getInt("success")==1)
{
Toast.makeText(register.this, "User registered
successfully", Toast.LENGTH_LONG).show();
}
else if(jobj.getInt("success")==0)
{
Toast.makeText(register.this, "Invalid data.
please enter correct data", Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(register.this, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(register.this, error.getMessage(),
Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param =new HashMap<String, String>();
param.put("uname",uname.getText().toString());
param.put("pass",pass.getText().toString());
param.put("area",area.getText().toString());
param.put("mno",mno.getText().toString());
return param;
}
};
RequestQueue rq=Volley.newRequestQueue(register.this);
rq.add(sr);
}
});
}
}
------------------------
activity_uedit.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.uedit">
<EditText
android:layout_width="210sp"
android:layout_height="wrap_content"
android:id="@+id/editText9"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="75dp"
android:visibility="visible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Enter password"
android:ems="10"
android:id="@+id/editText10"
android:layout_below="@+id/editText9"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<EditText
android:layout_width="210sp"
android:layout_height="wrap_content"
android:id="@+id/editText11"
android:textSize="20sp"
android:layout_below="@+id/editText10"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp" />
<EditText
android:layout_width="210sp"
android:layout_height="wrap_content"
android:id="@+id/editText12"
android:textSize="20sp"
android:layout_below="@+id/editText11"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:textSize="20sp"
android:id="@+id/button7"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:enabled="false" />
</RelativeLayout>
-------------
uedit.java
-------------
package com.example.ouhvs.que14;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class uedit extends AppCompatActivity {
TextView id;
EditText uname,pass,area,mno;
String uname1,pass1,id1;
Button update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uedit);
Intent i=getIntent();
uname1=i.getStringExtra("uname");
pass1=i.getStringExtra("pass");
id1=i.getStringExtra("id");
id=(TextView)findViewById(R.id.textView2);
uname=(EditText)findViewById(R.id.editText9);
pass=(EditText)findViewById(R.id.editText10);
area=(EditText)findViewById(R.id.editText11);
mno=(EditText)findViewById(R.id.editText12);
update=(Button)findViewById(R.id.button7);
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/fetchdata.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
id.setText(jobj.getString("id"));
uname.setText(jobj.getString("uname"));
pass.setText(jobj.getString("password"));
area.setText(jobj.getString("area"));
mno.setText(jobj.getString("mno"));
}catch (Exception e){
Toast.makeText(uedit.this, "Problem "+e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(uedit.this, "Error "+error.getMessage(),
Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("uname",uname1);
param.put("pass",pass1);
param.put("id",id1);
return param;
}
};
RequestQueue rq= Volley.newRequestQueue(uedit.this);
rq.add(sr);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/eupdate.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
if(jobj.getInt("success")==1)
{
Toast.makeText(uedit.this, "Data updated successfully",
Toast.LENGTH_SHORT).show();
}
else if(jobj.getInt("success")==0)
{
Toast.makeText(uedit.this, "Data could'nt be updated",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(uedit.this, "Error "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(uedit.this, error.getMessage(),
Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("id",id1);
param.put("uname",uname.getText().toString());
param.put("pass",pass.getText().toString());
param.put("area",area.getText().toString());
param.put("mno",mno.getText().toString());
return param;
}
};
RequestQueue rq=Volley.newRequestQueue(uedit.this);
rq.add(sr);
}
});
}
}
---------------------
Web Services
---------------------
---------------
elogin.php
---------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$qry=mysqli_query($cnn,"select uname,
password,id from euser where uname='$uname'
and password='$pass'");
if(mysqli_num_rows($qry)>0)
{
$response['success']=1;
$row1=mysqli_fetch_array($qry);
$response['msg']="User is valid";
$response['id1']=$row1['id'];
}
else
{
$response['success']=0;
$response['msg']="Invalid username or password";
}
echo json_encode($response);
?>
-------------
login.php
-------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$qry=mysqli_query($cnn,"select username,
password from official where username='$uname'
and password='$pass'");
if(mysqli_num_rows($qry)>0)
{
$response['success']=1;
$response['msg']="User is valid";
}
else
{
$response['success']=0;
$response['msg']="Invalid username or password";
}
echo json_encode($response);
?>
-----------------
register.php
-----------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$area=$_REQUEST['area'];
$mno=$_REQUEST['mno'];
if($uname!="" && $pass!="" && $area!="" && $mno!="")
{
$qry=mysqli_query($cnn,"INSERT INTO
`euser`(`uname`, `password`, `area`, `mobile`)
VALUES ('$uname','$pass','$area','$mno')");
if($qry)
{
$response['success']=1;
}
else
{
$response['success']=0;
}
}
else
{
$response['success']=0;
}
echo json_encode($response);
?>
-------------------
fetchdata.php
-------------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$id=$_REQUEST['id'];
$query=mysqli_query($cnn,"select * from euser where id='$id'");
if(mysqli_num_rows($query)>0)
{
$row=mysqli_fetch_array($query);
$response['id']=$row['id'];
$response['uname']=$row['uname'];
$response['password']=$row['password'];
$response['area']=$row['area'];
$response['mno']=$row['mobile'];
}
echo json_encode($response);
?>
------------------
eupdate.php
------------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$id=$_REQUEST['id'];
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$area=$_REQUEST['area'];
$mno=$_REQUEST['mno'];
if($id!="" && $uname!="" && $pass!="" && $area!="" && $mno!="")
{
$qry=mysqli_query($cnn,"UPDATE `euser`
SET `uname`='$uname',`password`='$pass',`area`='$area',
`mobile`='$mno' WHERE `id`='$id'");
if($qry)
{
$response['success']=1;
}
else
{
$response['success']=0;
}
}
else
{
$response['success']=0;
}
echo json_encode($response);
?>
-------------
area.php
-------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$qry=mysqli_query($cnn,"SELECT
distinct(`area`) FROM `euser` ORDER BY `area`");
$response['areas']=array();
if(mysqli_num_rows($qry)>0)
{
while($row=mysqli_fetch_array($qry))
{
$a=array();
$a['area']=$row['area'];
array_push($response['areas'],$a);
}
$response['success']=1;
}
else
{
$response['success']=0;
}
echo json_encode($response);
?>
----------------
display.php
----------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$area=$_REQUEST['area'];
$result=mysqli_query($cnn,"SELECT `uname`,
`area`, `mobile` FROM `euser` where `area`='$area'");
$response['data']=array();
if(mysqli_num_rows($result)>0)
{
while($row=mysqli_fetch_array($result))
{
$data=array();
$data['uname']=$row['uname'];
$data['mno']=$row['mobile'];
array_push($response['data'],$data);
}
$response['success']=1;
}
else
{
$response['success']=0;
}
echo json_encode($response);
?>
following to promote the Swachhata Abhiyan in Surat
1) User type : End User
a) Give facility of registration to citizen who are
willing to join the Swachhata Abhiyan
b) Login facility to registered user only
c) Allow user to modify the details when
they successfully logged in
2) User type : Official
a) Login
b) List the registered people in listview areawise
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
AndroidManifest.xml
-----------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ouhvs.que14">
<uses-permission android:name=
"android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".euser" />
<activity android:name=".official" />
<activity android:name=".userdetail" />
<activity android:name=".register" />
<activity android:name=".uedit" />
<activity android:name=".display">
</activity>
</application>
</manifest>
------------------------
activity_main.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Swachhata Abhiyan App"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End User"
android:textSize="20sp"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="350dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Official User"
android:textSize="20sp"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:src="@drawable/img1" />
</RelativeLayout>
-----------------------
MainActivity.java
-----------------------
package com.example.ouhvs.que14;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button user,official;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user=(Button)findViewById(R.id.button);
official=(Button)findViewById(R.id.button2);
user.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,euser.class);
startActivity(i);
}
});
official.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,official.class);
startActivity(i);
}
});
}
}
---------------------------
activity_official.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.official">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Enter username : "
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="Enter password : "
android:textSize="20sp"
android:inputType="textPassword"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"
android:id="@+id/button3"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp" />
</RelativeLayout>
----------------
official.java
----------------
package com.example.ouhvs.que14;
import android.app.MediaRouteActionProvider;
import android.content.Intent;
import android.support.annotation.MainThread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class official extends AppCompatActivity {
EditText name,pass1;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_official);
name=(EditText)findViewById(R.id.editText);
pass1=(EditText)findViewById(R.id.editText2);
login=(Button)findViewById(R.id.button3);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/login.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
if(jobj.getInt("success")==1)
{
Toast.makeText(official.this, jobj.getString("msg").
toString(), Toast.LENGTH_SHORT).show();
Intent i=new Intent(official.this,display.class);
startActivity(i);
}
else if(jobj.getInt("success")==0)
{
Toast.makeText(official.this, jobj.getString("msg").
toString(), Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
Toast.makeText(official.this, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(official.this, error.getMessage(),
Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("uname",name.getText().toString());
param.put("pass",pass1.getText().toString());
return param;
}
};
RequestQueue rq= Volley.newRequestQueue(official.this);
rq.add(sr);
}
});
}
}
----------------
listview.xml
----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:id="@+id/row1"
android:textSize="15sp"
android:width="50sp"/>
</TableRow>
</TableLayout>
</LinearLayout>
---------------------------
activity_display.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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="com.example.ouhvs.que14.display">
<TableRow>
<TextView
android:layout_width="150sp"
android:layout_height="wrap_content"
android:text="Select area : "
android:textSize="20sp"
android:id="@+id/textView3"
android:layout_column="0" />
<Spinner
android:layout_width="200sp"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_column="1" />
</TableRow>
<ListView android:id="@+id/listView"
android:layout_column="12" />
</TableLayout>
----------------
display.java
----------------
package com.example.ouhvs.que14;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class display extends AppCompatActivity {
ListView lv;
ArrayList<String> alist,info;
ArrayAdapter<String> ad;
Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
lv=(ListView)findViewById(R.id.listView);
sp=(Spinner)findViewById(R.id.spinner);
alist=new ArrayList();
info=new ArrayList();
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/area.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jobj = new JSONObject(response);
if(jobj.getInt("success")==1)
{
JSONArray area=jobj.getJSONArray("areas");
for(int i=0;i<area.length();i++)
{
JSONObject obj=area.getJSONObject(i);
alist.add(obj.getString("area"));
}
ad=new ArrayAdapter<String>(display.this,
android.R.layout.simple_list_item_1,alist);
sp.setAdapter(ad);
}
else {
Toast.makeText(display.this, "Couldnt fetch data",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
Toast.makeText(display.this, "Problem : "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(display.this, "Error : "+error.getMessage(),
Toast.LENGTH_SHORT).show();
}
})
{
};
RequestQueue rq= Volley.newRequestQueue(display.this);
rq.add(sr);
sp.setOnItemSelectedListener(new AdapterView.
OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
String str=alist.get(position);
loaddata(str);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void loaddata(final String str){
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/display.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
if(jobj.getInt("success")==1)
{
info.clear();
JSONArray jarr=jobj.getJSONArray("data");
for(int i=0;i<jarr.length();i++) {
JSONObject j = jarr.getJSONObject(i);
String str = j.getString("uname").toString() + " " +
j.getString("mno");
info.add(str);
}
lv.setAdapter(new ArrayAdapter<String>(display.this,
android.R.layout.simple_list_item_1,info));
}
else
{
Toast.makeText(display.this, "Problem loading data",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(display.this, "Error : "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("area",str);
return param;
}
};
RequestQueue rq=Volley.newRequestQueue(display.this);
rq.add(sr);
}
}
-------------------------
activity_euser.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.euser">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:hint="Enter username : "
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText4"
android:hint="Enter password : "
android:inputType="textPassword"
android:textSize="20sp"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"
android:id="@+id/button4"
android:layout_below="@+id/editText4"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="20sp"
android:id="@+id/button5"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
</RelativeLayout>
---------------
euser.java
---------------
package com.example.ouhvs.que14;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class euser extends AppCompatActivity {
Button login,register;
EditText uname,pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_euser);
login=(Button)findViewById(R.id.button4);
register=(Button)findViewById(R.id.button5);
uname=(EditText)findViewById(R.id.editText3);
pass=(EditText)findViewById(R.id.editText4);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/elogin.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
if(jobj.getInt("success")==1)
{
Toast.makeText(euser.this, jobj.getString("msg"),
Toast.LENGTH_SHORT).show();
Intent i=new Intent(euser.this,uedit.class);
i.putExtra("uname",uname.getText().toString());
i.putExtra("pass",pass.getText().toString());
i.putExtra("id",jobj.getString("id1"));
startActivity(i);
}
else if(jobj.getInt("success")==0)
{
Toast.makeText(euser.this, jobj.getString("msg"),
Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
Toast.makeText(euser.this, "Error "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(euser.this, "Problem "+error.getMessage(),
Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("uname",uname.getText().toString());
param.put("pass",pass.getText().toString());
return param;
}
};
RequestQueue rq= Volley.newRequestQueue(euser.this);
rq.add(sr);
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(euser.this,register.class);
startActivity(i);
}
});
}
}
----------------------------
activity_register.xml
----------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.register">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText5"
android:hint="Enter username : "
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textSize="20sp"
android:hint="Enter password : "
android:ems="10"
android:id="@+id/editText6"
android:layout_below="@+id/editText5"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText7"
android:hint="Enter area : "
android:textSize="20sp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="Enter mobile no. : "
android:textSize="20sp"
android:ems="10"
android:id="@+id/editText8"
android:layout_below="@+id/editText7"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:textSize="20sp"
android:id="@+id/button6"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
------------------
register.java
------------------
package com.example.ouhvs.que14;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class register extends AppCompatActivity {
EditText uname,pass,area,mno;
Button register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
uname=(EditText)findViewById(R.id.editText5);
pass=(EditText)findViewById(R.id.editText6);
area=(EditText)findViewById(R.id.editText7);
mno=(EditText)findViewById(R.id.editText8);
register=(Button)findViewById(R.id.button6);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/register.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jobj = new JSONObject(response);
if(jobj.getInt("success")==1)
{
Toast.makeText(register.this, "User registered
successfully", Toast.LENGTH_LONG).show();
}
else if(jobj.getInt("success")==0)
{
Toast.makeText(register.this, "Invalid data.
please enter correct data", Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(register.this, e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(register.this, error.getMessage(),
Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param =new HashMap<String, String>();
param.put("uname",uname.getText().toString());
param.put("pass",pass.getText().toString());
param.put("area",area.getText().toString());
param.put("mno",mno.getText().toString());
return param;
}
};
RequestQueue rq=Volley.newRequestQueue(register.this);
rq.add(sr);
}
});
}
}
------------------------
activity_uedit.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.ouhvs.que14.uedit">
<EditText
android:layout_width="210sp"
android:layout_height="wrap_content"
android:id="@+id/editText9"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="75dp"
android:visibility="visible" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Enter password"
android:ems="10"
android:id="@+id/editText10"
android:layout_below="@+id/editText9"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<EditText
android:layout_width="210sp"
android:layout_height="wrap_content"
android:id="@+id/editText11"
android:textSize="20sp"
android:layout_below="@+id/editText10"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp" />
<EditText
android:layout_width="210sp"
android:layout_height="wrap_content"
android:id="@+id/editText12"
android:textSize="20sp"
android:layout_below="@+id/editText11"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update"
android:textSize="20sp"
android:id="@+id/button7"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="100sp"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:enabled="false" />
</RelativeLayout>
-------------
uedit.java
-------------
package com.example.ouhvs.que14;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class uedit extends AppCompatActivity {
TextView id;
EditText uname,pass,area,mno;
String uname1,pass1,id1;
Button update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uedit);
Intent i=getIntent();
uname1=i.getStringExtra("uname");
pass1=i.getStringExtra("pass");
id1=i.getStringExtra("id");
id=(TextView)findViewById(R.id.textView2);
uname=(EditText)findViewById(R.id.editText9);
pass=(EditText)findViewById(R.id.editText10);
area=(EditText)findViewById(R.id.editText11);
mno=(EditText)findViewById(R.id.editText12);
update=(Button)findViewById(R.id.button7);
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/fetchdata.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
id.setText(jobj.getString("id"));
uname.setText(jobj.getString("uname"));
pass.setText(jobj.getString("password"));
area.setText(jobj.getString("area"));
mno.setText(jobj.getString("mno"));
}catch (Exception e){
Toast.makeText(uedit.this, "Problem "+e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(uedit.this, "Error "+error.getMessage(),
Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("uname",uname1);
param.put("pass",pass1);
param.put("id",id1);
return param;
}
};
RequestQueue rq= Volley.newRequestQueue(uedit.this);
rq.add(sr);
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringRequest sr=new StringRequest(Request.Method.POST,
"http://10.0.2.2:7080/android/eupdate.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try
{
JSONObject jobj=new JSONObject(response);
if(jobj.getInt("success")==1)
{
Toast.makeText(uedit.this, "Data updated successfully",
Toast.LENGTH_SHORT).show();
}
else if(jobj.getInt("success")==0)
{
Toast.makeText(uedit.this, "Data could'nt be updated",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(uedit.this, "Error "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(uedit.this, error.getMessage(),
Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param=new HashMap<String, String>();
param.put("id",id1);
param.put("uname",uname.getText().toString());
param.put("pass",pass.getText().toString());
param.put("area",area.getText().toString());
param.put("mno",mno.getText().toString());
return param;
}
};
RequestQueue rq=Volley.newRequestQueue(uedit.this);
rq.add(sr);
}
});
}
}
---------------------
Web Services
---------------------
---------------
elogin.php
---------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$qry=mysqli_query($cnn,"select uname,
password,id from euser where uname='$uname'
and password='$pass'");
if(mysqli_num_rows($qry)>0)
{
$response['success']=1;
$row1=mysqli_fetch_array($qry);
$response['msg']="User is valid";
$response['id1']=$row1['id'];
}
else
{
$response['success']=0;
$response['msg']="Invalid username or password";
}
echo json_encode($response);
?>
-------------
login.php
-------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$qry=mysqli_query($cnn,"select username,
password from official where username='$uname'
and password='$pass'");
if(mysqli_num_rows($qry)>0)
{
$response['success']=1;
$response['msg']="User is valid";
}
else
{
$response['success']=0;
$response['msg']="Invalid username or password";
}
echo json_encode($response);
?>
-----------------
register.php
-----------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$area=$_REQUEST['area'];
$mno=$_REQUEST['mno'];
if($uname!="" && $pass!="" && $area!="" && $mno!="")
{
$qry=mysqli_query($cnn,"INSERT INTO
`euser`(`uname`, `password`, `area`, `mobile`)
VALUES ('$uname','$pass','$area','$mno')");
if($qry)
{
$response['success']=1;
}
else
{
$response['success']=0;
}
}
else
{
$response['success']=0;
}
echo json_encode($response);
?>
-------------------
fetchdata.php
-------------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$id=$_REQUEST['id'];
$query=mysqli_query($cnn,"select * from euser where id='$id'");
if(mysqli_num_rows($query)>0)
{
$row=mysqli_fetch_array($query);
$response['id']=$row['id'];
$response['uname']=$row['uname'];
$response['password']=$row['password'];
$response['area']=$row['area'];
$response['mno']=$row['mobile'];
}
echo json_encode($response);
?>
------------------
eupdate.php
------------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$id=$_REQUEST['id'];
$uname=$_REQUEST['uname'];
$pass=$_REQUEST['pass'];
$area=$_REQUEST['area'];
$mno=$_REQUEST['mno'];
if($id!="" && $uname!="" && $pass!="" && $area!="" && $mno!="")
{
$qry=mysqli_query($cnn,"UPDATE `euser`
SET `uname`='$uname',`password`='$pass',`area`='$area',
`mobile`='$mno' WHERE `id`='$id'");
if($qry)
{
$response['success']=1;
}
else
{
$response['success']=0;
}
}
else
{
$response['success']=0;
}
echo json_encode($response);
?>
-------------
area.php
-------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$qry=mysqli_query($cnn,"SELECT
distinct(`area`) FROM `euser` ORDER BY `area`");
$response['areas']=array();
if(mysqli_num_rows($qry)>0)
{
while($row=mysqli_fetch_array($qry))
{
$a=array();
$a['area']=$row['area'];
array_push($response['areas'],$a);
}
$response['success']=1;
}
else
{
$response['success']=0;
}
echo json_encode($response);
?>
----------------
display.php
----------------
<?php
$response=array();
$cnn=mysqli_connect("localhost","root","","android");
$area=$_REQUEST['area'];
$result=mysqli_query($cnn,"SELECT `uname`,
`area`, `mobile` FROM `euser` where `area`='$area'");
$response['data']=array();
if(mysqli_num_rows($result)>0)
{
while($row=mysqli_fetch_array($result))
{
$data=array();
$data['uname']=$row['uname'];
$data['mno']=$row['mobile'];
array_push($response['data'],$data);
}
$response['success']=1;
}
else
{
$response['success']=0;
}
echo json_encode($response);
?>
No comments:
Post a Comment