gpt4 book ai didi

java - 从 android 调用 Web 服务 - NullPointerException

转载 作者:行者123 更新时间:2023-12-01 23:18:40 26 4
gpt4 key购买 nike

我正在尝试使用这里的代码 http://www.ibm.com/developerworks/webservices/library/ws-android/index.html?ca=dat-调用网络服务。我不断在代码的以下行抛出异常......

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 的输出,它看起来如下,这似乎不正确 - 为什么键为空?我认为这就是导致空指针异常的原因

addMessage{arg3=this; null=is; null=a; null=test; }

异常(exception):

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)

最佳答案

这解决了您的问题:

            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,...)。

关于java - 从 android 调用 Web 服务 - NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858159/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com