消费网络服务时,Android应用程序崩溃

时间:2015-05-08 09:18:43

标签: android web-services android-ksoap2

我正在尝试通过android使用Web服务。我使用kso​​ap2库并按照this example。每当我向文本字段输入数据时,应用程序崩溃。我收到以下错误:

Process: com.tinyvoice.webservice, PID: 14447
java.lang.VerifyError: org/ksoap2/SoapEnvelope
        at com.tinyvoice.webservice.WebServiceActivity$1.onClick(WebServiceActivity.java:49)

以下是链接中的应用:

public class WebServiceActivity extends AppCompatActivity {
/**
 * Called when the activity is first created.
 */
private static String SOAP_ACTION1 = "http://tempuri.org/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://tempuri.org/CelsiusToFahrenheit";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";

Button btnFar, btnCel, btnClear;
EditText txtFar, txtCel;

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

    btnFar = (Button) findViewById(R.id.btnFar);
    btnCel = (Button) findViewById(R.id.btnCel);
    btnClear = (Button) findViewById(R.id.btnClear);
    txtFar = (EditText) findViewById(R.id.txtFar);
    txtCel = (EditText) findViewById(R.id.txtCel);

    btnFar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Initialize soap request + add parameters
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);

            //Use this to add parameters
            request.addProperty("Fahrenheit", txtFar.getText().toString());

            //Declare the version of the SOAP request
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;

            try {
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION1, envelope);

                // Get the SoapResult from the envelope body.
                SoapObject result = (SoapObject) envelope.bodyIn;

                if (result != null) {
                    //Get the first property and change the label text
                    txtCel.setText(result.getProperty(0).toString());
                } else {
                    Toast.makeText(getApplicationContext(), "No Response", Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    btnCel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //same logic for Farenheit 
...
}
}

我正在使用Android Studio并将ksoap2.jar添加到libs文件夹,然后添加为库。 感谢

1 个答案:

答案 0 :(得分:2)

如果您正在使用Android Studio,则必须添加

 compile 'com.google.code.ksoap2-android:ksoap2-android:3.1.0'
dependencies的{​​{1}}中

并添加

gradle
repositories { maven { url 'https://oss.sonatype.org/content/repositories/ksoap2-android-releases/' } }

中的buildTypes

当我在过去面对同样的问题时,我通过使用上述技巧解决了问题。

还可以使用gradle

在后台执行与网络相关的任务