Ver Mensaje Individual
  #6 (permalink)  
Antiguo 26/03/2012, 03:41
inf45
 
Fecha de Ingreso: marzo-2012
Mensajes: 1
Antigüedad: 12 años, 1 mes
Puntos: 0
Respuesta: Conexion Android + php + mysql

Hola a todos!! yo estoy intentando conectarme a una base de datos mysql con la version android 2.1, pero no consigo que me muestre los datos, he seguido el manual de "Leunamal",os comento lo que tengo:
por un lado tengo los datos que me muestra el php, que son los siguientes

[{"id":"4","titulo":"atencionCliente.jpg","curso_id ":"29","numero":"1","tema":"3","subtema":"uno "},{"id":"6","titulo":"12.jpg","curso_id":"29","nu mero":"2","tema":"4","subtema":" dos "},{"id":"7","titulo":"00.jpg","curso_id":"34","nu mero":"1","tema":"5","subtema":"Indice "}]

y por otro el codigo java

package com.auto.application;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity ;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CursoActivity extends Activity {
AutoCompleteTextView texto;
Button boton;
TextView resultado;
String total;
public void onCreate(Bundle SavedInstanceState)
{
super.onCreate(SavedInstanceState);
setContentView(R.layout.curso);
texto = (AutoCompleteTextView) findViewById(R.id.cajaTexto);
// texto.setAdapter(adapter);
boton = (Button) findViewById(R.id.botonBuscar);
resultado = (TextView) findViewById(R.id.resultado);
boton.setOnClickListener (new OnClickListener()
{

public void onClick(View v)
{

resultado.setText(getServerData(total));
}
});
}
//se declara la direccion ip del servidor
public final static String url = "XXXXXX(datos que muestra anteriormente)";

private String getServerData (String returnString)
{
String txt = texto.getText().toString();
if(!txt.equals(""))
{
InputStream is = null;

String result ="";

//Se envian datos de consulta;
ArrayList<NameValuePair> parametros = new ArrayList<NameValuePair>();
parametros.add(new BasicNameValuePair("nombre", "1"));

//Conectando a la base de datos
try
{
HttpClient cliente = new DefaultHttpClient();
HttpPost direccion = new HttpPost(url);
HttpResponse respuesta = cliente.execute(direccion);
respuesta.setEntity(new UrlEncodedFormEntity(parametros));
HttpEntity entity = respuesta.getEntity();
is = entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag", "Error en la conexion a la base de datos"+e.toString());
Toast.makeText(this, "Error al conectar a la base de datos", Toast.LENGTH_LONG).show();
}

//Transformar respuesta de la conexion en string
try
{
BufferedReader reader = new BufferedReader (new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String linea = null;
while ((linea = reader.readLine()) != null)
{
sb.append(linea + "\n");

}
is.close();
result = sb.toString();
Log.e("log_tag", "Cadena JSON" + result);
}
catch(Exception e)
{
Log.e("log_tag", "Error al transformar datos. "+e.toString());
Toast.makeText(this, "Error al transformar datos", Toast.LENGTH_LONG).show();
}

//Recibir datos en formato JSON
try
{
JSONArray jArreglo = new JSONArray(result);
for(int i=0; i<jArreglo.length(); i++)
{
JSONObject json_datos = jArreglo.getJSONObject(i);
Log.i("log_tag", "id: "+json_datos.getInt("id")+
", titulo:"+json_datos.getString("titulo")+
", curso_id: "+json_datos.getInt("curso_id")+
", numero: "+json_datos.getInt("numero")+
", tema: "+json_datos.getInt("tema")+
", subtema: "+json_datos.getString("subtema"));
//Los datos se muestran en la pantalla
resultado.setText(total);
returnString += "\n\t" + jArreglo.getJSONObject(i);

}
}
catch (JSONException e)
{
Log.e("log_tag", "Error al recibir datos"+e.toString());
Toast.makeText(this, "Error al mostrar datos", Toast.LENGTH_LONG).show();
}
}
else Toast.makeText(this, "Escriba un curso", Toast.LENGTH_LONG).show();
return returnString;
}

}

y me sale un error al recibir datosorg.json.JSONException.
que puedo estar haciendo mal?.

Muchas gracias de antemano.
Saludoss!!!