dimanche 3 mai 2015

I am trying to create android application to insert data in sql server through web servervuc

I am new in android here m trying to save some data in SQL server from android through a web service but empty row inserted in SQL server....

Here is Web service code... this is working fine

[WebService(Namespace = "http://threepin.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

// [System.Web.Script.Services.ScriptService]
public class myWebService : System.Web.Services.WebService
{

    [WebMethod]
    public String insert(String name, String password)
    {
        SqlConnection conn = new SqlConnection("Data Source=.\\ ; Initial        Catalog=user; Integrated Security=true");
        SqlCommand insert = new SqlCommand("insert into login(name,   password) values('" + name + "','" + password + "')", conn);
       try
        {
            conn.Open();
            insert.ExecuteNonQuery();
            return name + password;
        }
        catch(Exception exp)
        {  
            conn.Close();
            return  exp.Message ;
        }
    }
 }
}

And this is android code file MyTask.java

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 android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

public class MyTask extends AsyncTask<String, Void, String> 
{
public String SOAP_ACTION = "http://ift.tt/1AwfP1l";
public String OPERATION_NAME = "insert";
public String WSDL_TARGET_NAMESPACE = "http://threepin.org";
public String SOAP_ADDRESS;
private SoapObject request ;
private HttpTransportSE httptransport;
private SoapSerializationEnvelope envelope;
Object response = null;
Context context;
Handler mhandler;
ProgressDialog pd;
String Result;
boolean isValid;

public MyTask(Context context) 
{

    this.context = context;
    pd = new ProgressDialog(context);

}

@Override
protected void onPreExecute() 
{
    super.onPreExecute();
    pd.setMessage("Inserting  Data");
    pd.setIndeterminate(false);
    pd.show();
    Log.d("MyTag", "On pre Execute");
}

@Override
protected String doInBackground(String... params) {

    Log.d("MyTag","do in background");
    SOAP_ADDRESS = "http://"+params[0]+"/myWebService.asmx";

    request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
    Log.d("MyTag","do in background 1"); 

    Log.d("MyTag", "do in background 2  "+request);


    request.addProperty("name",params[1]);
    Log.d("MyTag", "input name = "+params[1].toString());


    request.addProperty("password",params[2]);

    Log.d("MyTag", "input password = "+params[2].toString());
    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    Log.d("MyTag", "do in background 3  "+request.toString());

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

        try{
                httptransport = new HttpTransportSE(SOAP_ADDRESS);
              httptransport.call(SOAP_ACTION, envelope);
                response = envelope.getResponse();
                Log.d("MyTag", "response = "+response.toString());



                }
        catch(Exception exp)
        {
            response = exp.getMessage();
            Log.d("MyTag", "exception :     "+response.toString());

        }
        return null;
  }

    @Override
  protected void onPostExecute(final String result) {
    super.onPostExecute(result);
    Log.d("MyTag", "in post execute");
        pd.dismiss();

   }
}

Aucun commentaire:

Enregistrer un commentaire