Sunteți pe pagina 1din 12

L A B O R ATO R I O D I R I G I D O 8

Servicios Web
15 de noviembre de 2015

ALUMNO :
SARAVIA TASAYCO JUAN EMILIO
PROFESOR :
JOSE MANUEL CASTILLO CARA
CURSO :
PROGRAMACION DE DISPOSITIVOS MOVILES

ndice

ndice
1
2
3
4
5
6
7

Actividad 1
Actividad 2
Actividad 3
Actividad 4
Actividad 5
Actividad 6
Practica

3
5
5
6
9
9
11

actividad 1

actividad 1
1. Entramos en www.hostinger.com.ar y creamos nuestra cuenta.
2. Una vez creada vamos a administrar y luego en el apartado
Base de datos pulsamos Base de datos MySQL. Le ponemos
un nombre de BBDD, usuario y contrasea.
a) Estos datos de usuario y BBDD se tendrn que poner en el
fichero db_config.php
3. Verificamos que est creada la BDD.
4. Abrimos el PHPMyAdmin del lado izquierdo.
a) Creamos la tabla empresa que puede verificarlo en el
fichero db_get_allempresas.
b) Creamos las columnas que pone el fichero siendo:
1) id clave primaria y haciendo check en la columna A_1
2) Cotejamiento de nombre ponemos ut8_spanish...
3) A nombre le ponemos una longitud.
5. Una vez creada aadimos 3 registros de prueba.
6. Ahora vamos a Administracion de archivos 2 de nuestro
panel y lo instalamos.
7. En Public_html creamos un directorio que se llame
laboratorio8.
a) En dicha carpeta introducimos los tres archivos. Tenga en
cuenta que puede que se hayan modificado segn los datos
de acceso y tabla que hayas creado.
b) Explique los ficheros PHP.
8. Para verificar nuestro servidor escribimos la url debiendo salir
todos los registros insertados previamente.
a) www.dominio.sub/laboratorio8/get_all_empresas.php

actividad 1

actividad 2

actividad 2
1. Ingresar los permisos de conexin
<usespermission android : name= " android . permission . INTERNET" />

actividad 3
1. Creamos una nueva clase JAVA y le llamamos JSONParser.
2. Creamos los atributos y el constructor.
static
static
static
public
}

