Android – Consommer un WebService asmx

Pré requis :

télécharger la librairie ksoap2 : https://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2

Le webservice .net :

Qui renvoi une chaine de caratère sous format JSON

[WebMethod]

public string Test()

{

Npgsql.NpgsqlConnection sqlCon = new Npgsql.NpgsqlConnection();

string ConnectionStr = System.Configuration.ConfigurationManager.AppSettings[“connexion”].ToString();

sqlCon.ConnectionString = ConnectionStr;

string req = “select c.client_numero , client.adresse_nom  from  clients c ;

sqlCon.Open();

DataSet ds = new DataSet();

Npgsql.NpgsqlDataAdapter da = new Npgsql.NpgsqlDataAdapter(req, sqlCon);

da.Fill(ds);

sqlCon.Close();

string strDetails = “”;

if (ds.Tables[0].Rows.Count > 0)

{

strDetails += “[“;

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

{

strDetails +=

“{\”Field1\”:\”” + ds.Tables[0].Rows[i][“client_numero”].ToString() + “\”, \”Field2\”:\”” + ds.Tables[0].Rows[i][“adresse_nom”].ToString() + “\”},”;

}

strDetails += “]”;

strDetails = strDetails.Replace(“,]”, “]”);

}

return strDetails;

}

 

 

Classe avec méthode d’appel :

import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;

import org.ksoap2.serialization.SoapSerializationEnvelope;

import org.ksoap2.transport.HttpTransportSE;

public class AppelService

{

public final String SOAP_ACTION1 = http://tempuri.org/Test&#8221;;

public final String OPERATION_NAME1 = Test;

public final String WSDL_TARGET_NAMESPACE1 = http://tempuri.org/&#8221;;

public final String SOAP_ADDRESS1 = http://192.168.250.112/sv.asmx&#8221;;

public String call()

{

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE1,OPERATION_NAME1);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

SoapEnvelope.VER11);

envelope.dotNet = true;

envelope.setOutputSoapObject(request);

HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS1);

Object response=null;

try

{

httpTransport.call(SOAP_ACTION1, envelope);

response = envelope.getResponse();

}

catch (Exception exception)

{

response=exception.toString();

}

return response.toString();

}

}

 

 

Classe du main :

import org.json.JSONArray;

import org.json.JSONObject;

import android.app.Activity;

import android.os.Bundle;

import android.os.StrictMode;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);

Button btn1 = (Button)findViewById(R.id.button1);

final TextView tv1 = (TextView)findViewById(R.id.textView1);

btn1.setOnClickListener(new OnClickListener()

{

public void onClick(View view) {

AppelService sv = new AppelService();

String r = sv.call();

try

{

String jsontext = new String(r);

JSONArray entries = new JSONArray(jsontext);

String x = “”;

int i;

for (i=0;i<entries.length();i++)

{

JSONObject post = entries.getJSONObject(i);

x += “————\n”;

x += “Id:” + post.getString(“Field1”) + “\n”;

x += “Nom :” + post.getString(“Field2”) + “\n\n”;

}

tv1.setText(x);

}

catch (Exception je)

{

tv1.setText(“Error w/file: “ + je.getMessage());

}

}

}

);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

 

Partie graphique Main.xml :

<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android&#8221;

xmlns:tools=http://schemas.android.com/tools&#8221;

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:paddingBottom=“@dimen/activity_vertical_margin”

android:paddingLeft=“@dimen/activity_horizontal_margin”

android:paddingRight=“@dimen/activity_horizontal_margin”

android:paddingTop=“@dimen/activity_vertical_margin”

tools:context=“com.example.test.MainActivity” >

<TextView

android:id=“@+id/textView1”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“” />

<Button

android:id=“@+id/button1”

style=“?android:attr/buttonStyleSmall”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignLeft=“@+id/textView1”

android:layout_below=“@+id/textView1”

android:layout_marginLeft=“33dp”

android:layout_marginTop=“100dp”

android:text=“Button” />

</RelativeLayout>

Dans le fichier de configuration manifest.xml :

<uses-permission android:name=“android.permission.INTERNET”/>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s