我正在尝试使用此处的代码http://www.ibm.com/developerworks/webservices/library/ws-android/index.html?ca=dat-来调用Web服务。我一直在我的代码的下一行抛出异常......。
androidHttpTransport.call(SOAP_ACTION, envelope);
我写的课程如下:
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
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.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText editext1;
private EditText editext2;
private EditText editext3;
private EditText editext4;
private Button btn1;
private static final String NAMESPACE = "http://service.hotornot.com/";
private static String URL = "http://hotornot-hnwebservice.rhcloud.com/TomcatHotOrNot/messages?wsdl";
private static final String METHOD_NAME = "addMessage";
private static final String SOAP_ACTION = "http://service.hotornot.com/addMessage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editext1 = (EditText) findViewById(R.id.editText1);
editext2 = (EditText) findViewById(R.id.editText2);
editext3 = (EditText) findViewById(R.id.editText3);
editext4 = (EditText) findViewById(R.id.editText4);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo propInfo = new PropertyInfo();
propInfo.name = "arg0";
propInfo.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInfo, editext1.getText().toString());
PropertyInfo propInfo1 = new PropertyInfo();
propInfo.name = "arg1";
propInfo.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInfo1, editext2.getText().toString());
PropertyInfo propInf2 = new PropertyInfo();
propInfo.name = "arg2";
propInfo.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInf2, editext3.getText().toString());
PropertyInfo propInfo3 = new PropertyInfo();
propInfo.name = "arg3";
propInfo.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInfo3, editext4.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope
.getResponse();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
}
有趣的是 - 如果我调试代码并检查SoapObject的输出,它看起来如下似乎不正确 - 为什么键为null?我假设这是导致空指针异常的原因
addMessage{arg3=this; null=is; null=a; null=test; }
例外:
12-31 13:23:47.514: W/System.err(770): java.lang.NullPointerException
12-31 13:23:47.524: W/System.err(770): at java.io.Writer.write(Writer.java:141)
12-31 13:23:47.524: W/System.err(770): at org.kxml2.io.KXmlSerializer.startTag(KXmlSerializer.java:412)
12-31 13:23:47.524: W/System.err(770): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:581)
12-31 13:23:47.534: W/System.err(770): at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:566)
12-31 13:23:47.534: W/System.err(770): at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:623)
12-31 13:23:47.544: W/System.err(770): at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:547)
12-31 13:23:47.544: W/System.err(770): at org.ksoap2.SoapEnvelope.write(SoapEnvelope.java:192)
12-31 13:23:47.554: W/System.err(770): at org.ksoap2.transport.Transport.createRequestData(Transport.java:74)
12-31 13:23:47.554: W/System.err(770): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:58)
12-31 13:23:47.564: W/System.err(770): at com.example.hotornot.MainActivity$1.onClick(MainActivity.java:74)
12-31 13:23:47.564: W/System.err(770): at android.view.View.performClick(View.java:4204)
12-31 13:23:47.564: W/System.err(770): at android.view.View$PerformClick.run(View.java:17355)
12-31 13:23:47.574: W/System.err(770): at android.os.Handler.handleCallback(Handler.java:725)
12-31 13:23:47.574: W/System.err(770): at android.os.Handler.dispatchMessage(Handler.java:92)
12-31 13:23:47.584: W/System.err(770): at android.os.Looper.loop(Looper.java:137)
12-31 13:23:47.584: W/System.err(770): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-31 13:23:47.594: W/System.err(770): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 13:23:47.594: W/System.err(770): at java.lang.reflect.Method.invoke(Method.java:511)
12-31 13:23:47.604: W/System.err(770): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 13:23:47.604: W/System.err(770): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 13:23:47.604: W/System.err(770): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
这解决了您的问题:
PropertyInfo propInfo = new PropertyInfo();
propInfo.name = "arg0";
propInfo.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInfo, editext1.getText().toString());
PropertyInfo propInfo1 = new PropertyInfo();
propInfo1.name = "arg1";
propInfo1.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInfo1, editext2.getText().toString());
PropertyInfo propInf2 = new PropertyInfo();
propInf2.name = "arg2";
propInf2.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInf2, editext3.getText().toString());
PropertyInfo propInfo3 = new PropertyInfo();
propInfo3.name = "arg3";
propInfo3.type = PropertyInfo.STRING_CLASS;
request.addProperty(propInfo3, editext4.getText().toString());
说明:属性“name”和“type”的赋值是在错误的变量上完成的(总是第一个propInfo,但应该是propInfo1,...)。