InputStream i s = n u l l ;
JSONObject j O b j = n u l l ;
String json = " " ;
JSONParser ( ) {

3. Creamos el mtodo obtener un JSON desde la url haciendo


mediante el mtodo HTTP POST o GET. No se limite a copiar
pegar, comenta el cdigo.
public JSONObject makeHttpRequest ( S t r i n g u r l ,
S t r i n g method , L i s t params ) {
try {
i f ( method == "POST" ) {
D e f a u l t H t t p C l i e n t h t t p C l i e n t = new
DefaultHttpClient ( ) ;
HttpPost h t t p P o s t = new HttpPost ( u r l ) ;
h t t p P o s t . s e t E n t i t y (new
UrlEncodedFormEntity ( params ) ) ;
HttpResponse httpResponse =
httpClient . execute ( httpPost ) ;
H t t p E n t i t y h t t p E n t i t y = httpResponse . g e t E n t i t y ( ) ;
i s = h t t p E n t i t y . getContent ( ) ;
} e l s e i f ( method == "GET" ) {
DefaultHttpClient httpClient =
new D e f a u l t H t t p C l i e n t ( ) ;
S t r i n g paramString =
URLEncodedUtils . format ( params , " u t f 8" ) ;
u r l += " ? " + paramString ;
HttpGet h t tp Ge t = new HttpGet ( u r l ) ;
HttpResponse httpResponse =
h t t p C l i e n t . e x e c u t e ( ht tp G et ) ;
H t t p E n t i t y h t t p E n t i t y = httpResponse . g e t E n t i t y ( ) ;
i s = h t t p E n t i t y . getContent ( ) ;
}

actividad 4

} c a t c h ( UnsupportedEncodingException e ) {
e . printStackTrace ( ) ;
} catch ( ClientProtocolException e ) {
e . printStackTrace ( ) ;
} c a t c h ( IOException e ) {
e . printStackTrace ( ) ;
}
try {
BufferedReader r e a d e r = new BufferedReader (
new InputStreamReader ( i s , " i s o 88591 " ) , 8 ) ;
S t r i n g B u i l d e r sb = new S t r i n g B u i l d e r ( ) ;
Stri ng l i n e = null ;
while ( ( l i n e = r e a d e r . readLine ( ) ) ! = n u l l ) {
sb . append ( l i n e + " \n " ) ;
}
is . close ( ) ;
j s o n = sb . t o S t r i n g ( ) ;
} catch ( Exception e ) {
Log . e ( " B u f f e r E r r o r " , " E r r o r c o n v e r t r e s u l t " + e . t o S t r i n g ( ) ) ;
}
try {
j O b j = new JSONObject ( j s o n ) ;
} c a t c h ( JSONException e ) {
Log . e ( "JSON P a r s e r " , " E r r o r p a r s i n g data " + e . t o S t r i n g ( ) ) ;
}
return jObj ;
}

actividad 4
1. Para todas las actividades no se olvide de comentar el cdigo
2. Agregamos los atributos, tanto el objeto JSON como los que
vayamos a necesitar de comunicacin con el servidor.
p r i v a t e P r o g r e s s D i a l o g pDialog ;
JSONParser j P a r s e r = new JSONParser ( ) ;
A r r a y L i s t <HashMap< S t r i n g , S t r i n g >> empresaList ;
private s t a t i c String url_all_empresas =
" h t t p :// m i d i r e c c i o n / l a b o r a t o r i o 8 / g e t _ a l l _ e m p r e s a s . php " ;
p r i v a t e s t a t i c f i n a l S t r i n g TAG_SUCCESS = " s u c c e s s " ;
p r i v a t e s t a t i c f i n a l S t r i n g TAG_PRODUCTS = " empresas " ;
p r i v a t e s t a t i c f i n a l S t r i n g TAG_ID = " i d " ;
p r i v a t e s t a t i c f i n a l S t r i n g TAG_NOMBRE = " nombre " ;
JSONArray products = n u l l ;
ListView l i s t a ;

actividad 4

3. El mtodo onCreate tendr:


public void onCreate ( Bundle s a v e d I n s t a n c e S t a t e ) {
super . onCreate ( s a v e d I n s t a n c e S t a t e ) ;
setContentView ( R . l a y o u t . a c t i v i t y _ m y ) ;
empresaList = new A r r a y L i s t <HashMap< S t r i n g , S t r i n g > > ( ) ;
new LoadAllProducts ( ) . e x e c u t e ( ) ;
l i s t a = ( ListView ) findViewById (R . id . l i s t A l l P r o d u c t s ) ;
ActionBar a c t i o n B a r = getSupportActionBar ( ) ;
a c t i o n B a r . setDisplayHomeAsUpEnabled ( t r u e ) ;
}
4. Por ltimo vamos a realizar un AsyncTask para la conexin con
nuestro servidor.
La cabecera:
c l a s s LoadAllProducts extends AsyncTask< S t r i n g ,
String , String > {
El mtodo onPreExecute
@Override
p r o t e c t e d void onPreExecute ( ) {
super . onPreExecute ( ) ;
pDialog = new P r o g r e s s D i a l o g ( MyActivity . t h i s ) ;
pDialog . setMessage ( " Cargando comercios .
Por f a v o r e s p e r e . . . " ) ;
pDialog . s e t I n d e t e r m i n a t e ( f a l s e ) ;
pDialog . s e t C a n c e l a b l e ( f a l s e ) ;
pDialog . show ( ) ;
}
El mtodo doInBackground
p r o t e c t e d S t r i n g doInBackground ( S t r i n g . . . a r g s ) {
L i s t params = new A r r a y L i s t ( ) ;
JSONObject j s o n =
j P a r s e r . makeHttpRequest ( u r l _ a l l _ e m p r e s a s , "GET" , params ) ;
l o g . d ( " A l l Products : " , j s o n . t o S t r i n g ( ) ) ;
try {
i n t s u c c e s s = j s o n . g e t I n t (TAG_SUCCESS ) ;
i f ( s u c c e s s == 1 ) {
products= j s o n . getJSONArray (TAG_PRODUCTS ) ;
f o r ( i n t i = 0 ; i < products . l e n g t h ( ) ; i ++) {
JSONObject c = products . getJSONObject ( i ) ;
S t r i n g i d = c . g e t S t r i n g ( TAG_ID ) ;
S t r i n g name = c . g e t S t r i n g (TAG_NOMBRE ) ;
HashMap map = new HashMap ( ) ;
map . put ( TAG_ID , i d ) ;

actividad 4

map . put (TAG_NOMBRE, name ) ;


empresaList . add (map ) ;
}
}
} c a t c h ( JSONException e ) {
e . printStackTrace ( ) ;
}
return null ;
}
El mtodo onPostExecute
p r o t e c t e d void onPostExecute ( S t r i n g f i l e _ u r l ) {
pDialog . d i s m i s s ( ) ;
runOnUiThread (new Runnable ( ) {
public void run ( ) {
L i s t A d a p t e r adapter = new SimpleAdapter (
MyActivity . t h i s , empresaList , R . l a y o u t . s i n g l e _ p o s t ,
new S t r i n g [ ] {
TAG_ID ,
TAG_NOMBRE,
},
new i n t [ ] {
R . id . single_post_tv_id ,
R . i d . single_post_tv_nombre ,
});
/ / setListAdapter ( adapter ) ;
l i s t a . setAdapter ( adapter ) ;
}
});
}

actividad 5

actividad 5
1. Fichero xml main
<LinearLayout
android : layout_width= " f i l l _ p a r e n t "
android : l a y o u t _ h e i g h t = " f i l l _ p a r e n t "
android : o r i e n t a t i o n = " v e r t i c a l " >
<ListView
android : i d= "@+i d/ l i s t A l l P r o d u c t s "
android : layout_width= " f i l l _ p a r e n t "
android : l a y o u t _ h e i g h t = " wrap_content "
android : d i v i d e r H e i g h t = " 3dp "
android : background= " # f f f " />
</LinearLayout >

actividad 6
1. Fichero XML
<LinearLayout
android : layout_width= " f i l l _ p a r e n t "
android : l a y o u t _ h e i g h t = " match_parent "
android : background= " # f 0 f 0 f 0 "
android : o r i e n t a t i o n = " v e r t i c a l " >
<TextView
android : i d= "@+i d/s i n g l e _ p o s t _ t v _ n o m b r e "
android : layout_width= " wrap_content "
android : l a y o u t _ h e i g h t = " wrap_content "
android : l a y o u t _ g r a v i t y = " bottom "
android : paddingBottom= " 2 dip "
android : padding= " 10dp "
android : t e x t C o l o r = " #333 "
android : t e x t S i z e = " 12dp "
android : t e x t S t y l e = " bold "
android : t e x t = " nombre " />
<TextView
android : layout_width= " wrap_content "
android : l a y o u t _ h e i g h t = " wrap_content "
android : textAppearance= " ? android : a t t r /textAppearanceSmall "
android : t e x t = " i d "
android : i d= "@+i d/ s i n g l e _ p o s t _ t v _ i d "
android : v i s i b i l i t y = " gone " />
</LinearLayout >

actividad 6

https://github.com/saravia/BaseDatos

10

practica

practica
1. En esta prctica hemos visto como leer de una BBDD. Se pide
que podamos crear una empresa en nuestra aplicacin y se
verifique en el servidor.
2. Creamos un nuevo campo de BBDD que se va a llamar
descripcin. Modifica tanto la aplicacin como la BBDD del
servidor para que se pueda ver este nuevo campo.

https://github.com/saravia/Laboratorio8_Practica_1

11

Referencias

referencias
[1] Conceptos Android: http://www.ajpdsoft.com/modules.php?
name=News&file=article&sid=603

[2] Ejemplos basicos de Android: http://www.aprendeandroid.com/


l4/interface2.htm

[3] Videos Android ejemplos: https://www.youtube.com/watch?v=


sS3oDIcHNFo&list=PLN4U3dnOYpn4moPQiVz5QoByoHMoMg5Vd

12

S-ar putea să vă placă și