java 创建新的 SoapObject 会炸毁 android 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10324529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:34:49  来源:igfitidea点击:

Creating new SoapObject Blows up android app

javaandroidksoap2

提问by Pavel P

I'm new to ksoap2 and I'm trying to get example w3 schools example working. For some reason it always fails on this line.

我是 ksoap2 的新手,我正在尝试让示例 w3 学校示例工作。由于某种原因,它总是在这条线上失败。

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

I dont know what is causing the fail. The android app just blows up. I have set internet permisions in Manifest. Im not really sure what is going on. Thanks

我不知道是什么导致了失败。android 应用程序刚刚爆炸。我在清单中设置了互联网权限。我不太确定发生了什么。谢谢

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class WebServiceTurorialActivity extends Activity implements OnClickListener{

private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    (findViewById(R.id.button1)).setOnClickListener(this);
}


public void onClick(View v) {
    int a;
    int b;
    try
    { 
        // EditText ed1=(EditText)findViewById(R.id.editText1);
        // EditText ed2=(EditText)findViewById(R.id.editText2); 
        // a = Integer.parseInt(ed1.getText().toString());
        // b = Integer.parseInt(ed2.getText().toString()); 

         SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
         //Request.addProperty("a", a);
        // Request.addProperty("b", b);
         Request.addProperty("Celsius", "32");

         SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         soapEnvelope.dotNet = true;
         soapEnvelope.setOutputSoapObject(Request);

         HttpTransportSE transport= new HttpTransportSE(URL);

         transport.call(SOAP_ACTION, soapEnvelope);
         SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

         Toast.makeText(this,"200 " + resultString,Toast.LENGTH_SHORT).show();

     }catch(Exception ex) {
         Toast.makeText(this,"FALSE",Toast.LENGTH_SHORT).show();
     }
} 

}

}

回答by cimbo

I had the same problem. That works for me:

我有同样的问题。这对我行得通:

As the prev answer said:

正如上一个回答所说:

  • In your build path clear all libraries (jar files).
  • In project's main directory create a folder and named it "libs" (not "lib").
  • Now Eclipse ADT Plugin will add your jar files to build path
  • 在您的构建路径中清除所有库(jar 文件)。
  • 在项目的主目录中创建一个文件夹并将其命名为“libs”(不是“lib”)。
  • 现在 Eclipse ADT 插件会将您的 jar 文件添加到构建路径

Not realy for me. My way:

对我来说不是。我的方式:

  1. In your build path remove the "ksoap2-android-assembly-2.x.x-jar-with-dependencies.jar"
  2. Click OK (Ignore Errors shown)
  3. In project's main directory create a folder "libs"
  4. Copy the JAR-File into this directory (!)
  5. In your build path add the JAR file with this directory
  1. 在您的构建路径中删除“ksoap2-android-assembly-2.xx-jar-with-dependencies.jar”
  2. 单击确定(忽略显示的错误)
  3. 在项目的主目录中创建一个文件夹“libs”
  4. 将 JAR 文件复制到此目录中 (!)
  5. 在您的构建路径中,使用此目录添加 JAR 文件

回答by Bi Sus

For everyone having also this problem... I was trying to solve this problem for many many hours, but then I detected what my fault was: I did not correctly download the ksoap2 library, so there was only a jar file which was 21 KB, but the correct jar file is about 150KB. So make sure that you download the file correctly!

对于也有这个问题的每个人......我试图解决这个问题很多小时,但后来我发现我的错是:我没有正确下载 ksoap2 库,所以只有一个 21 KB 的 jar 文件,但正确的 jar 文件大约为 150KB。因此,请确保正确下载文件!

回答by OguzOzkeroglu

  • In your build path clear all libraries (jar files).
  • In project's main directory create a folder and named it "libs" (not "lib").
  • Now Eclipse ADT Plugin will add your jar files to build path.
  • Happy coding
  • 在您的构建路径中清除所有库(jar 文件)。
  • 在项目的主目录中创建一个文件夹并将其命名为“libs”(不是“lib”)。
  • 现在 Eclipse ADT 插件会将您的 jar 文件添加到构建路径。
  • 快乐编码

If you still get the same problem, try these classes (works for me):

如果您仍然遇到同样的问题,请尝试这些课程(对我有用):

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String fahrenheit = CelsiusToFahrenheitWs.celsiusToFahrenheit("32");
        Toast.makeText(getApplicationContext(), fahrenheit, Toast.LENGTH_SHORT).show();
    }
}

And

public class CelsiusToFahrenheitWs {
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
    private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";

    public static String celsiusToFahrenheit(String celsius) {
        String fahrenheit = null;

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("Celsius", celsius);

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

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            fahrenheit = response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            fahrenheit = null;
        }
        return fahrenheit;
    }
}

回答by Gábor AUTH

I've created an another SOAP library to call SOAP services on Android, the name of the project is AndroidSOAP ( http://wiki.javaforum.hu/display/ANDROIDSOAP/Home).

我创建了另一个 SOAP 库来在 Android 上调用 SOAP 服务,项目名称是 AndroidSOAP ( http://wiki.javaforum.hu/display/ANDROIDSOAP/Home)。

I've released a version 0.0.5 today, check it out: http://wiki.javaforum.hu/display/ANDROIDSOAP/2012/05/01/Version+0.0.5+released

我今天发布了 0.0.5 版本,请查看:http://wiki.javaforum.hu/display/ANDROIDSOAP/2012/05/01/Version+0.0.5+released

回答by NisSa

W3Schools.com is no more accepting NAMESPACE as tempuri.org which caused SOAP exception in the code. You need to use "http://www.w3schools.com/webservices/" for NAMESPACE and "http://www.w3schools.com/webservices/CelsiusToFahrenheit" for SOAP_ACTION.

W3Schools.com 不再接受 NAMESPACE 作为 tempuri.org,这会导致代码中的 SOAP 异常。你需要使用“ http://www.w3schools.com/webservices/”的命名空间和“ http://www.w3schools.com/webservices/CelsiusToFahrenheit”为SOAP_ACTION。

Hope this is help!

希望这是有帮助的!