在 android 中使用 kSOAP 连接到网络服务
connecting to webservice with kSOAP in android
我正在尝试将 Web 服务与 kSOAP 库连接。代码向服务器发送 4 个字段,如用户名和密码、姓名和家庭,但在服务器中所有内容都收到 null !这是我的代码,问题在哪里?
WebService.java
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.util.Log;
public class WebService {
private static String NAMESPACE = "xxxxxxxxxxxx";
public final static String URL = "xxxxxxxxxxxx";
private static String SOAP_ACTION = "xxxxxxxxxxxxxxx";
private static final String METHOD = "Register";
public static String invokeWS() {
String resTxt = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD);
request.addProperty("username","user");
request.addProperty("password","pass");
request.addProperty("name","MyName");
request.addProperty("family","MyFamily");
Log.v("", ""+request);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope( SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION+METHOD, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
resTxt = response.toString();
} catch (Exception e) {
e.printStackTrace();
Log.e("",""+e);
resTxt = "Error !!!!";
}
return resTxt;
}
}
并在 MainActivity.java
中调用 invokeWS()
AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
displayText = WebService.invokeWS();
return null;
}
@Override
protected void onPostExecute(Void result) {
//Set response
Log.v("", "Recive : "+displayText);
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
将此行添加到您的代码中:
envelope.dotNet=true
我正在尝试将 Web 服务与 kSOAP 库连接。代码向服务器发送 4 个字段,如用户名和密码、姓名和家庭,但在服务器中所有内容都收到 null !这是我的代码,问题在哪里? WebService.java
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.util.Log;
public class WebService {
private static String NAMESPACE = "xxxxxxxxxxxx";
public final static String URL = "xxxxxxxxxxxx";
private static String SOAP_ACTION = "xxxxxxxxxxxxxxx";
private static final String METHOD = "Register";
public static String invokeWS() {
String resTxt = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD);
request.addProperty("username","user");
request.addProperty("password","pass");
request.addProperty("name","MyName");
request.addProperty("family","MyFamily");
Log.v("", ""+request);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope( SoapEnvelope.VER12);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION+METHOD, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
resTxt = response.toString();
} catch (Exception e) {
e.printStackTrace();
Log.e("",""+e);
resTxt = "Error !!!!";
}
return resTxt;
}
}
并在 MainActivity.java
中调用 invokeWS() AsyncCallWS task = new AsyncCallWS();
//Call execute
task.execute();
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
displayText = WebService.invokeWS();
return null;
}
@Override
protected void onPostExecute(Void result) {
//Set response
Log.v("", "Recive : "+displayText);
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
将此行添加到您的代码中:
envelope.dotNet=true