21.04.2014 Views

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

Curso-Android-Desarrollo-de-Aplicaciones-Móviles

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CURSO ANDROID<br />

127<br />

txt.setText(translatedText);<br />

Para la sección <strong>de</strong> la búsqueda en YouTube iniciamos obteniendo el parámetro <strong>de</strong> búsqueda y ocultando<br />

el teclado.<br />

EditText etQueryText = (EditText)findViewById(R.id.etQueryText);<br />

String queryText = etQueryText.getText().toString();<br />

InputMethodManager imm =<br />

(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);<br />

imm.hi<strong>de</strong>SoftInputFromWindow(etQueryText.getWindowToken(), 0);<br />

Utilizando este parámetro <strong>de</strong> búsqueda, preparamos el URL <strong>de</strong>l request, indicamos que la respuesta la<br />

queremos en JSON y que la cantidad máxima <strong>de</strong> resultados será 5.<br />

GenericUrl url = new GenericUrl(“https://gdata.youtube.com/feeds<br />

/api/vi<strong>de</strong>os?alt=jsonc&amp;max-results=5&q=” + queryText);<br />

Preparamos la requisición que requiere <strong>de</strong> transporte http y el parser <strong>de</strong> JSON:<br />

HttpTransport transport = new NetHttpTransport();<br />

final JsonFactory jsonFactory = new JacksonFactory();<br />

Usando la instancia recién creada <strong>de</strong> HttpTransport, vamos a construir un RequestFactory indicándole<br />

el tipo <strong>de</strong> parser y los encabezados para el request.<br />

HttpRequestFactory factory = transport.createRequestFactory(new<br />

HttpRequestInitializer() {<br />

@Overri<strong>de</strong><br />

public void initialize(HttpRequest request) {<br />

//indicamos el parser a utilizar<br />

JsonCParser parser = new JsonCParser();<br />

parser.jsonFactory = jsonFactory;<br />

request.addParser(parser);<br />

//indicamos el nombre <strong>de</strong> la aplicación y la versión <strong>de</strong> gData<br />

//JSON es soportado solo en versión 2 <strong>de</strong> gData<br />

GoogleHea<strong>de</strong>rs hea<strong>de</strong>rs = new GoogleHea<strong>de</strong>rs();<br />

hea<strong>de</strong>rs.setApplicationName(“Maestros-Del-Web-<strong>Android</strong>/1.0”);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!