使用 ex.getMessage() 打印时,androidHttpTransport.call(...,...) 抛出空异常

标签 android web-services soap nullpointerexception

我从使用 eclipse 开发的 andriod 应用程序调用 webservice, 但在调用时出现异常 androidHttpTransport.call(SOAP_ACTION, 信封);

ex.getMessage() 打印“空”异常消息

在我指定的 list 文件中

 <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

完整代码如下

///////////////////
public class Yasir1Activity extends Activity {
    TextView tv;  



            private final String NAMESPACE = "http://www.webserviceX.NET/"; 
            private final String URL = "http://www.webservicex.net/ConvertWeight.asmx"; 
            private final String SOAP_ACTION = "http://www.webserviceX.NET/ConvertWeight"; 
            private final String METHOD_NAME = "ConvertWeight"; 
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {  
                super.onCreate(savedInstanceState);  
                setContentView(R.layout.main);  
                tv=(TextView)findViewById(R.id.txt); 

            String msg="";
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
            String weight = "3700"; 
            String fromUnit = "Grams"; 
            String toUnit = "Kilograms"; 

            PropertyInfo weightProp =new PropertyInfo(); 
            weightProp.setName("Weight"); 
            weightProp.setValue(weight); 
            weightProp.setType(double.class); 
            request.addProperty(weightProp); 

            PropertyInfo fromProp =new PropertyInfo(); 
            fromProp.setName("FromUnit"); 
            fromProp.setValue(fromUnit); 
            fromProp.setType(String.class); 
            request.addProperty(fromProp); 

            PropertyInfo toProp =new PropertyInfo(); 
            toProp.setName("ToUnit"); 
            toProp.setValue(toUnit); 
            toProp.setType(String.class); 
            request.addProperty(toProp); 

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
            try 
            { 
                msg="before call";
                androidHttpTransport.call(SOAP_ACTION, envelope); //throwing exception
                msg+="call success";


                SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); 
                //Log.i("myApp", response.toString()); 
                //TextView tv = new TextView(this); 
                msg+=response.toString();
                tv.setText(response.toString());//weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit); 

            } 
            catch(SocketException ex)
               {
                 msg+="Error on soapPrimitiveData() " + ex.getMessage();
                   ex.printStackTrace();
               }
            catch (Exception e) 
            { 
                e.printStackTrace(); 
                msg += "Exception " +e.getMessage(); //"Exception null" is printing
            }


            tv.setText( msg);

            } 
            }

最佳答案

我遇到了同样的问题。我得到一个空异常,因为我在主线程中尝试。 Honeycomb SDK 或更高版本的问题。 http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

解决方案,在新线程中运行进程。

您好!

关于使用 ex.getMessage() 打印时,androidHttpTransport.call(...,...) 抛出空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8924882/

相关文章:

android - 如何在后端而不是 App Engine 数据存储中创建与 Google Cloud SQL 连接的 Android 应用程序?

soap - JAXB 'forgets' 在设置属性值但未设置元素值时添加 nil ='true'

java - Web 服务:远程接口(interface)与请求/响应方案

java - 并发修改异常,想不通

java - Android NDK JNI 点表示法方法签名变量

android - 在 viewpager 中使用 backstack 和 back 按钮

java - "java.rmi.Remote cannot be resolved to a type"是什么意思?

java - 使用 Java 中的 Apache CXF 进行服务身份验证

java - 基于日内瓦的 STS,Java 互操作

android - Ksoap 向后结果