listaUsuarios) {
24 | this.listaUsuarios = listaUsuarios;
25 | }
26 |
27 | @Override
28 | public UsuariosHolder onCreateViewHolder(ViewGroup parent, int viewType) {
29 | View vista= LayoutInflater.from(parent.getContext()).inflate(R.layout.usuarios_list_image,parent,false);
30 | RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
31 | ViewGroup.LayoutParams.WRAP_CONTENT);
32 | vista.setLayoutParams(layoutParams);
33 | return new UsuariosHolder(vista);
34 | }
35 |
36 | @Override
37 | public void onBindViewHolder(UsuariosHolder holder, int position) {
38 | holder.txtDocumento.setText(listaUsuarios.get(position).getDocumento().toString());
39 | holder.txtNombre.setText(listaUsuarios.get(position).getNombre().toString());
40 | holder.txtProfesion.setText(listaUsuarios.get(position).getProfesion().toString());
41 |
42 | if (listaUsuarios.get(position).getImagen()!=null){
43 | holder.imagen.setImageBitmap(listaUsuarios.get(position).getImagen());
44 | }else{
45 | holder.imagen.setImageResource(R.drawable.img_base);
46 | }
47 | }
48 |
49 | @Override
50 | public int getItemCount() {
51 | return listaUsuarios.size();
52 | }
53 |
54 | public class UsuariosHolder extends RecyclerView.ViewHolder{
55 |
56 | TextView txtDocumento,txtNombre,txtProfesion;
57 | ImageView imagen;
58 |
59 | public UsuariosHolder(View itemView) {
60 | super(itemView);
61 | txtDocumento= (TextView) itemView.findViewById(R.id.idDocumento);
62 | txtNombre= (TextView) itemView.findViewById(R.id.idNombre);
63 | txtProfesion= (TextView) itemView.findViewById(R.id.idProfesion);
64 | imagen=(ImageView) itemView.findViewById(R.id.idImagen);
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/usuarios_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
18 |
19 |
26 |
27 |
28 |
29 |
33 |
34 |
41 |
42 |
49 |
50 |
51 |
52 |
53 |
57 |
58 |
65 |
66 |
73 |
74 |
75 |
76 |
81 |
82 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_registrar_usuario.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
13 |
14 |
23 |
24 |
28 |
29 |
35 |
36 |
44 |
45 |
46 |
47 |
52 |
53 |
58 |
59 |
60 |
68 |
69 |
70 |
71 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/fragments/BienvenidaFragment.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.fragments;
2 |
3 | import android.content.Context;
4 | import android.net.Uri;
5 | import android.os.Bundle;
6 | import android.support.v4.app.Fragment;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 |
11 | import co.quindio.sena.tutorialwebservice.R;
12 |
13 | /**
14 | * A simple {@link Fragment} subclass.
15 | * Activities that contain this fragment must implement the
16 | * {@link BienvenidaFragment.OnFragmentInteractionListener} interface
17 | * to handle interaction events.
18 | * Use the {@link BienvenidaFragment#newInstance} factory method to
19 | * create an instance of this fragment.
20 | */
21 | public class BienvenidaFragment extends Fragment {
22 | // TODO: Rename parameter arguments, choose names that match
23 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
24 | private static final String ARG_PARAM1 = "param1";
25 | private static final String ARG_PARAM2 = "param2";
26 |
27 | // TODO: Rename and change types of parameters
28 | private String mParam1;
29 | private String mParam2;
30 |
31 | private OnFragmentInteractionListener mListener;
32 |
33 | public BienvenidaFragment() {
34 | // Required empty public constructor
35 | }
36 |
37 | /**
38 | * Use this factory method to create a new instance of
39 | * this fragment using the provided parameters.
40 | *
41 | * @param param1 Parameter 1.
42 | * @param param2 Parameter 2.
43 | * @return A new instance of fragment BienvenidaFragment.
44 | */
45 | // TODO: Rename and change types and number of parameters
46 | public static BienvenidaFragment newInstance(String param1, String param2) {
47 | BienvenidaFragment fragment = new BienvenidaFragment();
48 | Bundle args = new Bundle();
49 | args.putString(ARG_PARAM1, param1);
50 | args.putString(ARG_PARAM2, param2);
51 | fragment.setArguments(args);
52 | return fragment;
53 | }
54 |
55 | @Override
56 | public void onCreate(Bundle savedInstanceState) {
57 | super.onCreate(savedInstanceState);
58 | if (getArguments() != null) {
59 | mParam1 = getArguments().getString(ARG_PARAM1);
60 | mParam2 = getArguments().getString(ARG_PARAM2);
61 | }
62 | }
63 |
64 | @Override
65 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
66 | Bundle savedInstanceState) {
67 | // Inflate the layout for this fragment
68 | return inflater.inflate(R.layout.fragment_bienvenida, container, false);
69 | }
70 |
71 | // TODO: Rename method, update argument and hook method into UI event
72 | public void onButtonPressed(Uri uri) {
73 | if (mListener != null) {
74 | mListener.onFragmentInteraction(uri);
75 | }
76 | }
77 |
78 | @Override
79 | public void onAttach(Context context) {
80 | super.onAttach(context);
81 | if (context instanceof OnFragmentInteractionListener) {
82 | mListener = (OnFragmentInteractionListener) context;
83 | } else {
84 | throw new RuntimeException(context.toString()
85 | + " must implement OnFragmentInteractionListener");
86 | }
87 | }
88 |
89 | @Override
90 | public void onDetach() {
91 | super.onDetach();
92 | mListener = null;
93 | }
94 |
95 | /**
96 | * This interface must be implemented by activities that contain this
97 | * fragment to allow an interaction in this fragment to be communicated
98 | * to the activity and potentially other fragments contained in that
99 | * activity.
100 | *
101 | * See the Android Training lesson Communicating with Other Fragments for more information.
104 | */
105 | public interface OnFragmentInteractionListener {
106 | // TODO: Update argument type and name
107 | void onFragmentInteraction(Uri uri);
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/fragments/DesarrolladorFragment.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.fragments;
2 |
3 | import android.content.Context;
4 | import android.net.Uri;
5 | import android.os.Bundle;
6 | import android.support.v4.app.Fragment;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 |
11 | import co.quindio.sena.tutorialwebservice.R;
12 |
13 | /**
14 | * A simple {@link Fragment} subclass.
15 | * Activities that contain this fragment must implement the
16 | * {@link DesarrolladorFragment.OnFragmentInteractionListener} interface
17 | * to handle interaction events.
18 | * Use the {@link DesarrolladorFragment#newInstance} factory method to
19 | * create an instance of this fragment.
20 | */
21 | public class DesarrolladorFragment extends Fragment {
22 | // TODO: Rename parameter arguments, choose names that match
23 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
24 | private static final String ARG_PARAM1 = "param1";
25 | private static final String ARG_PARAM2 = "param2";
26 |
27 | // TODO: Rename and change types of parameters
28 | private String mParam1;
29 | private String mParam2;
30 |
31 | private OnFragmentInteractionListener mListener;
32 |
33 | public DesarrolladorFragment() {
34 | // Required empty public constructor
35 | }
36 |
37 | /**
38 | * Use this factory method to create a new instance of
39 | * this fragment using the provided parameters.
40 | *
41 | * @param param1 Parameter 1.
42 | * @param param2 Parameter 2.
43 | * @return A new instance of fragment DesarrolladorFragment.
44 | */
45 | // TODO: Rename and change types and number of parameters
46 | public static DesarrolladorFragment newInstance(String param1, String param2) {
47 | DesarrolladorFragment fragment = new DesarrolladorFragment();
48 | Bundle args = new Bundle();
49 | args.putString(ARG_PARAM1, param1);
50 | args.putString(ARG_PARAM2, param2);
51 | fragment.setArguments(args);
52 | return fragment;
53 | }
54 |
55 | @Override
56 | public void onCreate(Bundle savedInstanceState) {
57 | super.onCreate(savedInstanceState);
58 | if (getArguments() != null) {
59 | mParam1 = getArguments().getString(ARG_PARAM1);
60 | mParam2 = getArguments().getString(ARG_PARAM2);
61 | }
62 | }
63 |
64 | @Override
65 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
66 | Bundle savedInstanceState) {
67 | // Inflate the layout for this fragment
68 | return inflater.inflate(R.layout.fragment_desarrollador, container, false);
69 | }
70 |
71 | // TODO: Rename method, update argument and hook method into UI event
72 | public void onButtonPressed(Uri uri) {
73 | if (mListener != null) {
74 | mListener.onFragmentInteraction(uri);
75 | }
76 | }
77 |
78 | @Override
79 | public void onAttach(Context context) {
80 | super.onAttach(context);
81 | if (context instanceof OnFragmentInteractionListener) {
82 | mListener = (OnFragmentInteractionListener) context;
83 | } else {
84 | throw new RuntimeException(context.toString()
85 | + " must implement OnFragmentInteractionListener");
86 | }
87 | }
88 |
89 | @Override
90 | public void onDetach() {
91 | super.onDetach();
92 | mListener = null;
93 | }
94 |
95 | /**
96 | * This interface must be implemented by activities that contain this
97 | * fragment to allow an interaction in this fragment to be communicated
98 | * to the activity and potentially other fragments contained in that
99 | * activity.
100 | *
101 | * See the Android Training lesson Communicating with Other Fragments for more information.
104 | */
105 | public interface OnFragmentInteractionListener {
106 | // TODO: Update argument type and name
107 | void onFragmentInteraction(Uri uri);
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/adapter/UsuariosImagenUrlAdapter.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.adapter;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.support.v7.widget.RecyclerView;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 | import android.widget.Toast;
12 |
13 | import com.android.volley.RequestQueue;
14 | import com.android.volley.Response;
15 | import com.android.volley.VolleyError;
16 | import com.android.volley.toolbox.ImageRequest;
17 | import com.android.volley.toolbox.Volley;
18 |
19 | import java.util.List;
20 |
21 | import co.quindio.sena.tutorialwebservice.R;
22 | import co.quindio.sena.tutorialwebservice.entidades.Usuario;
23 | import co.quindio.sena.tutorialwebservice.entidades.VolleySingleton;
24 |
25 | /**
26 | * Created by CHENAO on 6/08/2017.
27 | */
28 |
29 | public class UsuariosImagenUrlAdapter extends RecyclerView.Adapter{
30 |
31 | List listaUsuarios;
32 | // RequestQueue request;
33 | Context context;
34 |
35 |
36 | public UsuariosImagenUrlAdapter(List listaUsuarios, Context context) {
37 | this.listaUsuarios = listaUsuarios;
38 | this.context=context;
39 | // request= Volley.newRequestQueue(context);
40 | }
41 |
42 | @Override
43 | public UsuariosHolder onCreateViewHolder(ViewGroup parent, int viewType) {
44 | View vista= LayoutInflater.from(parent.getContext()).inflate(R.layout.usuarios_list_image,parent,false);
45 | RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
46 | ViewGroup.LayoutParams.WRAP_CONTENT);
47 | vista.setLayoutParams(layoutParams);
48 | return new UsuariosHolder(vista);
49 | }
50 |
51 | @Override
52 | public void onBindViewHolder(UsuariosHolder holder, int position) {
53 | holder.txtDocumento.setText(listaUsuarios.get(position).getDocumento().toString());
54 | holder.txtNombre.setText(listaUsuarios.get(position).getNombre().toString());
55 | holder.txtProfesion.setText(listaUsuarios.get(position).getProfesion().toString());
56 |
57 | if (listaUsuarios.get(position).getRutaImagen()!=null){
58 | //
59 | cargarImagenWebService(listaUsuarios.get(position).getRutaImagen(),holder);
60 | }else{
61 | holder.imagen.setImageResource(R.drawable.img_base);
62 | }
63 | }
64 |
65 | private void cargarImagenWebService(String rutaImagen, final UsuariosHolder holder) {
66 |
67 | String ip=context.getString(R.string.ip);
68 |
69 | String urlImagen=ip+"/ejemploBDRemota/"+rutaImagen;
70 | urlImagen=urlImagen.replace(" ","%20");
71 |
72 | ImageRequest imageRequest=new ImageRequest(urlImagen, new Response.Listener() {
73 | @Override
74 | public void onResponse(Bitmap response) {
75 | holder.imagen.setImageBitmap(response);
76 | }
77 | }, 0, 0, ImageView.ScaleType.CENTER, null, new Response.ErrorListener() {
78 | @Override
79 | public void onErrorResponse(VolleyError error) {
80 | Toast.makeText(context,"Error al cargar la imagen",Toast.LENGTH_SHORT).show();
81 | }
82 | });
83 | //request.add(imageRequest);
84 | VolleySingleton.getIntanciaVolley(context).addToRequestQueue(imageRequest);
85 | }
86 |
87 | @Override
88 | public int getItemCount() {
89 | return listaUsuarios.size();
90 | }
91 |
92 | public class UsuariosHolder extends RecyclerView.ViewHolder{
93 |
94 | TextView txtDocumento,txtNombre,txtProfesion;
95 | ImageView imagen;
96 |
97 | public UsuariosHolder(View itemView) {
98 | super(itemView);
99 | txtDocumento= (TextView) itemView.findViewById(R.id.idDocumento);
100 | txtNombre= (TextView) itemView.findViewById(R.id.idNombre);
101 | txtProfesion= (TextView) itemView.findViewById(R.id.idProfesion);
102 | imagen=(ImageView) itemView.findViewById(R.id.idImagen);
103 | }
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_consulta_usuario_url.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
15 |
16 |
25 |
26 |
30 |
31 |
37 |
38 |
48 |
49 |
50 |
51 |
52 |
53 |
60 |
61 |
67 |
68 |
69 |
70 |
78 |
79 |
88 |
89 |
98 |
99 |
100 |
101 |
109 |
110 |
111 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/MainActivity.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice;
2 |
3 | import android.net.Uri;
4 | import android.os.Bundle;
5 | import android.support.design.widget.FloatingActionButton;
6 | import android.support.design.widget.Snackbar;
7 | import android.support.v4.app.Fragment;
8 | import android.view.View;
9 | import android.support.design.widget.NavigationView;
10 | import android.support.v4.view.GravityCompat;
11 | import android.support.v4.widget.DrawerLayout;
12 | import android.support.v7.app.ActionBarDrawerToggle;
13 | import android.support.v7.app.AppCompatActivity;
14 | import android.support.v7.widget.Toolbar;
15 | import android.view.Menu;
16 | import android.view.MenuItem;
17 |
18 | import co.quindio.sena.tutorialwebservice.fragments.BienvenidaFragment;
19 | import co.quindio.sena.tutorialwebservice.fragments.ConsultaListaUsuarioImagenUrlFragment;
20 | import co.quindio.sena.tutorialwebservice.fragments.ConsultaUsuarioUrlFragment;
21 | import co.quindio.sena.tutorialwebservice.fragments.ConsultarListaUsuariosFragment;
22 | import co.quindio.sena.tutorialwebservice.fragments.ConsultarUsuarioFragment;
23 | import co.quindio.sena.tutorialwebservice.fragments.ConsutarListausuarioImagenFragment;
24 | import co.quindio.sena.tutorialwebservice.fragments.DesarrolladorFragment;
25 | import co.quindio.sena.tutorialwebservice.fragments.RegistrarUsuarioFragment;
26 | import co.quindio.sena.tutorialwebservice.interfaces.IFragments;
27 |
28 | public class MainActivity extends AppCompatActivity
29 | implements NavigationView.OnNavigationItemSelectedListener,IFragments{
30 |
31 | @Override
32 | protected void onCreate(Bundle savedInstanceState) {
33 | super.onCreate(savedInstanceState);
34 | setContentView(R.layout.activity_main);
35 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
36 | setSupportActionBar(toolbar);
37 |
38 |
39 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
40 | ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
41 | this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
42 | drawer.setDrawerListener(toggle);
43 | toggle.syncState();
44 |
45 | Fragment miFragment=new BienvenidaFragment();
46 | getSupportFragmentManager().beginTransaction().replace(R.id.content_main,miFragment).commit();
47 |
48 | NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
49 | navigationView.setNavigationItemSelectedListener(this);
50 | }
51 |
52 | @Override
53 | public void onBackPressed() {
54 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
55 | if (drawer.isDrawerOpen(GravityCompat.START)) {
56 | drawer.closeDrawer(GravityCompat.START);
57 | } else {
58 | super.onBackPressed();
59 | }
60 | }
61 |
62 | @Override
63 | public boolean onCreateOptionsMenu(Menu menu) {
64 | // Inflate the menu; this adds items to the action bar if it is present.
65 | getMenuInflater().inflate(R.menu.main, menu);
66 | return true;
67 | }
68 |
69 | @Override
70 | public boolean onOptionsItemSelected(MenuItem item) {
71 | // Handle action bar item clicks here. The action bar will
72 | // automatically handle clicks on the Home/Up button, so long
73 | // as you specify a parent activity in AndroidManifest.xml.
74 | int id = item.getItemId();
75 |
76 | //noinspection SimplifiableIfStatement
77 | if (id == R.id.action_settings) {
78 | return true;
79 | }
80 |
81 | return super.onOptionsItemSelected(item);
82 | }
83 |
84 | @SuppressWarnings("StatementWithEmptyBody")
85 | @Override
86 | public boolean onNavigationItemSelected(MenuItem item) {
87 | // Handle navigation view item clicks here.
88 | int id = item.getItemId();
89 |
90 | Fragment miFragment=null;
91 | boolean fragmentSeleccionado=false;
92 |
93 | if (id == R.id.nav_inicio) {
94 | miFragment=new BienvenidaFragment();
95 | fragmentSeleccionado=true;
96 | }else if (id == R.id.nav_registro) {
97 | miFragment=new RegistrarUsuarioFragment();
98 | fragmentSeleccionado=true;
99 | } else if (id == R.id.nav_consulta_individual) {
100 | miFragment=new ConsultarUsuarioFragment();
101 | fragmentSeleccionado=true;
102 | } else if (id == R.id.nav_consulta_Url) {
103 | miFragment=new ConsultaUsuarioUrlFragment();
104 | fragmentSeleccionado=true;
105 | } else if (id == R.id.nav_consulta_gral) {
106 | miFragment=new ConsultarListaUsuariosFragment();
107 | fragmentSeleccionado=true;
108 | } else if (id == R.id.nav_consulta_gral_img) {
109 | miFragment=new ConsutarListausuarioImagenFragment();
110 | fragmentSeleccionado=true;
111 | }else if (id == R.id.nav_consulta_gral_img_url) {
112 | miFragment=new ConsultaListaUsuarioImagenUrlFragment();
113 | fragmentSeleccionado=true;
114 | } else if (id == R.id.nav_desarrollador) {
115 | miFragment=new DesarrolladorFragment();
116 | fragmentSeleccionado=true;
117 | }
118 |
119 | if (fragmentSeleccionado==true){
120 | getSupportFragmentManager().beginTransaction().replace(R.id.content_main,miFragment).commit();
121 | }
122 |
123 | DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
124 | drawer.closeDrawer(GravityCompat.START);
125 | return true;
126 | }
127 |
128 | @Override
129 | public void onFragmentInteraction(Uri uri) {
130 |
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/fragments/ConsutarListausuarioImagenFragment.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.fragments;
2 |
3 | import android.app.ProgressDialog;
4 | import android.content.Context;
5 | import android.net.Uri;
6 | import android.os.Bundle;
7 | import android.support.v4.app.Fragment;
8 | import android.support.v7.widget.LinearLayoutManager;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.view.LayoutInflater;
11 | import android.view.View;
12 | import android.view.ViewGroup;
13 | import android.widget.Toast;
14 |
15 | import com.android.volley.Request;
16 | import com.android.volley.RequestQueue;
17 | import com.android.volley.Response;
18 | import com.android.volley.VolleyError;
19 | import com.android.volley.toolbox.JsonObjectRequest;
20 | import com.android.volley.toolbox.Volley;
21 |
22 | import org.json.JSONArray;
23 | import org.json.JSONException;
24 | import org.json.JSONObject;
25 |
26 | import java.util.ArrayList;
27 |
28 | import co.quindio.sena.tutorialwebservice.R;
29 | import co.quindio.sena.tutorialwebservice.adapter.UsuariosAdapter;
30 | import co.quindio.sena.tutorialwebservice.adapter.UsuariosImagenAdapter;
31 | import co.quindio.sena.tutorialwebservice.entidades.Usuario;
32 | import co.quindio.sena.tutorialwebservice.entidades.VolleySingleton;
33 |
34 |
35 | /**
36 | * A simple {@link Fragment} subclass.
37 | * Activities that contain this fragment must implement the
38 | * {@link ConsutarListausuarioImagenFragment.OnFragmentInteractionListener} interface
39 | * to handle interaction events.
40 | * Use the {@link ConsutarListausuarioImagenFragment#newInstance} factory method to
41 | * create an instance of this fragment.
42 | */
43 | public class ConsutarListausuarioImagenFragment extends Fragment
44 | implements Response.Listener,Response.ErrorListener{
45 | // TODO: Rename parameter arguments, choose names that match
46 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
47 | private static final String ARG_PARAM1 = "param1";
48 | private static final String ARG_PARAM2 = "param2";
49 |
50 | // TODO: Rename and change types of parameters
51 | private String mParam1;
52 | private String mParam2;
53 |
54 | private OnFragmentInteractionListener mListener;
55 |
56 | RecyclerView recyclerUsuarios;
57 | ArrayList listaUsuarios;
58 |
59 | ProgressDialog dialog;
60 |
61 | // RequestQueue request;
62 | JsonObjectRequest jsonObjectRequest;
63 |
64 |
65 | public ConsutarListausuarioImagenFragment() {
66 | // Required empty public constructor
67 | }
68 |
69 | /**
70 | * Use this factory method to create a new instance of
71 | * this fragment using the provided parameters.
72 | *
73 | * @param param1 Parameter 1.
74 | * @param param2 Parameter 2.
75 | * @return A new instance of fragment ConsutarListausuarioImagenFragment.
76 | */
77 | // TODO: Rename and change types and number of parameters
78 | public static ConsutarListausuarioImagenFragment newInstance(String param1, String param2) {
79 | ConsutarListausuarioImagenFragment fragment = new ConsutarListausuarioImagenFragment();
80 | Bundle args = new Bundle();
81 | args.putString(ARG_PARAM1, param1);
82 | args.putString(ARG_PARAM2, param2);
83 | fragment.setArguments(args);
84 | return fragment;
85 | }
86 |
87 | @Override
88 | public void onCreate(Bundle savedInstanceState) {
89 | super.onCreate(savedInstanceState);
90 | if (getArguments() != null) {
91 | mParam1 = getArguments().getString(ARG_PARAM1);
92 | mParam2 = getArguments().getString(ARG_PARAM2);
93 | }
94 | }
95 |
96 | @Override
97 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
98 | Bundle savedInstanceState) {
99 | View vista= inflater.inflate(R.layout.fragment_consutar_listausuario_imagen, container, false);;
100 |
101 | listaUsuarios=new ArrayList<>();
102 |
103 | recyclerUsuarios = (RecyclerView) vista.findViewById(R.id.idRecyclerImagen);
104 | recyclerUsuarios.setLayoutManager(new LinearLayoutManager(this.getContext()));
105 | recyclerUsuarios.setHasFixedSize(true);
106 |
107 | // request= Volley.newRequestQueue(getContext());
108 |
109 | cargarWebService();
110 |
111 | return vista;
112 | }
113 |
114 | private void cargarWebService() {
115 |
116 | dialog=new ProgressDialog(getContext());
117 | dialog.setMessage("Consultando Imagenes");
118 | dialog.show();
119 |
120 | String ip=getString(R.string.ip);
121 |
122 | String url=ip+"/ejemploBDRemota/wsJSONConsultarListaImagenes.php";
123 | jsonObjectRequest=new JsonObjectRequest(Request.Method.GET,url,null,this,this);
124 | // request.add(jsonObjectRequest);
125 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(jsonObjectRequest);
126 | }
127 |
128 | @Override
129 | public void onResponse(JSONObject response) {
130 | Usuario usuario=null;
131 |
132 | JSONArray json=response.optJSONArray("usuario");
133 |
134 | try {
135 |
136 | for (int i=0;i
198 | * See the Android Training lesson Communicating with Other Fragments for more information.
201 | */
202 | public interface OnFragmentInteractionListener {
203 | // TODO: Update argument type and name
204 | void onFragmentInteraction(Uri uri);
205 | }
206 | }
207 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/fragments/ConsultarListaUsuariosFragment.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.fragments;
2 |
3 | import android.app.ProgressDialog;
4 | import android.content.Context;
5 | import android.net.Uri;
6 | import android.os.Bundle;
7 | import android.support.v4.app.Fragment;
8 | import android.support.v7.widget.LinearLayoutManager;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.util.Log;
11 | import android.view.LayoutInflater;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.Toast;
15 |
16 | import com.android.volley.Request;
17 | import com.android.volley.RequestQueue;
18 | import com.android.volley.Response;
19 | import com.android.volley.VolleyError;
20 | import com.android.volley.toolbox.JsonObjectRequest;
21 | import com.android.volley.toolbox.Volley;
22 |
23 | import org.json.JSONArray;
24 | import org.json.JSONException;
25 | import org.json.JSONObject;
26 |
27 | import java.util.ArrayList;
28 |
29 | import co.quindio.sena.tutorialwebservice.R;
30 | import co.quindio.sena.tutorialwebservice.adapter.UsuariosAdapter;
31 | import co.quindio.sena.tutorialwebservice.entidades.Usuario;
32 | import co.quindio.sena.tutorialwebservice.entidades.VolleySingleton;
33 |
34 | /**
35 | * A simple {@link Fragment} subclass.
36 | * Activities that contain this fragment must implement the
37 | * {@link ConsultarListaUsuariosFragment.OnFragmentInteractionListener} interface
38 | * to handle interaction events.
39 | * Use the {@link ConsultarListaUsuariosFragment#newInstance} factory method to
40 | * create an instance of this fragment.
41 | */
42 | public class ConsultarListaUsuariosFragment extends Fragment implements Response.Listener,Response.ErrorListener{
43 | // TODO: Rename parameter arguments, choose names that match
44 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
45 | private static final String ARG_PARAM1 = "param1";
46 | private static final String ARG_PARAM2 = "param2";
47 |
48 | // TODO: Rename and change types of parameters
49 | private String mParam1;
50 | private String mParam2;
51 |
52 | private OnFragmentInteractionListener mListener;
53 |
54 | RecyclerView recyclerUsuarios;
55 | ArrayList listaUsuarios;
56 |
57 | ProgressDialog progress;
58 |
59 | // RequestQueue request;
60 | JsonObjectRequest jsonObjectRequest;
61 |
62 |
63 | public ConsultarListaUsuariosFragment() {
64 | // Required empty public constructor
65 | }
66 |
67 | /**
68 | * Use this factory method to create a new instance of
69 | * this fragment using the provided parameters.
70 | *
71 | * @param param1 Parameter 1.
72 | * @param param2 Parameter 2.
73 | * @return A new instance of fragment ConsultarListaUsuariosFragment.
74 | */
75 | // TODO: Rename and change types and number of parameters
76 | public static ConsultarListaUsuariosFragment newInstance(String param1, String param2) {
77 | ConsultarListaUsuariosFragment fragment = new ConsultarListaUsuariosFragment();
78 | Bundle args = new Bundle();
79 | args.putString(ARG_PARAM1, param1);
80 | args.putString(ARG_PARAM2, param2);
81 | fragment.setArguments(args);
82 | return fragment;
83 | }
84 |
85 | @Override
86 | public void onCreate(Bundle savedInstanceState) {
87 | super.onCreate(savedInstanceState);
88 | if (getArguments() != null) {
89 | mParam1 = getArguments().getString(ARG_PARAM1);
90 | mParam2 = getArguments().getString(ARG_PARAM2);
91 | }
92 | }
93 |
94 | @Override
95 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
96 | Bundle savedInstanceState) {
97 | View vista=inflater.inflate(R.layout.fragment_consultar_lista_usuarios, container, false);
98 |
99 | listaUsuarios=new ArrayList<>();
100 |
101 | recyclerUsuarios= (RecyclerView) vista.findViewById(R.id.idRecycler);
102 | recyclerUsuarios.setLayoutManager(new LinearLayoutManager(this.getContext()));
103 | recyclerUsuarios.setHasFixedSize(true);
104 |
105 | // request= Volley.newRequestQueue(getContext());
106 |
107 | cargarWebService();
108 |
109 | return vista;
110 |
111 | }
112 |
113 | private void cargarWebService() {
114 |
115 | progress=new ProgressDialog(getContext());
116 | progress.setMessage("Consultando...");
117 | progress.show();
118 |
119 | String ip=getString(R.string.ip);
120 |
121 | String url=ip+"/ejemploBDRemota/wsJSONConsultarLista.php";
122 |
123 | jsonObjectRequest=new JsonObjectRequest(Request.Method.GET,url,null,this,this);
124 | // request.add(jsonObjectRequest);
125 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(jsonObjectRequest);
126 | }
127 |
128 | @Override
129 | public void onErrorResponse(VolleyError error) {
130 | Toast.makeText(getContext(), "No se puede conectar "+error.toString(), Toast.LENGTH_LONG).show();
131 | System.out.println();
132 | Log.d("ERROR: ", error.toString());
133 | progress.hide();
134 | }
135 |
136 | @Override
137 | public void onResponse(JSONObject response) {
138 | Usuario usuario=null;
139 |
140 | JSONArray json=response.optJSONArray("usuario");
141 |
142 | try {
143 |
144 | for (int i=0;i
199 | * See the Android Training lesson Communicating with Other Fragments for more information.
202 | */
203 | public interface OnFragmentInteractionListener {
204 | // TODO: Update argument type and name
205 | void onFragmentInteraction(Uri uri);
206 | }
207 | }
208 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/fragments/ConsultarUsuarioFragment.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.fragments;
2 |
3 | import android.app.ProgressDialog;
4 | import android.content.Context;
5 | import android.net.Uri;
6 | import android.os.Bundle;
7 | import android.support.v4.app.Fragment;
8 | import android.util.Log;
9 | import android.view.LayoutInflater;
10 | import android.view.View;
11 | import android.view.ViewGroup;
12 | import android.widget.Button;
13 | import android.widget.EditText;
14 | import android.widget.ImageView;
15 | import android.widget.TextView;
16 | import android.widget.Toast;
17 |
18 | import com.android.volley.Request;
19 | import com.android.volley.RequestQueue;
20 | import com.android.volley.Response;
21 | import com.android.volley.VolleyError;
22 | import com.android.volley.toolbox.JsonObjectRequest;
23 | import com.android.volley.toolbox.Volley;
24 |
25 | import org.json.JSONArray;
26 | import org.json.JSONException;
27 | import org.json.JSONObject;
28 | import org.w3c.dom.Text;
29 |
30 | import co.quindio.sena.tutorialwebservice.R;
31 | import co.quindio.sena.tutorialwebservice.entidades.Usuario;
32 | import co.quindio.sena.tutorialwebservice.entidades.VolleySingleton;
33 |
34 | /**
35 | * A simple {@link Fragment} subclass.
36 | * Activities that contain this fragment must implement the
37 | * {@link ConsultarUsuarioFragment.OnFragmentInteractionListener} interface
38 | * to handle interaction events.
39 | * Use the {@link ConsultarUsuarioFragment#newInstance} factory method to
40 | * create an instance of this fragment.
41 | */
42 | public class ConsultarUsuarioFragment extends Fragment implements Response.Listener,Response.ErrorListener{
43 | // TODO: Rename parameter arguments, choose names that match
44 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
45 | private static final String ARG_PARAM1 = "param1";
46 | private static final String ARG_PARAM2 = "param2";
47 |
48 | // TODO: Rename and change types of parameters
49 | private String mParam1;
50 | private String mParam2;
51 |
52 | private OnFragmentInteractionListener mListener;
53 |
54 | EditText campoDocumento;
55 | TextView txtNombre,txtProfesion;
56 | Button btnConsultarUsuario;
57 | ProgressDialog progreso;
58 | ImageView campoImagen;
59 |
60 | //RequestQueue request;
61 | JsonObjectRequest jsonObjectRequest;
62 |
63 | public ConsultarUsuarioFragment() {
64 | // Required empty public constructor
65 | }
66 |
67 | /**
68 | * Use this factory method to create a new instance of
69 | * this fragment using the provided parameters.
70 | *
71 | * @param param1 Parameter 1.
72 | * @param param2 Parameter 2.
73 | * @return A new instance of fragment ConsultarUsuarioFragment.
74 | */
75 | // TODO: Rename and change types and number of parameters
76 | public static ConsultarUsuarioFragment newInstance(String param1, String param2) {
77 | ConsultarUsuarioFragment fragment = new ConsultarUsuarioFragment();
78 | Bundle args = new Bundle();
79 | args.putString(ARG_PARAM1, param1);
80 | args.putString(ARG_PARAM2, param2);
81 | fragment.setArguments(args);
82 | return fragment;
83 | }
84 |
85 | @Override
86 | public void onCreate(Bundle savedInstanceState) {
87 | super.onCreate(savedInstanceState);
88 | if (getArguments() != null) {
89 | mParam1 = getArguments().getString(ARG_PARAM1);
90 | mParam2 = getArguments().getString(ARG_PARAM2);
91 | }
92 | }
93 |
94 | @Override
95 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
96 | Bundle savedInstanceState) {
97 | View vista=inflater.inflate(R.layout.fragment_consultar_usuario, container, false);
98 |
99 | campoDocumento= (EditText) vista.findViewById(R.id.campoDocumento);
100 | txtNombre= (TextView) vista.findViewById(R.id.txtNombre);
101 | txtProfesion= (TextView) vista.findViewById(R.id.txtProfesion);
102 | btnConsultarUsuario= (Button) vista.findViewById(R.id.btnConsultarUsuario);
103 | campoImagen=(ImageView) vista.findViewById(R.id.imagenId);
104 |
105 | // request= Volley.newRequestQueue(getContext());
106 |
107 | btnConsultarUsuario.setOnClickListener(new View.OnClickListener() {
108 | @Override
109 | public void onClick(View view) {
110 | cargarWebService();
111 | }
112 | });
113 |
114 |
115 | return vista;
116 | }
117 |
118 | private void cargarWebService() {
119 |
120 | progreso=new ProgressDialog(getContext());
121 | progreso.setMessage("Consultando...");
122 | progreso.show();
123 |
124 | String ip=getString(R.string.ip);
125 |
126 | String url=ip+"/ejemploBDRemota/wsJSONConsultarUsuarioImagen.php?documento="
127 | +campoDocumento.getText().toString();
128 |
129 | jsonObjectRequest=new JsonObjectRequest(Request.Method.GET,url,null,this,this);
130 | // request.add(jsonObjectRequest);
131 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(jsonObjectRequest);
132 | }
133 |
134 |
135 | @Override
136 | public void onErrorResponse(VolleyError error) {
137 | progreso.hide();
138 | Toast.makeText(getContext(),"No se pudo Consultar "+error.toString(),Toast.LENGTH_SHORT).show();
139 | Log.i("ERROR",error.toString());
140 | }
141 |
142 | @Override
143 | public void onResponse(JSONObject response) {
144 | progreso.hide();
145 |
146 | // Toast.makeText(getContext(),"Mensaje: "+response,Toast.LENGTH_SHORT).show();
147 |
148 | Usuario miUsuario=new Usuario();
149 |
150 | JSONArray json=response.optJSONArray("usuario");
151 | JSONObject jsonObject=null;
152 |
153 | try {
154 | jsonObject=json.getJSONObject(0);
155 | miUsuario.setNombre(jsonObject.optString("nombre"));
156 | miUsuario.setProfesion(jsonObject.optString("profesion"));
157 | miUsuario.setDato(jsonObject.optString("imagen"));
158 | } catch (JSONException e) {
159 | e.printStackTrace();
160 | }
161 |
162 | txtNombre.setText("Nombre :"+miUsuario.getNombre());
163 | txtProfesion.setText("Profesion :"+miUsuario.getProfesion());
164 |
165 | if (miUsuario.getImagen()!=null){
166 | campoImagen.setImageBitmap(miUsuario.getImagen());
167 | }else{
168 | campoImagen.setImageResource(R.drawable.img_base);
169 | }
170 |
171 |
172 | }
173 |
174 | // TODO: Rename method, update argument and hook method into UI event
175 | public void onButtonPressed(Uri uri) {
176 | if (mListener != null) {
177 | mListener.onFragmentInteraction(uri);
178 | }
179 | }
180 |
181 | @Override
182 | public void onAttach(Context context) {
183 | super.onAttach(context);
184 | if (context instanceof OnFragmentInteractionListener) {
185 | mListener = (OnFragmentInteractionListener) context;
186 | } else {
187 | throw new RuntimeException(context.toString()
188 | + " must implement OnFragmentInteractionListener");
189 | }
190 | }
191 |
192 | @Override
193 | public void onDetach() {
194 | super.onDetach();
195 | mListener = null;
196 | }
197 |
198 |
199 | /**
200 | * This interface must be implemented by activities that contain this
201 | * fragment to allow an interaction in this fragment to be communicated
202 | * to the activity and potentially other fragments contained in that
203 | * activity.
204 | *
205 | * See the Android Training lesson Communicating with Other Fragments for more information.
208 | */
209 | public interface OnFragmentInteractionListener {
210 | // TODO: Update argument type and name
211 | void onFragmentInteraction(Uri uri);
212 | }
213 | }
214 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/fragments/ConsultaListaUsuarioImagenUrlFragment.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.fragments;
2 |
3 | import android.app.ProgressDialog;
4 | import android.content.Context;
5 | import android.net.Uri;
6 | import android.os.Bundle;
7 | import android.support.v4.app.Fragment;
8 | import android.support.v7.widget.LinearLayoutManager;
9 | import android.support.v7.widget.RecyclerView;
10 | import android.util.Log;
11 | import android.view.LayoutInflater;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.widget.Toast;
15 |
16 | import com.android.volley.Request;
17 | import com.android.volley.RequestQueue;
18 | import com.android.volley.Response;
19 | import com.android.volley.VolleyError;
20 | import com.android.volley.toolbox.JsonObjectRequest;
21 | import com.android.volley.toolbox.Volley;
22 |
23 | import org.json.JSONArray;
24 | import org.json.JSONException;
25 | import org.json.JSONObject;
26 |
27 | import java.util.ArrayList;
28 |
29 | import co.quindio.sena.tutorialwebservice.R;
30 | import co.quindio.sena.tutorialwebservice.adapter.UsuariosImagenAdapter;
31 | import co.quindio.sena.tutorialwebservice.adapter.UsuariosImagenUrlAdapter;
32 | import co.quindio.sena.tutorialwebservice.entidades.Usuario;
33 | import co.quindio.sena.tutorialwebservice.entidades.VolleySingleton;
34 |
35 | /**
36 | * A simple {@link Fragment} subclass.
37 | * Activities that contain this fragment must implement the
38 | * {@link ConsultaListaUsuarioImagenUrlFragment.OnFragmentInteractionListener} interface
39 | * to handle interaction events.
40 | * Use the {@link ConsultaListaUsuarioImagenUrlFragment#newInstance} factory method to
41 | * create an instance of this fragment.
42 | */
43 | public class ConsultaListaUsuarioImagenUrlFragment extends Fragment implements Response.Listener,Response.ErrorListener{
44 | // TODO: Rename parameter arguments, choose names that match
45 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
46 | private static final String ARG_PARAM1 = "param1";
47 | private static final String ARG_PARAM2 = "param2";
48 |
49 | // TODO: Rename and change types of parameters
50 | private String mParam1;
51 | private String mParam2;
52 |
53 | private OnFragmentInteractionListener mListener;
54 |
55 | RecyclerView recyclerUsuarios;
56 | ArrayList listaUsuarios;
57 |
58 | ProgressDialog dialog;
59 |
60 | // RequestQueue request;
61 | JsonObjectRequest jsonObjectRequest;
62 |
63 | public ConsultaListaUsuarioImagenUrlFragment() {
64 | // Required empty public constructor
65 | }
66 |
67 | /**
68 | * Use this factory method to create a new instance of
69 | * this fragment using the provided parameters.
70 | *
71 | * @param param1 Parameter 1.
72 | * @param param2 Parameter 2.
73 | * @return A new instance of fragment ConsultaListaUsuarioImagenUrlFragment.
74 | */
75 | // TODO: Rename and change types and number of parameters
76 | public static ConsultaListaUsuarioImagenUrlFragment newInstance(String param1, String param2) {
77 | ConsultaListaUsuarioImagenUrlFragment fragment = new ConsultaListaUsuarioImagenUrlFragment();
78 | Bundle args = new Bundle();
79 | args.putString(ARG_PARAM1, param1);
80 | args.putString(ARG_PARAM2, param2);
81 | fragment.setArguments(args);
82 | return fragment;
83 | }
84 |
85 | @Override
86 | public void onCreate(Bundle savedInstanceState) {
87 | super.onCreate(savedInstanceState);
88 | if (getArguments() != null) {
89 | mParam1 = getArguments().getString(ARG_PARAM1);
90 | mParam2 = getArguments().getString(ARG_PARAM2);
91 | }
92 | }
93 |
94 | @Override
95 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
96 | Bundle savedInstanceState) {
97 | View vista= inflater.inflate(R.layout.fragment_consulta_lista_usuario_imagen_url, container, false);
98 |
99 | listaUsuarios=new ArrayList<>();
100 |
101 | recyclerUsuarios = (RecyclerView) vista.findViewById(R.id.idRecyclerImagen);
102 | recyclerUsuarios.setLayoutManager(new LinearLayoutManager(this.getContext()));
103 | recyclerUsuarios.setHasFixedSize(true);
104 |
105 | // request= Volley.newRequestQueue(getContext());
106 |
107 | cargarWebService();
108 |
109 | return vista;
110 |
111 | }
112 |
113 | private void cargarWebService() {
114 | dialog=new ProgressDialog(getContext());
115 | dialog.setMessage("Consultando Imagenes");
116 | dialog.show();
117 |
118 | String ip=getString(R.string.ip);
119 |
120 | String url=ip+"/ejemploBDRemota/wsJSONConsultarListaImagenesUrl.php";
121 | jsonObjectRequest=new JsonObjectRequest(Request.Method.GET,url,null,this,this);
122 | // request.add(jsonObjectRequest);
123 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(jsonObjectRequest);
124 | }
125 |
126 | @Override
127 | public void onResponse(JSONObject response) {
128 | Usuario usuario=null;
129 |
130 | JSONArray json=response.optJSONArray("usuario");
131 |
132 | try {
133 |
134 | for (int i=0;i
198 | * See the Android Training lesson Communicating with Other Fragments for more information.
201 | */
202 | public interface OnFragmentInteractionListener {
203 | // TODO: Update argument type and name
204 | void onFragmentInteraction(Uri uri);
205 | }
206 | }
207 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/fragments/RegistrarUsuarioFragment.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.fragments;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.ProgressDialog;
5 | import android.content.Context;
6 | import android.content.DialogInterface;
7 | import android.content.Intent;
8 | import android.content.pm.PackageManager;
9 | import android.graphics.Bitmap;
10 | import android.graphics.BitmapFactory;
11 | import android.graphics.Matrix;
12 | import android.media.MediaScannerConnection;
13 | import android.net.Uri;
14 | import android.os.Build;
15 | import android.os.Bundle;
16 | import android.os.Environment;
17 | import android.provider.MediaStore;
18 | import android.provider.Settings;
19 | import android.support.annotation.NonNull;
20 | import android.support.design.widget.Snackbar;
21 | import android.support.v4.app.Fragment;
22 | import android.support.v4.content.FileProvider;
23 | import android.support.v7.app.AlertDialog;
24 | import android.util.Base64;
25 | import android.util.Log;
26 | import android.view.LayoutInflater;
27 | import android.view.View;
28 | import android.view.ViewGroup;
29 | import android.widget.Button;
30 | import android.widget.EditText;
31 | import android.widget.ImageView;
32 | import android.widget.RelativeLayout;
33 | import android.widget.Toast;
34 |
35 | import com.android.volley.AuthFailureError;
36 | import com.android.volley.DefaultRetryPolicy;
37 | import com.android.volley.Request;
38 | import com.android.volley.RequestQueue;
39 | import com.android.volley.Response;
40 | import com.android.volley.VolleyError;
41 | import com.android.volley.toolbox.JsonObjectRequest;
42 | import com.android.volley.toolbox.StringRequest;
43 | import com.android.volley.toolbox.Volley;
44 |
45 | import org.json.JSONObject;
46 |
47 | import java.io.ByteArrayOutputStream;
48 | import java.io.File;
49 | import java.io.IOException;
50 | import java.util.HashMap;
51 | import java.util.Map;
52 |
53 | import co.quindio.sena.tutorialwebservice.R;
54 | import co.quindio.sena.tutorialwebservice.entidades.VolleySingleton;
55 |
56 | import static android.Manifest.permission.CAMERA;
57 | import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
58 |
59 | /**
60 | * A simple {@link Fragment} subclass.
61 | * Activities that contain this fragment must implement the
62 | * {@link RegistrarUsuarioFragment.OnFragmentInteractionListener} interface
63 | * to handle interaction events.
64 | * Use the {@link RegistrarUsuarioFragment#newInstance} factory method to
65 | * create an instance of this fragment.
66 | */
67 | public class RegistrarUsuarioFragment extends Fragment {
68 | // TODO: Rename parameter arguments, choose names that match
69 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
70 | private static final String ARG_PARAM1 = "param1";
71 | private static final String ARG_PARAM2 = "param2";
72 |
73 | // TODO: Rename and change types of parameters
74 | private String mParam1;
75 | private String mParam2;
76 |
77 | private OnFragmentInteractionListener mListener;
78 |
79 |
80 | private static final String CARPETA_PRINCIPAL = "misImagenesApp/";//directorio principal
81 | private static final String CARPETA_IMAGEN = "imagenes";//carpeta donde se guardan las fotos
82 | private static final String DIRECTORIO_IMAGEN = CARPETA_PRINCIPAL + CARPETA_IMAGEN;//ruta carpeta de directorios
83 | private String path;//almacena la ruta de la imagen
84 | File fileImagen;
85 | Bitmap bitmap;
86 |
87 | private final int MIS_PERMISOS = 100;
88 | private static final int COD_SELECCIONA = 10;
89 | private static final int COD_FOTO = 20;
90 |
91 | EditText campoNombre,campoDocumento,campoProfesion;
92 | Button botonRegistro,btnFoto;
93 | ImageView imgFoto;
94 | ProgressDialog progreso;
95 |
96 | RelativeLayout layoutRegistrar;//permisos
97 |
98 | // RequestQueue request;
99 | JsonObjectRequest jsonObjectRequest;
100 |
101 | StringRequest stringRequest;
102 |
103 | public RegistrarUsuarioFragment() {
104 | // Required empty public constructor
105 | }
106 |
107 | /**
108 | * Use this factory method to create a new instance of
109 | * this fragment using the provided parameters.
110 | *
111 | * @param param1 Parameter 1.
112 | * @param param2 Parameter 2.
113 | * @return A new instance of fragment RegistrarUsuarioFragment.
114 | */
115 | // TODO: Rename and change types and number of parameters
116 | public static RegistrarUsuarioFragment newInstance(String param1, String param2) {
117 | RegistrarUsuarioFragment fragment = new RegistrarUsuarioFragment();
118 | Bundle args = new Bundle();
119 | args.putString(ARG_PARAM1, param1);
120 | args.putString(ARG_PARAM2, param2);
121 | fragment.setArguments(args);
122 | return fragment;
123 | }
124 |
125 | @Override
126 | public void onCreate(Bundle savedInstanceState) {
127 | super.onCreate(savedInstanceState);
128 | if (getArguments() != null) {
129 | mParam1 = getArguments().getString(ARG_PARAM1);
130 | mParam2 = getArguments().getString(ARG_PARAM2);
131 | }
132 | }
133 |
134 | @Override
135 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
136 | Bundle savedInstanceState) {
137 |
138 | View vista=inflater.inflate(R.layout.fragment_registrar_usuario, container, false);
139 | campoDocumento= (EditText) vista.findViewById(R.id.campoDoc);
140 | campoNombre= (EditText) vista.findViewById(R.id.campoNombre);
141 | campoProfesion= (EditText) vista.findViewById(R.id.campoProfesion);
142 | botonRegistro= (Button) vista.findViewById(R.id.btnRegistrar);
143 | btnFoto=(Button)vista.findViewById(R.id.btnFoto);
144 |
145 | imgFoto=(ImageView)vista.findViewById(R.id.imgFoto);
146 |
147 |
148 | layoutRegistrar= (RelativeLayout) vista.findViewById(R.id.idLayoutRegistrar);
149 |
150 | // request= Volley.newRequestQueue(getContext());
151 |
152 | //Permisos
153 | if(solicitaPermisosVersionesSuperiores()){
154 | btnFoto.setEnabled(true);
155 | }else{
156 | btnFoto.setEnabled(false);
157 | }
158 |
159 |
160 | botonRegistro.setOnClickListener(new View.OnClickListener() {
161 | @Override
162 | public void onClick(View view) {
163 | cargarWebService();
164 | }
165 | });
166 |
167 | btnFoto.setOnClickListener(new View.OnClickListener() {
168 | @Override
169 | public void onClick(View view) {
170 | mostrarDialogOpciones();
171 | }
172 | });
173 |
174 | return vista;
175 | }
176 |
177 | private void mostrarDialogOpciones() {
178 | final CharSequence[] opciones={"Tomar Foto","Elegir de Galeria","Cancelar"};
179 | final AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
180 | builder.setTitle("Elige una Opción");
181 | builder.setItems(opciones, new DialogInterface.OnClickListener() {
182 | @Override
183 | public void onClick(DialogInterface dialogInterface, int i) {
184 | if (opciones[i].equals("Tomar Foto")){
185 | abriCamara();
186 | }else{
187 | if (opciones[i].equals("Elegir de Galeria")){
188 | Intent intent=new Intent(Intent.ACTION_PICK,
189 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
190 | intent.setType("image/");
191 | startActivityForResult(intent.createChooser(intent,"Seleccione"),COD_SELECCIONA);
192 | }else{
193 | dialogInterface.dismiss();
194 | }
195 | }
196 | }
197 | });
198 | builder.show();
199 | }
200 |
201 | private void abriCamara() {
202 | File miFile=new File(Environment.getExternalStorageDirectory(),DIRECTORIO_IMAGEN);
203 | boolean isCreada=miFile.exists();
204 |
205 | if(isCreada==false){
206 | isCreada=miFile.mkdirs();
207 | }
208 |
209 | if(isCreada==true){
210 | Long consecutivo= System.currentTimeMillis()/1000;
211 | String nombre=consecutivo.toString()+".jpg";
212 |
213 | path=Environment.getExternalStorageDirectory()+File.separator+DIRECTORIO_IMAGEN
214 | +File.separator+nombre;//indicamos la ruta de almacenamiento
215 |
216 | fileImagen=new File(path);
217 |
218 | Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
219 | intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(fileImagen));
220 |
221 | ////
222 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
223 | {
224 | String authorities=getContext().getPackageName()+".provider";
225 | Uri imageUri= FileProvider.getUriForFile(getContext(),authorities,fileImagen);
226 | intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
227 | }else
228 | {
229 | intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileImagen));
230 | }
231 | startActivityForResult(intent,COD_FOTO);
232 |
233 | ////
234 |
235 | }
236 |
237 | }
238 |
239 |
240 | @Override
241 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
242 | super.onActivityResult(requestCode, resultCode, data);
243 |
244 | switch (requestCode){
245 | case COD_SELECCIONA:
246 | Uri miPath=data.getData();
247 | imgFoto.setImageURI(miPath);
248 |
249 | try {
250 | bitmap=MediaStore.Images.Media.getBitmap(getContext().getContentResolver(),miPath);
251 | imgFoto.setImageBitmap(bitmap);
252 | } catch (IOException e) {
253 | e.printStackTrace();
254 | }
255 |
256 | break;
257 | case COD_FOTO:
258 | MediaScannerConnection.scanFile(getContext(), new String[]{path}, null,
259 | new MediaScannerConnection.OnScanCompletedListener() {
260 | @Override
261 | public void onScanCompleted(String path, Uri uri) {
262 | Log.i("Path",""+path);
263 | }
264 | });
265 |
266 | bitmap= BitmapFactory.decodeFile(path);
267 | imgFoto.setImageBitmap(bitmap);
268 |
269 | break;
270 | }
271 | bitmap=redimensionarImagen(bitmap,600,800);
272 | }
273 |
274 | private Bitmap redimensionarImagen(Bitmap bitmap, float anchoNuevo, float altoNuevo) {
275 |
276 | int ancho=bitmap.getWidth();
277 | int alto=bitmap.getHeight();
278 |
279 | if(ancho>anchoNuevo || alto>altoNuevo){
280 | float escalaAncho=anchoNuevo/ancho;
281 | float escalaAlto= altoNuevo/alto;
282 |
283 | Matrix matrix=new Matrix();
284 | matrix.postScale(escalaAncho,escalaAlto);
285 |
286 | return Bitmap.createBitmap(bitmap,0,0,ancho,alto,matrix,false);
287 |
288 | }else{
289 | return bitmap;
290 | }
291 |
292 |
293 | }
294 |
295 |
296 | //permisos
297 | ////////////////
298 |
299 | private boolean solicitaPermisosVersionesSuperiores() {
300 | if (Build.VERSION.SDK_INT() {
386 |
387 | @Override
388 | public void onResponse(String response) {
389 | progreso.hide();
390 |
391 | if (response.trim().equalsIgnoreCase("registra")){
392 | campoNombre.setText("");
393 | campoDocumento.setText("");
394 | campoProfesion.setText("");
395 | Toast.makeText(getContext(),"Se ha registrado con exito",Toast.LENGTH_SHORT).show();
396 | }else{
397 | Toast.makeText(getContext(),"No se ha registrado ",Toast.LENGTH_SHORT).show();
398 | Log.i("RESPUESTA: ",""+response);
399 | }
400 |
401 | }
402 | }, new Response.ErrorListener() {
403 | @Override
404 | public void onErrorResponse(VolleyError error) {
405 | Toast.makeText(getContext(),"No se ha podido conectar",Toast.LENGTH_SHORT).show();
406 | progreso.hide();
407 | }
408 | }){
409 | @Override
410 | protected Map getParams() throws AuthFailureError {
411 | String documento=campoDocumento.getText().toString();
412 | String nombre=campoNombre.getText().toString();
413 | String profesion=campoProfesion.getText().toString();
414 |
415 | String imagen=convertirImgString(bitmap);
416 |
417 | Map parametros=new HashMap<>();
418 | parametros.put("documento",documento);
419 | parametros.put("nombre",nombre);
420 | parametros.put("profesion",profesion);
421 | parametros.put("imagen",imagen);
422 |
423 | return parametros;
424 | }
425 | };
426 | //request.add(stringRequest);
427 | stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
428 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(stringRequest);
429 | }
430 |
431 | private String convertirImgString(Bitmap bitmap) {
432 |
433 | ByteArrayOutputStream array=new ByteArrayOutputStream();
434 | bitmap.compress(Bitmap.CompressFormat.JPEG,100,array);
435 | byte[] imagenByte=array.toByteArray();
436 | String imagenString= Base64.encodeToString(imagenByte,Base64.DEFAULT);
437 |
438 | return imagenString;
439 | }
440 |
441 |
442 | // TODO: Rename method, update argument and hook method into UI event
443 | public void onButtonPressed(Uri uri) {
444 | if (mListener != null) {
445 | mListener.onFragmentInteraction(uri);
446 | }
447 | }
448 |
449 | @Override
450 | public void onAttach(Context context) {
451 | super.onAttach(context);
452 | if (context instanceof OnFragmentInteractionListener) {
453 | mListener = (OnFragmentInteractionListener) context;
454 | } else {
455 | throw new RuntimeException(context.toString()
456 | + " must implement OnFragmentInteractionListener");
457 | }
458 | }
459 |
460 | @Override
461 | public void onDetach() {
462 | super.onDetach();
463 | mListener = null;
464 | }
465 |
466 |
467 |
468 | /**
469 | * This interface must be implemented by activities that contain this
470 | * fragment to allow an interaction in this fragment to be communicated
471 | * to the activity and potentially other fragments contained in that
472 | * activity.
473 | *
474 | * See the Android Training lesson Communicating with Other Fragments for more information.
477 | */
478 | public interface OnFragmentInteractionListener {
479 | // TODO: Update argument type and name
480 | void onFragmentInteraction(Uri uri);
481 | }
482 | }
483 |
--------------------------------------------------------------------------------
/app/src/main/java/co/quindio/sena/tutorialwebservice/fragments/ConsultaUsuarioUrlFragment.java:
--------------------------------------------------------------------------------
1 | package co.quindio.sena.tutorialwebservice.fragments;
2 |
3 | import android.app.ProgressDialog;
4 | import android.content.Context;
5 | import android.content.DialogInterface;
6 | import android.content.Intent;
7 | import android.content.pm.PackageManager;
8 | import android.graphics.Bitmap;
9 | import android.graphics.BitmapFactory;
10 | import android.graphics.Matrix;
11 | import android.media.MediaScannerConnection;
12 | import android.net.Uri;
13 | import android.os.Build;
14 | import android.os.Bundle;
15 | import android.os.Environment;
16 | import android.provider.MediaStore;
17 | import android.provider.Settings;
18 | import android.support.annotation.NonNull;
19 | import android.support.v4.app.Fragment;
20 | import android.support.v4.content.FileProvider;
21 | import android.support.v7.app.AlertDialog;
22 | import android.util.Base64;
23 | import android.util.Log;
24 | import android.view.LayoutInflater;
25 | import android.view.View;
26 | import android.view.ViewGroup;
27 | import android.widget.Button;
28 | import android.widget.EditText;
29 | import android.widget.ImageButton;
30 | import android.widget.ImageView;
31 | import android.widget.TextView;
32 | import android.widget.Toast;
33 |
34 | import com.android.volley.AuthFailureError;
35 | import com.android.volley.Request;
36 | import com.android.volley.RequestQueue;
37 | import com.android.volley.Response;
38 | import com.android.volley.VolleyError;
39 | import com.android.volley.toolbox.ImageRequest;
40 | import com.android.volley.toolbox.JsonObjectRequest;
41 | import com.android.volley.toolbox.StringRequest;
42 | import com.android.volley.toolbox.Volley;
43 |
44 | import org.json.JSONArray;
45 | import org.json.JSONException;
46 | import org.json.JSONObject;
47 |
48 | import java.io.ByteArrayOutputStream;
49 | import java.io.File;
50 | import java.io.IOException;
51 | import java.util.HashMap;
52 | import java.util.Map;
53 |
54 | import co.quindio.sena.tutorialwebservice.R;
55 | import co.quindio.sena.tutorialwebservice.entidades.Usuario;
56 | import co.quindio.sena.tutorialwebservice.entidades.VolleySingleton;
57 |
58 | import static android.Manifest.permission.CAMERA;
59 | import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
60 |
61 | /**
62 | * A simple {@link Fragment} subclass.
63 | * Activities that contain this fragment must implement the
64 | * {@link ConsultaUsuarioUrlFragment.OnFragmentInteractionListener} interface
65 | * to handle interaction events.
66 | * Use the {@link ConsultaUsuarioUrlFragment#newInstance} factory method to
67 | * create an instance of this fragment.
68 | */
69 | public class ConsultaUsuarioUrlFragment extends Fragment {
70 | // TODO: Rename parameter arguments, choose names that match
71 | // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
72 | private static final String ARG_PARAM1 = "param1";
73 | private static final String ARG_PARAM2 = "param2";
74 |
75 | // TODO: Rename and change types of parameters
76 | private String mParam1;
77 | private String mParam2;
78 |
79 | private OnFragmentInteractionListener mListener;
80 |
81 | EditText txtDocumento;
82 | EditText etiNombre;//SE MODIFICA
83 | EditText etiProfesion;//SE MODIFICA
84 | ProgressDialog pDialog;
85 | ImageButton btnConsultar;//SE MODIFICA
86 | ImageView campoImagen;
87 | Button btnActualizar, btnEliminar;//SE MODIFICA
88 |
89 | //SE MODIFICA
90 | private final int MIS_PERMISOS = 100;
91 | private static final int COD_SELECCIONA = 10;
92 | private static final int COD_FOTO = 20;
93 |
94 | private static final String CARPETA_PRINCIPAL = "misImagenesApp/";//directorio principal
95 | private static final String CARPETA_IMAGEN = "imagenes";//carpeta donde se guardan las fotos
96 | private static final String DIRECTORIO_IMAGEN = CARPETA_PRINCIPAL + CARPETA_IMAGEN;//ruta carpeta de directorios
97 | private String path;//almacena la ruta de la imagen
98 | File fileImagen;
99 | Bitmap bitmap;
100 | //
101 |
102 |
103 | //RequestQueue request;
104 | JsonObjectRequest jsonObjectRequest;
105 | StringRequest stringRequest;//SE MODIFICA
106 |
107 | public ConsultaUsuarioUrlFragment() {
108 | // Required empty public constructor
109 | }
110 |
111 | /**
112 | * Use this factory method to create a new instance of
113 | * this fragment using the provided parameters.
114 | *
115 | * @param param1 Parameter 1.
116 | * @param param2 Parameter 2.
117 | * @return A new instance of fragment ConsultaUsuarioUrlFragment.
118 | */
119 | // TODO: Rename and change types and number of parameters
120 | public static ConsultaUsuarioUrlFragment newInstance(String param1, String param2) {
121 | ConsultaUsuarioUrlFragment fragment = new ConsultaUsuarioUrlFragment();
122 | Bundle args = new Bundle();
123 | args.putString(ARG_PARAM1, param1);
124 | args.putString(ARG_PARAM2, param2);
125 | fragment.setArguments(args);
126 | return fragment;
127 | }
128 |
129 | @Override
130 | public void onCreate(Bundle savedInstanceState) {
131 | super.onCreate(savedInstanceState);
132 | if (getArguments() != null) {
133 | mParam1 = getArguments().getString(ARG_PARAM1);
134 | mParam2 = getArguments().getString(ARG_PARAM2);
135 | }
136 | }
137 |
138 | @Override
139 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
140 | Bundle savedInstanceState) {
141 | View vista= inflater.inflate(R.layout.fragment_consulta_usuario_url, container, false);
142 |
143 | txtDocumento= (EditText) vista.findViewById(R.id.campoDocumento);
144 | etiNombre= (EditText) vista.findViewById(R.id.txtNombre);
145 | etiProfesion= (EditText) vista.findViewById(R.id.txtProfesion);
146 | btnConsultar= (ImageButton) vista.findViewById(R.id.btnConsultarUsuario);//SE MODIFICA
147 | campoImagen= (ImageView) vista.findViewById(R.id.imagenId);
148 | btnActualizar=(Button) vista.findViewById(R.id.btnActualizar);//SE MODIFICA
149 | btnEliminar=(Button) vista.findViewById(R.id.btnEliminar);//SE MODIFICA
150 |
151 |
152 | // request= Volley.newRequestQueue(getContext());
153 |
154 | btnConsultar.setOnClickListener(new View.OnClickListener() {
155 | @Override
156 | public void onClick(View view) {
157 | cargarWebService();
158 | }
159 | });
160 |
161 | //SE MODIFICA
162 | ////////////////////////////////////////////////////////////
163 |
164 | //Permisos
165 | if(solicitaPermisosVersionesSuperiores()){
166 | campoImagen.setEnabled(true);
167 | }else{
168 | campoImagen.setEnabled(false);
169 | }
170 |
171 | //evento imagen
172 | campoImagen.setOnClickListener(new View.OnClickListener() {
173 | @Override
174 | public void onClick(View view) {
175 | mostrarDialogOpciones();
176 | }
177 | });
178 |
179 | //eventos botones
180 | btnActualizar.setOnClickListener(new View.OnClickListener() {
181 | @Override
182 | public void onClick(View view) {
183 | webServiceActualizar();
184 | }
185 | });
186 |
187 | btnEliminar.setOnClickListener(new View.OnClickListener() {
188 | @Override
189 | public void onClick(View view) {
190 | webServiceEliminar();
191 | }
192 | });
193 |
194 | ///////////////////////////////////////////////////////////
195 |
196 | return vista;
197 | }
198 |
199 |
200 | private void cargarWebService() {
201 | pDialog=new ProgressDialog(getContext());
202 | pDialog.setMessage("Cargando...");
203 | pDialog.show();
204 |
205 | final String ip=getString(R.string.ip);
206 |
207 | String url=ip+"/ejemploBDRemota/wsJSONConsultarUsuarioUrl.php?documento="+txtDocumento.getText().toString();
208 |
209 | jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener() {
210 | @Override
211 | public void onResponse(JSONObject response) {
212 | pDialog.hide();
213 |
214 | Usuario miUsuario=new Usuario();
215 |
216 | JSONArray json=response.optJSONArray("usuario");
217 | JSONObject jsonObject=null;
218 |
219 | try {
220 | jsonObject=json.getJSONObject(0);
221 | miUsuario.setNombre(jsonObject.optString("nombre"));
222 | miUsuario.setProfesion(jsonObject.optString("profesion"));
223 | miUsuario.setRutaImagen(jsonObject.optString("ruta_imagen"));
224 | } catch (JSONException e) {
225 | e.printStackTrace();
226 | }
227 |
228 | etiNombre.setText(miUsuario.getNombre());//SE MODIFICA
229 | etiProfesion.setText(miUsuario.getProfesion());//SE MODIFICA
230 |
231 | String urlImagen=ip+"/ejemploBDRemota/"+miUsuario.getRutaImagen();
232 | //Toast.makeText(getContext(), "url "+urlImagen, Toast.LENGTH_LONG).show();
233 | cargarWebServiceImagen(urlImagen);
234 | }
235 | }, new Response.ErrorListener() {
236 | @Override
237 | public void onErrorResponse(VolleyError error) {
238 | Toast.makeText(getContext(), "No se puede conectar "+error.toString(), Toast.LENGTH_LONG).show();
239 | System.out.println();
240 | pDialog.hide();
241 | Log.d("ERROR: ", error.toString());
242 | }
243 | });
244 |
245 | // request.add(jsonObjectRequest);
246 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(jsonObjectRequest);
247 | }
248 |
249 | private void cargarWebServiceImagen(String urlImagen) {
250 | urlImagen=urlImagen.replace(" ","%20");
251 |
252 | ImageRequest imageRequest=new ImageRequest(urlImagen, new Response.Listener() {
253 | @Override
254 | public void onResponse(Bitmap response) {
255 | bitmap=response;//SE MODIFICA
256 | campoImagen.setImageBitmap(response);
257 | }
258 | }, 0, 0, ImageView.ScaleType.CENTER, null, new Response.ErrorListener() {
259 | @Override
260 | public void onErrorResponse(VolleyError error) {
261 | Toast.makeText(getContext(),"Error al cargar la imagen",Toast.LENGTH_SHORT).show();
262 | Log.i("ERROR IMAGEN","Response -> "+error);
263 | }
264 | });
265 | // request.add(imageRequest);
266 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(imageRequest);
267 | }
268 |
269 | //SE MODIFICA
270 | //Llamado WebServices, permisos y logica imagen
271 | ////////////////
272 |
273 |
274 | private void webServiceActualizar() {
275 | pDialog=new ProgressDialog(getContext());
276 | pDialog.setMessage("Cargando...");
277 | pDialog.show();
278 |
279 | String ip=getString(R.string.ip);
280 |
281 | String url=ip+"/ejemploBDRemota/wsJSONUpdateMovil.php?";
282 |
283 | stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener() {
284 | @Override
285 | public void onResponse(String response) {
286 | pDialog.hide();
287 |
288 | if (response.trim().equalsIgnoreCase("actualiza")){
289 | // etiNombre.setText("");
290 | // txtDocumento.setText("");
291 | // etiProfesion.setText("");
292 | Toast.makeText(getContext(),"Se ha Actualizado con exito",Toast.LENGTH_SHORT).show();
293 | }else{
294 | Toast.makeText(getContext(),"No se ha Actualizado ",Toast.LENGTH_SHORT).show();
295 | Log.i("RESPUESTA: ",""+response);
296 | }
297 |
298 | }
299 | }, new Response.ErrorListener() {
300 | @Override
301 | public void onErrorResponse(VolleyError error) {
302 | Toast.makeText(getContext(),"No se ha podido conectar",Toast.LENGTH_SHORT).show();
303 | pDialog.hide();
304 | }
305 | }){
306 | @Override
307 | protected Map getParams() throws AuthFailureError {
308 | String documento=txtDocumento.getText().toString();
309 | String nombre=etiNombre.getText().toString();
310 | String profesion=etiProfesion.getText().toString();
311 |
312 | String imagen=convertirImgString(bitmap);
313 |
314 | Map parametros=new HashMap<>();
315 | parametros.put("documento",documento);
316 | parametros.put("nombre",nombre);
317 | parametros.put("profesion",profesion);
318 | parametros.put("imagen",imagen);
319 |
320 | return parametros;
321 | }
322 | };
323 | //request.add(stringRequest);
324 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(stringRequest);
325 | }
326 |
327 | private String convertirImgString(Bitmap bitmap) {
328 |
329 | ByteArrayOutputStream array=new ByteArrayOutputStream();
330 | bitmap.compress(Bitmap.CompressFormat.JPEG,100,array);
331 | byte[] imagenByte=array.toByteArray();
332 | String imagenString= Base64.encodeToString(imagenByte,Base64.DEFAULT);
333 |
334 | return imagenString;
335 | }
336 |
337 | private void webServiceEliminar() {
338 | pDialog=new ProgressDialog(getContext());
339 | pDialog.setMessage("Cargando...");
340 | pDialog.show();
341 |
342 | String ip=getString(R.string.ip);
343 |
344 | String url=ip+"/ejemploBDRemota/wsJSONDeleteMovil.php?documento="+txtDocumento.getText().toString();
345 |
346 | stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener() {
347 | @Override
348 | public void onResponse(String response) {
349 | pDialog.hide();
350 |
351 | if (response.trim().equalsIgnoreCase("elimina")){
352 | etiNombre.setText("");
353 | txtDocumento.setText("");
354 | etiProfesion.setText("");
355 | campoImagen.setImageResource(R.drawable.img_base);
356 | Toast.makeText(getContext(),"Se ha Eliminado con exito",Toast.LENGTH_SHORT).show();
357 | }else{
358 | if (response.trim().equalsIgnoreCase("noExiste")){
359 | Toast.makeText(getContext(),"No se encuentra la persona ",Toast.LENGTH_SHORT).show();
360 | Log.i("RESPUESTA: ",""+response);
361 | }else{
362 | Toast.makeText(getContext(),"No se ha Eliminado ",Toast.LENGTH_SHORT).show();
363 | Log.i("RESPUESTA: ",""+response);
364 | }
365 |
366 | }
367 |
368 | }
369 | }, new Response.ErrorListener() {
370 | @Override
371 | public void onErrorResponse(VolleyError error) {
372 | Toast.makeText(getContext(),"No se ha podido conectar",Toast.LENGTH_SHORT).show();
373 | pDialog.hide();
374 | }
375 | });
376 | //request.add(stringRequest);
377 | VolleySingleton.getIntanciaVolley(getContext()).addToRequestQueue(stringRequest);
378 | }
379 |
380 | private void mostrarDialogOpciones() {
381 | final CharSequence[] opciones={"Tomar Foto","Elegir de Galeria","Cancelar"};
382 | final AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
383 | builder.setTitle("Elige una Opción");
384 | builder.setItems(opciones, new DialogInterface.OnClickListener() {
385 | @Override
386 | public void onClick(DialogInterface dialogInterface, int i) {
387 | if (opciones[i].equals("Tomar Foto")){
388 | abriCamara();
389 | }else{
390 | if (opciones[i].equals("Elegir de Galeria")){
391 | Intent intent=new Intent(Intent.ACTION_PICK,
392 | MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
393 | intent.setType("image/");
394 | startActivityForResult(intent.createChooser(intent,"Seleccione"),COD_SELECCIONA);
395 | }else{
396 | dialogInterface.dismiss();
397 | }
398 | }
399 | }
400 | });
401 | builder.show();
402 | }
403 |
404 | private void abriCamara() {
405 | File miFile=new File(Environment.getExternalStorageDirectory(),DIRECTORIO_IMAGEN);
406 | boolean isCreada=miFile.exists();
407 |
408 | if(isCreada==false){
409 | isCreada=miFile.mkdirs();
410 | }
411 |
412 | if(isCreada==true){
413 | Long consecutivo= System.currentTimeMillis()/1000;
414 | String nombre=consecutivo.toString()+".jpg";
415 |
416 | path=Environment.getExternalStorageDirectory()+File.separator+DIRECTORIO_IMAGEN
417 | +File.separator+nombre;//indicamos la ruta de almacenamiento
418 |
419 | fileImagen=new File(path);
420 |
421 | Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
422 | intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(fileImagen));
423 |
424 | ////
425 | if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
426 | {
427 | String authorities=getContext().getPackageName()+".provider";
428 | Uri imageUri= FileProvider.getUriForFile(getContext(),authorities,fileImagen);
429 | intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
430 | }else
431 | {
432 | intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fileImagen));
433 | }
434 | startActivityForResult(intent,COD_FOTO);
435 |
436 | ////
437 |
438 | }
439 |
440 | }
441 |
442 | @Override
443 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
444 | super.onActivityResult(requestCode, resultCode, data);
445 |
446 | switch (requestCode){
447 | case COD_SELECCIONA:
448 | Uri miPath=data.getData();
449 | campoImagen.setImageURI(miPath);
450 |
451 | try {
452 | bitmap=MediaStore.Images.Media.getBitmap(getContext().getContentResolver(),miPath);
453 | campoImagen.setImageBitmap(bitmap);
454 | } catch (IOException e) {
455 | e.printStackTrace();
456 | }
457 |
458 | break;
459 | case COD_FOTO:
460 | MediaScannerConnection.scanFile(getContext(), new String[]{path}, null,
461 | new MediaScannerConnection.OnScanCompletedListener() {
462 | @Override
463 | public void onScanCompleted(String path, Uri uri) {
464 | Log.i("Path",""+path);
465 | }
466 | });
467 |
468 | bitmap= BitmapFactory.decodeFile(path);
469 | campoImagen.setImageBitmap(bitmap);
470 |
471 | break;
472 | }
473 | bitmap=redimensionarImagen(bitmap,600,800);
474 | }
475 |
476 | private Bitmap redimensionarImagen(Bitmap bitmap, float anchoNuevo, float altoNuevo) {
477 |
478 | int ancho=bitmap.getWidth();
479 | int alto=bitmap.getHeight();
480 |
481 | if(ancho>anchoNuevo || alto>altoNuevo){
482 | float escalaAncho=anchoNuevo/ancho;
483 | float escalaAlto= altoNuevo/alto;
484 |
485 | Matrix matrix=new Matrix();
486 | matrix.postScale(escalaAncho,escalaAlto);
487 |
488 | return Bitmap.createBitmap(bitmap,0,0,ancho,alto,matrix,false);
489 |
490 | }else{
491 | return bitmap;
492 | }
493 |
494 | }
495 |
496 | //PERMISOS
497 | private boolean solicitaPermisosVersionesSuperiores() {
498 | if (Build.VERSION.SDK_INT
601 | * See the Android Training lesson Communicating with Other Fragments for more information.
604 | */
605 | public interface OnFragmentInteractionListener {
606 | // TODO: Update argument type and name
607 | void onFragmentInteraction(Uri uri);
608 | }
609 | }
610 |
--------------------------------------------------------------------------------