在android中用kSOAP连接到webservice

时间:2015-11-18 12:33:06

标签: java android android-ksoap2

我正在尝试将Web服务与kSOAP库连接。代码将4个字段(如用户名和密码,名称和系列)发送到服务器,但在服务器中,所有内容都为空!这是我的代码问题在哪里? 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) {
}
}

1 个答案:

答案 0 :(得分:2)

将此行添加到您的代码:

envelope.dotNet=true