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

发布于 2024-12-28 07:58:58 字数 3264 浏览 0 评论 0原文

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

ex.getMessage() 在清单文件中打印“null”异常消息,

我已指定

 <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);

            } 
            }

I call webservice from andriod application, developed using eclipse,
but getting exception when calling
androidHttpTransport.call(SOAP_ACTION, envelope);

ex.getMessage() printing "null" exception message

in manifist file i have specified

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

following is complete code

///////////////////
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);

            } 
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南烟雨〆相思醉 2025-01-04 07:58:58

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

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

问候!

I had the same problem. I was getting a null exception because i was trying in the main thread. A problem with Honeycomb SDK or higher. http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

The solution, run the process in a new thread.

Greetings!

森林很绿却致人迷途 2025-01-04 07:58:58

您应该看到此Double with ksoap2 on Android?,将 Double 属性添加到您的肥皂请求中在 ksoap2 中。

You should see this Double with ksoap2 on Android? to add Double property to your soap request in ksoap2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文