Fix of ksoap objects

0

I am developing an app in android studio, I am consuming a webservices in asmx.

The detail is that I have in my websevices a method called menú , which brings me in an xml menu, which I want to show a ListView , the detail is that when I run the app. If I make the connection, if I see that it brings me the xml, but when I try to show it in the listview it does not show me anything ... reviewing forums I saw that I had to put a for to go one by one , but it does not, ah, and I'm using the class AsyncStack .

I leave the code where I send to call the webservices and the code of my activity where I implemented the listview .

webservices:

package com.example.onc_lap.proyecto;

/**
 * Created by ONC-LAP on 19/04/2016.
 */

        import org.ksoap2.SoapEnvelope;
        import org.ksoap2.serialization.PropertyInfo;
        import org.ksoap2.serialization.SoapObject;
        import org.ksoap2.serialization.SoapPrimitive;
        import org.ksoap2.serialization.SoapSerializationEnvelope;
        import org.ksoap2.transport.HttpTransportSE;

        import java.util.List;

public class webService {
    //Namespace of the Webservice - can be found in WSDL
    private static String NAMESPACE = "http://ONControl_MobileWS/";
    //Webservice URL - WSDL File location
    private static String URL = "http://oncontrol.no-ip.net:9020/ONControl_MobileWS.asmx";//Make sure you changed IP address
    //SOAP Action URI again Namespace + Web method name
    private static String METHOD_NAME = "Login";
    private static List arraylist;

    public static boolean Login(String usuario,String contrasena, String conexion) {
            boolean loginStatus = false;
        // Create request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        // Property which holds input parameters
        PropertyInfo unamePI = new PropertyInfo();
        PropertyInfo passPI = new PropertyInfo();
        // Set Username
        unamePI.setName("Usuario");
        // Set Value
        unamePI.setValue(usuario);
        // Set dataType
        unamePI.setType(String.class);
        // Add the property to request object
        request.addProperty(unamePI);
        //Set Password
        passPI.setName("Contrasena");
        //Set dataType
        passPI.setValue(contrasena);
        //Set dataType
        passPI.setType(String.class);
        //Add the property to request object
        request.addProperty(passPI);
        // Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        // Set output SOAP object
        envelope.setOutputSoapObject(request);
        // Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,7000);

        try {
            // Invoke web service
            androidHttpTransport.call("http://oncontrol.no-ip.net:9020/Login", envelope);
            // Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            // Assign it to  boolean variable variable
            loginStatus = Boolean.parseBoolean(response.toString());

        } catch (Exception e) {
            //Assign Error Status true in static variable 'errored'
            MainActivity.errored = true;
            e.printStackTrace();
        }
        //Return booleam to calling object
        return loginStatus;
    }

    public static void Menu() {
        String MenuResult = "";
        // Create request
        SoapObject request = new SoapObject("http://oncontrol.no-ip.net:9020/","Menu");
        // Property which holds input parameters

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.implicitTypes=false;
        // Set output SOAP object
        envelope.setOutputSoapObject(request);
        // Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,7000);

        try {
            // Invoke web service
            androidHttpTransport.call("http://oncontrol.no-ip.net:9020/Menu", envelope);
            // Get the response
            SoapObject obj1 = (SoapObject) envelope.bodyIn;
            SoapObject obj2 =(SoapObject) obj1.getProperty(0);


            for (int i = 0; i< obj2.getPropertyCount(); i++)
            {
                int opci = Integer.parseInt(obj2.getProperty(0).toString());

                // String id1=obj2.getProperty(0).toString();

                if(opci != 0)
                {
                    arraylist.add(""+ opci);

                }
                /* tv3.setText(id3);*/
            }
           // SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            // Assign it to  boolean variable variable
          //  MenuResult = response.toString();
//            String ASDF ="333";

        } catch (Exception e) {
            //Assign Error Status true in static variable 'errored'
            e.printStackTrace();
        }


    }
}

Activity:

package com.example.onc_lap.proyecto;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
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 java.util.ArrayList;
import java.util.List;

/**
 * Created by ONC-LAP on 19/04/2016.
 */
public class menu extends Activity {

    Button btnmenu;
    String ResultMenu;
    ProgressDialog dialog;
    ListView listmenu;
    ArrayList<String> arraylist = new ArrayList<String>();
    ArrayAdapter<String>  arrayadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        listmenu=(ListView)findViewById(R.id.listmenu);

        btnmenu = (Button) findViewById(R.id.btnmenu);
        btnmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AsyncCallWSMenu task = new AsyncCallWSMenu();
                //Call execute
                task.execute();

            }
        });
        {

        }

    }

    private class AsyncCallWSMenu extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params)         {
            webService.Menu();
            return null;
        }

        @Override
        //Make Progress Bar visible
        protected void onPreExecute() {

            dialog=new ProgressDialog(menu.this);
            dialog.setIndeterminate(false);
            dialog.setMessage("Loding...");
            dialog.setCancelable(false);
            dialog.show();

        }

        protected void onProgressUpdate(String... params) {
        }

        @Override
        //Once WebService returns response
        protected void onPostExecute(Void result)
        {

            if(arraylist.size()!=0){
                dialog.dismiss();
                arrayadapter = new ArrayAdapter<String>( menu.this,
                        android.R.layout.simple_list_item_activated_1, arraylist );
                listmenu.setAdapter(arrayadapter);
            }else{
                dialog.dismiss();

            }
        }
    }
}
    
asked by Hugo Rodriguez 20.04.2016 в 20:54
source

1 answer

1

It simply ensures that in your Menu() method that is executed within doInBackground() , you are adding the data within the arrayList:

arraylist.add(""+ opci);

Since if you do not have data this ArrayList , simply it will not contain data the Adapter and it will not be shown in your list:

   @Override
    protected void onPostExecute(Void result)
    {    
        if(arraylist.size()!=0){
            dialog.dismiss();
            arrayadapter = new ArrayAdapter<String>( menu.this,
            android.R.layout.simple_list_item_activated_1, arraylist);
            listmenu.setAdapter(arrayadapter);
        }else{
            dialog.dismiss();

        }
    }
  • I give you two suggestions:

1) You can cast an integer with String.valueOf ():

 arraylist.add(String.valueOf(opci));

instead of using:

 arraylist.add(""+ opci);

2) To validate if an ArrayList contains data use the method isEmpty () :

if(arraylist.isEmpty()){
...
...
    
answered by 20.04.2016 в 21:26