I would like to know how I can pass data from MySql to an Android ListView, I have been trying to do it but the application stops me, I have already armed my web server with the following PHP code
function getEmpresas($empresa)
{
$mysqli = new mysqli('localhost', 'root', '', 'dbcompinfra');
if ($mysqli->connect_errno) {
die("Fallo la conexión" . $mysqli->mysqli_connect_errno() . ")" . $mysqli->mysqli_connect_errno());
}
$resultado = $mysqli->query("SELECT * FROM concesionario ORDER BY nombre");
while ($fila = mysqli_fetch_array($resultado)) {
$empresaArray [] = array('nombre' => $fila[1]);
}
$arrayJson = json_encode($empresaArray);
return new soapval('return', 'xsd:string', $arrayJson);
$resultado->close();
}
and it already shows me the string in JSON but now my problem is that I can not make it happen to the Listview in Android.
This is my code in Android Studio
package com.ejemplo.alexis_caballero.sigc11app.sigc11;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.ejemplo.alexis_caballero.sigc11app.DAO.DaoEmpresas;
import com.ejemplo.alexis_caballero.sigc11app.R;
import com.ejemplo.alexis_caballero.sigc11app.adaptadores.AdapterEmpresas;
import com.ejemplo.alexis_caballero.sigc11app.adaptadores.AdapterSolicitudNis;
import com.ejemplo.alexis_caballero.sigc11app.adaptadores.ConsultarDatos;
import com.ejemplo.alexis_caballero.sigc11app.items.ItemEmpresas;
import com.ejemplo.alexis_caballero.sigc11app.items.ItemSolicitudNis;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.kobjects.util.Strings;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
public class Datos extends AppCompatActivity {
//public ListView lstEmpresas;
public ArrayList<ItemEmpresas> myItemEp;
public AdapterEmpresas myAdapterE;
//Conexion ws
private String ip = "10.169.6.189";
private final String URL_SERVICE = "http://"+ip+"/sigc11appws/servidor.php#";
private final String NAMESPACE = "http://"+ip+"/sigc11appws/";
private String METHOD_NAME = "";
private String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private SoapObject request;
private SoapSerializationEnvelope envelo;
private HttpTransportSE transport;
private PropertyInfo empresa = null;
ProgressDialog dialogAsynk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos);
//lstEmpresas = (ListView)findViewById(R.id.lstEmpresas);
ArrayList<ItemEmpresas> myItemEp = new ArrayList<ItemEmpresas>();
try {
//new AsyncronoEmp().execute();
}catch (Exception e){
e.printStackTrace();
}
myAdapterE = new AdapterEmpresas(this,myItemEp);
//lstEmpresas.setAdapter(myAdapterE);
//registerForContextMenu(lstEmpresas);
}
//***********Metodo para obtener las EMPRESAS*********//
public ArrayList<ItemEmpresas> getAllEmpresas(String params){
METHOD_NAME = "getEmpresas";
SOAP_ACTION = NAMESPACE + METHOD_NAME;
ArrayList<ItemEmpresas> myArrayWSE = new ArrayList<ItemEmpresas>();
try {
request = new SoapObject(NAMESPACE, METHOD_NAME);
this.empresa = new PropertyInfo();
this.empresa.setName("empresa");
this.empresa.setValue(params);
this.empresa.setType(String.class);
request.addProperty(this.empresa);
envelo = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelo.dotNet=false;
envelo.setOutputSoapObject(request);
transport = new HttpTransportSE(URL_SERVICE);
transport.call(SOAP_ACTION, envelo);
String resultadoWS = (String) envelo.getResponse();
Type tipo = new TypeToken<ArrayList<ItemEmpresas>>(){}.getType();
Gson convertidoJson = new Gson();
myArrayWSE = convertidoJson.fromJson(resultadoWS, tipo);
}catch (Exception e){
e.printStackTrace();
}
return myArrayWSE;
}
//****************Tarea Asyncrona para trabajar segundo plano obtencion de empresas**********//
class AsyncronoEmp extends AsyncTask<String, Integer, ArrayList<ItemEmpresas>>{
@Override
protected ArrayList<ItemEmpresas> doInBackground(String... params) {
return getAllEmpresas(params[0]);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(ArrayList<ItemEmpresas> itemEmpresases) {
super.onPostExecute(itemEmpresases);
dialogAsynk.dismiss();
if(itemEmpresases.size()> 1){
ItemEmpresas myEmpresa = new ItemEmpresas();
myEmpresa.setNombre(itemEmpresases.get(0).getNombre());
}else {
Crouton.makeText(Datos.this, "Sin Novedades", Style.INFO).show();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Crouton.cancelAllCroutons();
}
}
If you have any examples of how I can do it, it would help me a lot
the libraries I use is Ksoa2 and SOAP
Errors thrown by the Logcat
03-01 10: 45: 11.257 3911-4035 / com.example.alexis_caballero.sigc11app E / AndroidRuntime: FATAL EXCEPTION: AsyncTask # 1 Process: com.example.alexis_caballero.sigc11app, PID: 3911 java.lang.RuntimeException: An error occured while executing doInBackground () at android.os.AsyncTask $ 3.done (AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion (FutureTask.java:355) at java.util.concurrent.FutureTask.setException (FutureTask.java:222) at java.util.concurrent.FutureTask.run (FutureTask.java:242) at android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:587) at java.lang.Thread.run (Thread.java:818) Caused by: java.lang.ArrayIndexOutOfBoundsException: length = 0; index = 0 at com.example.alexis_caballero.sigc11app.sigc11.Data $ AsyncronoEmp.doInBackground (Datos.java:139) at com.example.alexis_caballero.sigc11app.sigc11.Data $ AsyncronoEmp.doInBackground (Datos.java:135) at android.os.AsyncTask $ 2.call (AsyncTask.java:288) at java.util.concurrent.FutureTask.run (FutureTask.java:237) at android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:587) at java.lang.Thread.run (Thread.java:818)
This is the string my PHP query sent me
This is the class I use for the items'public class ItemImpresas implements Serializable {
int id;
String nombre;
public ItemEmpresas(int id, String nombre) {
this.id = id;
this.nombre = nombre;
}
public ItemEmpresas(int id) {
this.id = id;
}
public ItemEmpresas(String nombre) {
this.nombre = nombre;
}
public ItemEmpresas() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
} '
and this is my adapter 'public class AdapterCompanies extends BaseAdapter {
protected AppCompatActivity datosAtivity;
protected ArrayList<ItemEmpresas> itemEmpresas;
public AdapterEmpresas (AppCompatActivity datosAtivity, ArrayList<ItemEmpresas> itemEmpresas){
this.datosAtivity = datosAtivity;
this.itemEmpresas = itemEmpresas;
}
@Override
public int getCount() {
return itemEmpresas.size();
}
@Override
public Object getItem(int i) {
return itemEmpresas.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertview, ViewGroup parent) {
View v = convertview;
if (convertview == null){
LayoutInflater inf = (LayoutInflater)datosAtivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inf.inflate(R.layout.item_empresas, null);
}
ItemEmpresas myItemEmpresas = itemEmpresas.get(i);
TextView txtEmpresa;
txtEmpresa = (TextView)v.findViewById(R.id.txtEmpresa);
txtEmpresa.setText("Empresa: " + myItemEmpresas.getNombre());
return v;
}
} '