-2

// So...i this webservice with a connection at a database. I have two webmethods : one //for search in database and one for insert. I don't know if I'm connected to webservice //because my app exit when I press any button. Can you help me,Thank you very much!

package com.example.btn;

//import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
//import android.app.Activity;
//import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
//import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity {

     private static String SOAP_ACTION1 = "http://localhost/CheckRecords";

     private static String SOAP_ACTION2 = "http://localhost/RecordData";

     private static String NAMESPACE = "http://localhost/";

     private static String METHOD_NAME1 = "CheckRecords";

     private static String METHOD_NAME2 = "RecordData";

     private static String URL = "http://http://192.168.100.176/WebS/Service1.asmx?wsdl";

   // Button btnCheck,btnRecord;
   // EditText barcodein,denumirein;


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

       Button btnCheck = (Button) findViewById(R.id.btnCheck);
       Button btnRecord = (Button) findViewById(R.id.btnRecord);
       final EditText   barcodein = (EditText) findViewById(R.id.barcodein);
       final EditText denumirein = (EditText) findViewById(R.id.denumirein);

    btnCheck.setOnClickListener(new View.OnClickListener()
    {
              @Override
              public void onClick(View v)
              {
                    //Initialize soap request + add parameters
              SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);       

              //Use this to add parameters
              request.addProperty("newbarcode",barcodein.getText().toString());

              //Declare the version of the SOAP request
              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

              envelope.setOutputSoapObject(request);
              envelope.dotNet = true;

              try {
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                    //this is the actual part that will call the webservice
                    androidHttpTransport.call(SOAP_ACTION1,envelope);

                    // Get the SoapResult from the envelope body.
                    SoapObject result = (SoapObject)envelope.bodyIn;

                    if(result != null)
                    {
                          //Get the first property and change the label text
                          denumirein.setText(result.getProperty(0).toString());
                    }
                    else
                    {
                          Toast.makeText(getApplicationContext(), "No data found",Toast.LENGTH_LONG).show();
                    }
              } catch (Exception e) {
                    e.printStackTrace();
              }
              }
        });

    btnRecord.setOnClickListener(new View.OnClickListener()

    {

              @Override
              public void onClick(View v)

              {
                    //Initialize soap request + add parameters
              SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);       

              //Use this to add parameters
              request.addProperty("newbarcode",barcodein.getText().toString());

              //Declare the version of the SOAP request
              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

              envelope.setOutputSoapObject(request);

              envelope.dotNet = true;

              try {

                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                    //this is the actual part that will call the webservice

                    androidHttpTransport.call(SOAP_ACTION2, envelope);

                          Toast.makeText(getApplicationContext(), "Inserted",Toast.LENGTH_LONG).show();


              } catch (Exception e) {

                    e.printStackTrace();

              }

              }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
nobalG
  • 4,544
  • 3
  • 34
  • 72
Epy Catalin
  • 15
  • 1
  • 7
  • http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Jul 11 '14 at 07:22
  • post the logcat please – nobalG Jul 11 '14 at 07:25
  • Probably you would get `NetworkOnMainThreadException`. Refer [this](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) question. – SSS Jul 11 '14 at 07:45

1 Answers1

0

I have two webmethods: one for search in database and one for insert. I don't know if I'm connected to web service because my app exit when I press any button.

=> Because you are trying to make a network call directly on main UI thread. To resolve this issue, you need to implement AsyncTask.

And I would request you to check Logcat output at least which exactly gives you an error/exception log.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295