1

我在以下代码中遇到异常

                package com.dipl.smpm.smpm;

 import org.ksoap2.SoapEnvelope;
 import org.ksoap2.serialization.SoapObject;
 import org.ksoap2.serialization.SoapPrimitive;
 import org.ksoap2.serialization.SoapSerializationEnvelope;
 import org.ksoap2.transport.AndroidHttpTransport;
 import org.ksoap2.transport.HttpTransport;

 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.TextView;
 import android.widget.Toast;
public class ConnectHttp extends Activity {
    private static final String SOAP_ACTION = "http://tempuri.org/loginDetails";
    private static final String METHOD_NAME = "loginDetails";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://localhost:88/EmpService.asmx";
    TextView tv;
 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
    tv=(TextView)findViewById(R.id.textView11);
    SoapObject sobj_requestLogin=new SoapObject(NAMESPACE, METHOD_NAME);
    sobj_requestLogin.addProperty("username", "a");
    sobj_requestLogin.addProperty("password", "a");
    SoapSerializationEnvelope spenve_Login = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    spenve_Login.dotNet=true;
    spenve_Login.setOutputSoapObject(sobj_requestLogin);

    AndroidHttpTransport htrans_login= new AndroidHttpTransport(URL);

    try{
        Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
        htrans_login.call(SOAP_ACTION,spenve_Login);
        Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT).show();
        SoapPrimitive spprim_login=(SoapPrimitive)spenve_Login.getResponse();
        Toast.makeText(getApplicationContext(), "3", Toast.LENGTH_SHORT).show();
        tv.setText("result: "+spprim_login+" ");
        Toast.makeText(getApplicationContext(), "4", Toast.LENGTH_SHORT).show();
    }
    catch(Exception e){
        Toast.makeText(getApplicationContext(), "Exception", Toast.LENGTH_LONG).show();
    }


}
 }

我成功地得到了一个吐司'1',但它会在吐司'2'到来之前立即出现'异常'吐司。帮帮我我的错误是什么?我也使用 2.3 ksoap2 2.3 jar 作为用户定义的库。

4

2 回答 2

1

像这样使用。

   final String SOAP_ACTION = "http://tempuri.org/loginDetails";
    final String METHOD_NAME = "loginDetails";
    final String NAMESPACE = "http://tempuri.org/";
    final String URL = "http://localhost:88/EmpService.asmx";


    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("username", "a");
    request.addProperty("password", "b");
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                          envelope.dotNet = true;
                      envelope.setOutputSoapObject(request);

    HttpTransportSE ht = new HttpTransportSE(URL);

    try {
          ht.call(SOAP_ACTION, envelope);
          final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                String str = response.toString();          
        }
     catch (Exception e) 
     {
      e.printStackTrace();
     }

在 manifest.xml 中添加这一行:

<uses-permission android:name="android.permission.INTERNET" />
于 2013-07-04T07:33:25.530 回答
0

我刚刚将 URL 中的 localhost 更改为本地系统的 ip。解决了。感谢@NIRMAL

于 2013-07-09T11:31:36.073 回答