2016-03-10 1 views
0

Je veux appeler le service web asp.net dans android. (ce service Web très simple, somme deux entiers et résultat de retour.)callig C# service web en android

mon problème est le suivant: lorsque je lance mon application et que je clique sur le bouton (pour donner des nombres à partir des champs de texte et additionner par service web et afficher le résultat par un AlertDialog) app être gel dans un état asd ne fonctionne pas. c'est ma capture d'écran de l'application dans ce statut (après un clic sur le bouton): freeze in this status! :-(

et après exposition de longue date ce massage: «Le savon ne répond-vous ce qu'il fermer? » .

Aidez-moi s'il vous plaît!

dans mes codes de service Web:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 

// [System.Web.Script.Services.ScriptService] 

public class Service : System.Web.Services.WebService 
{ 
public Service() { 

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
} 

[WebMethod] 
public int add (int a, int b) { 
    return a + b; 

} 

} 

c'est CallSoap, cette classe appeler mon service web:

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
public class CallSoap 
{ 
public final String SOAP_ACTION = "http://tempuri.org/add"; 

public final String OPERATION_NAME = "add"; 

public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 

public final String SOAP_ADDRESS = "http://s1.azarindk.co.ir/service.asmx"; 
public CallSoap() 
{ 
} 
public String Call(int a,int b) 
{ 
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME); 
//PropertyInfo pi =new PropertyInfo(); 

request.addProperty("a", Integer.valueOf(a)); 
request.addProperty("b", Integer.valueOf(b)); 

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11); 
envelope.dotNet = true; 

envelope.setOutputSoapObject(request); 

HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 
Object response=null; 
try 
{ 
httpTransport.call(SOAP_ACTION, envelope); 
response = envelope.getResponse(); 
} 
catch (Exception exception) 
{ 
response=exception.toString(); 
} 
System.out.print(response.toString()); 
return response.toString(); 


} 
} 

ce qui est un fil pour appeler CallSoap dans la principale activité:

package com.example.soap; 
public class Caller extends Thread 
{ 
public CallSoap cs; 
public int a,b; 


public void run(){ 
    try{ 
     cs=new CallSoap(); 
     String resp=cs.Call(a, b); 
     MainActivity.rslt=resp; 
    }catch(Exception ex) 
    {MainActivity.rslt=ex.toString(); 
    }  
} 
} 

et ceci est ma principale activité:

package com.example.soap; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends ActionBarActivity { 

public static String rslt=""; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button b1=(Button)findViewById(R.id.button1); 
    final AlertDialog ad=new AlertDialog.Builder(this).create(); 

    b1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
     // TODO Auto-generated method stub 

     CallSoap cs=new CallSoap(); 

     try 
     { 
      EditText ed1=(EditText)findViewById(R.id.editText1); 
      EditText ed2=(EditText)findViewById(R.id.editText2); 
      int a=Integer.parseInt(ed1.getText().toString()); 
      int b=Integer.parseInt(ed2.getText().toString()); 
      rslt="START"; 

      Caller c=new Caller(); 
      c.a=a; 
      c.b=b; 
     // c.ad=ad; 

      c.join(); 
      c.start(); 

      while(rslt=="START") { 
       try { 
        Thread.sleep(10); 
       }catch(Exception ex) { 
       } 
      } 

      ad.setTitle("RESULT OF ADD of "+a+" and "+b); 
      ad.setMessage(rslt); 

     }catch(Exception ex) { 
      ad.setTitle("Error!"); ad.setMessage(ex.toString()); 
     } 
     ad.show(); 
    } }); 

      } 

} 

Répondre

0

Vous avez une boucle d'interrogation sur votre thread principal, ce qui est très mauvais. Ce n'est pas mieux que de bloquer le thread principal avec la transaction réseau elle-même. En outre, il semble que votre boucle ne se terminera jamais puisque la valeur de rslt ne change jamais. Enfin, n'utilisez pas == pour comparer les chaînes en Java.

Renseignez-vous sur les techniques pour faire face à un travail fileté dans Android. Vous avez beaucoup d'options, mais vous devrez suivre un bon schéma.

+0

merci Doug, :-) –