通过url调用web服务到android应用程序

时间:2013-05-10 05:08:11

标签: android web-services

我使用Soap object来完成此任务,如前所述。但不幸的是,代码不起作用。它会给出一条消息“unfortunately,ksoap2sample has stoped”。我找不到我的错误发生的地方。当我调试代码时,它显示NOClassDefFoundError。请帮我纠正此代码。

这是我的代码

package projects.ksoap2sample;

import java.io.IOException;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.TargetApi;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class MainActivity extends Activity {

private static final String SOAP_ACTION = "http://tempuri.org/getTown";
private static final String METHOD_NAME = "getTown";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2/TestProject/Service.asmx";

private TextView lblResult;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lblResult = (TextView) findViewById(R.id.textView1);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("id", "DSGFDG");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);

        // SoapPrimitive resultsRequestSOAP = (SoapPrimitive)
        // envelope.getResponse();
        // SoapPrimitive resultsRequestSOAP = (SoapPrimitive)
        // envelope.getResponse();
        SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;

        lblResult.setText(resultsRequestSOAP.toString());
        System.out.println("Response::" + resultsRequestSOAP.toString());

    } catch (Exception e) {

        System.out.println("Error" + e);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

这是我的web方法asp.net c#

[WebMethod]
public DataTable getTown(string id)
{
    DataTable dt = new DataTable();
    string strsql = "Select PersonName,Gender,CivilStatus From PersonalData where PersonName=" + id;
    dt = com.GetDataTableFromSql(strsql);
    return dt;

}

0 个答案:

没有答案