Android 尝试使用kSoap方法连接到web服务时出现NullPointerException
我的web服务应该返回一个整数,但每次运行代码时都会出现NullPointerException错误。 任何想法或帮助都将不胜感激 这是我的密码:Android 尝试使用kSoap方法连接到web服务时出现NullPointerException,android,web-services,soap,ksoap2,Android,Web Services,Soap,Ksoap2,我的web服务应该返回一个整数,但每次运行代码时都会出现NullPointerException错误。 任何想法或帮助都将不胜感激 这是我的密码: public class CGCountTest extends Activity { TextView testTV; private static final String NAMESPACE = "http://passport-america.com/webservices/"; private static fi
public class CGCountTest extends Activity {
TextView testTV;
private static final String NAMESPACE = "http://passport-america.com/webservices/";
private static final String URL = "http://localhost:11746/Service1.asmx";
private static final String SOAP_ACTION = "http://www.passport-america.com/webservices/getCGCount";
private static final String METHOD_NAME = "getCGCount";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.soap_test);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
java.lang.Integer result = (Integer)envelope.getResponse();
TextView testTV = (TextView)findViewById(R.id.testTV);
result.toString();
testTV.setText(result);
}
catch(Exception e)
{
testTV.setText(e.getMessage());
}
}
这是日志
06-02 15:13:36.557: WARN/dalvikvm(326): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
06-02 15:13:36.557: ERROR/AndroidRuntime(326): Uncaught handler: thread main exiting due to uncaught exception
06-02 15:13:36.876: ERROR/AndroidRuntime(326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pa.passammain/com.pa.passammain.CGCountTest}: java.lang.NullPointerException
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.os.Looper.loop(Looper.java:123)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.app.ActivityThread.main(ActivityThread.java:4203)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at dalvik.system.NativeStart.main(Native Method)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): Caused by: java.lang.NullPointerException
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at com.pa.passammain.CGCountTest.onCreate(CGCountTest.java:46)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
06-02 15:13:36.876: ERROR/AndroidRuntime(326): ... 11 more
我认为我的url字符串可能是问题所在,但我尝试使用我的ip时运气不佳这不是您的url字符串(它可能不正确,但它是一个有效字符串),您正在使用的一个函数调用将null作为输入参数之一。很可能是因为您试图执行返回null的操作,然后在另一个方法调用中使用该值
我不知道代码第326行有什么内容,但我敢打赌其中一个对象为null,所以我会首先检查它
如果您想粘贴第326行,我可以提供进一步的帮助。我处理过类似的情况,下面的代码没有任何错误
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "getOutletCount";
String SOAP_ACTION = "http://tempuri.org/getOutletCount";
String URL = "http://10.20.10.80/dinotawebsrv/GetDataWebService.asmx";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
System.out.println("web service call completed......");
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
return Integer.parseInt(response.toString());
试着调试它,检查您是否在响应中得到任何结果,而且我认为您不能直接将响应转换为整数。祝你好运。谢谢你这么快的回复!有趣的是,在这个特定的类文件中,我只有49行代码。我错过什么了吗?如果可以的话,我对android开发还是比较陌生的。326是进程ID,不是行号。java:46是堆栈跟踪指示的内容。