Android模拟器使用KSOAP2调用WebService

 

Android模拟器使用KSOAP2调用WebService

分类: Andriod 252人阅读 评论 (0) 收藏 举报

    项目需要Android去访问webservice,网上找了很多例子、中英文网站,每次运行都是出现错误“The application ***(process  包名) has stopped unexpectedly.Please try again “ ,困扰了好几天,是因为Eclipse导入ksoap2-android-assembly-2.4-jar-with- dependencies.jar包的时候的方式的问题。

     之前导入jar包的时候,右键工程 Build path, java build path,选择libraries,在右边的按钮中点击“Add Library”,选择“User library”,点击“下一步”,点击“User librarys”按钮, 在出现的界面中点击“New..”按钮,在弹出的界面中随便起一个名字,点击“确定”, 点击“Add jars”按钮选择第三方jar包,点击“确定”完成操作。

    后来右键工程,Build path,java build path,选择libraries,在右边的按钮中点击“Add External JARs”,然后选择合适的jar包。

    还有一个问题就是Android模拟器访问Tomcat上部署的webservice程序时,不能用localhost 或者本级IP,Android默认访问本机地址为10.0.2.2。

 

import java.io.IOException;

import org.ksoap2.SoapEnvelope;  
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;  
import org.ksoap2.serialization.SoapSerializationEnvelope;  
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;  
import org.xmlpull.v1.XmlPullParserException;


import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
   
public class Main extends Activity
{  
 private static final String SOAP_ACTION = "http://10.0.2.2:8080/erictest/services/MyService?wsdl "; //null
 private static final String METHOD_NAME = "checkUser";
 private static final String NAMESPACE = "http://test.webservice.whitehouse.net.cn "; // targetNamespace  wsdl
 private static final String URL = "http://10.0.2.2:8080/erictest/services/MyService?wsdl ";
 private TextView view1;
   
    @Override 
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        view1 = (TextView) findViewById(R.id.tvResult);
        SoapObject request = new SoapObject(SOAP_ACTION, METHOD_NAME);
  request.addProperty("userName", "eric");
  request.addProperty("userPwd", "831987");
  System.out.println("-----------------------1");
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  envelope.dotNet = true;
  System.out.println("-----------------------2");
  envelope.setOutputSoapObject(request);
  AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
  System.out.println("-----------------------3");
  try
  {
   System.out.println("-----------------------4");
 
   androidHttpTransport.call(SOAP_ACTION, envelope);
 
   System.out.println("-----------------------5");

//   SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
   Object result = (Object)envelope.getResponse();
 
   System.out.println("-----------------------6");
//   String xy = resultsRequestSOAP.getProperty(0).toString();

   if(result.toString().equals("true"))
   {
    view1.setText("返回数据成功");
   }
   else
   {
    view1.setText("返回数据失败");
   }
  
   System.out.println("result: "+result);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 
    }  

 

附:

使用KSOAP2调用WebService http://www.ophonesdn.com/article/show/169

访问公共wsdl 的例子 http://www.helloandroid.com/tutorials/using-ksoap2-android-and-parsing-output-data

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/free510/archive/2010/09/14/5883982.aspx

你可能感兴趣的:(webservice)