El día de hoy les voy a explicar como crear un archivo .txt para una aplicación en android y como leer el correspondiente archivo.
String FILENAME = "ejemplo.txt"; /////nombre del archivo
String string = "ejemplo de cadena"; //// Ingresamos la cadena que se desea guardar
///////////////////////////Creando el documento/////////////////////
FileOutputStream fos = openFileOutput(FILENAME, Activity.MODE_PRIVATE);
//////////// el archivo es privado y solo se puede leer el mismo activity
fos.write(string.getBytes());
fos.flush();
fos.close();
//////////////////////////////////////////////////////////////////////////////////
InputStreamReader archivo = new InputStreamReader(openFileInput("ejemplo.txt"));
BufferedReader br = new BufferedReader(archivo);
String linea = br.readLine();
String todo = "";
while (linea != null) ////////////////// recorre hasta que la linea sea null
{
todo = todo + linea; ////////////// recuperamos la informacion por linea y la concatenamos
linea = br.readLine(); /////////////raliza el salto de liena
}
Toast.makeText(getApplicationContext(),todo , Toast.LENGTH_LONG).show(); ///////////////////se imprime con una alerta lo que se recupero del archivo
br.close();
archivo.close();
Ju@ncho C0de
sábado, 12 de octubre de 2013
Conectando un web service con Ksoap 2 en android utilizando AsyncTask
Realizando una programa en android me encontré con un problema al conectarme con un WS en dispositivos que utilizaran android versión 4 en adelante, pues resulta que no se puede conectar si lo llamamos directamente en el MainActivity, la solución utilizar AsyncTask
private static final String accionSoap = "urn:example1#example";
private static final String Metodo = "example";
private static final String namespace = "urn:example1"; ////Configuracion del WS
private static final String url = "http://tuurl/servicio?wsdl";
protected void onCreate(Bundle savedInstanceState)
{
new backMethod().execute();/////////////////METODO PARA LLAMAR EL AsyncTask
}
public class backMethod extends AsyncTask<String, Object, Object >
{
private final ProgressDialog dialog = new ProgressDialog(
MainActivity.this);
protected void onPreExecute()
{
dialog.setMessage("HOLA MUNDO..."); ///MENSAJE DE CARGA
dialog.show(); //Mostramos el diálogo antes de comenzar
}
protected void onCancelled(Object result)
{
//super.onCancelled(result);
}
@Override
protected Object doInBackground(String... params)
{
Object resultado = null;
SoapObject request = new SoapObject(namespace1, Metodo1);
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER10);
sobre.dotNet = true;
request.addProperty("data", "1");
sobre.setOutputSoapObject(request);
try
{
HttpTransportSE transporte = new HttpTransportSE(url1);
transporte.call(accionSoap1, sobre);
resultado = (SoapPrimitive) sobre.getResponse();
}
catch (Exception e)
{
e.printStackTrace();
}
return resultado;
}
protected void onPostExecute(Object result)
{
//Here All your UI part is Done
if (result != null)
{
String string = result.toString();//////OBTENEMOS LA RESPUESTA
} else
{
/////////// MENSAJE SI NO HAY RESPUESTA
}
super.onPostExecute(result);
if (this.dialog.isShowing())
{
dialog.dismiss();
}
super.onPostExecute(result);
}
}
Si necesitan ayuda para la solución de su problema y tengo la manera de ayudar con gusto la atenderé.
private static final String accionSoap = "urn:example1#example";
private static final String Metodo = "example";
private static final String namespace = "urn:example1"; ////Configuracion del WS
private static final String url = "http://tuurl/servicio?wsdl";
protected void onCreate(Bundle savedInstanceState)
{
new backMethod().execute();/////////////////METODO PARA LLAMAR EL AsyncTask
}
public class backMethod extends AsyncTask<String, Object, Object >
{
private final ProgressDialog dialog = new ProgressDialog(
MainActivity.this);
protected void onPreExecute()
{
dialog.setMessage("HOLA MUNDO..."); ///MENSAJE DE CARGA
dialog.show(); //Mostramos el diálogo antes de comenzar
}
protected void onCancelled(Object result)
{
//super.onCancelled(result);
}
@Override
protected Object doInBackground(String... params)
{
Object resultado = null;
SoapObject request = new SoapObject(namespace1, Metodo1);
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER10);
sobre.dotNet = true;
request.addProperty("data", "1");
sobre.setOutputSoapObject(request);
try
{
HttpTransportSE transporte = new HttpTransportSE(url1);
transporte.call(accionSoap1, sobre);
resultado = (SoapPrimitive) sobre.getResponse();
}
catch (Exception e)
{
e.printStackTrace();
}
return resultado;
}
protected void onPostExecute(Object result)
{
//Here All your UI part is Done
if (result != null)
{
String string = result.toString();//////OBTENEMOS LA RESPUESTA
} else
{
/////////// MENSAJE SI NO HAY RESPUESTA
}
super.onPostExecute(result);
if (this.dialog.isShowing())
{
dialog.dismiss();
}
super.onPostExecute(result);
}
}
Si necesitan ayuda para la solución de su problema y tengo la manera de ayudar con gusto la atenderé.
Suscribirse a:
Entradas (Atom)