[英]Pass ArrayList data into SOAP web service in android
您好,我需要將Array-list數據傳遞給soap Web服務。 到目前為止,我有以下代碼。
public class ResultActivity extends Activity {
public final String NAMESPACE = "";
public final String URL = "";
public final String SOAP_ACTION_1 = "";
public final String METHOD_NAME_1 = "";
ProgressDialog mProgressDialog;
SoapObject mSoapObjectCompanyDetailResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
System.out.println("Size In resxusr " + OnLineApplication.mParserResults.size());
for (int i = 0; i < OnLineApplication.mParserResults.size(); i++) {
System.out.println("ID " + OnLineApplication.mParserResults.get(i).getCompanyId());
System.out.println("Q " + OnLineApplication.mParserResults.get(i).getQuestion());
System.out.println("A " + OnLineApplication.mParserResults.get(i).getAnswer());
}
new insertResult().execute();
}
public class insertResult extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog = ProgressDialog.show(ResultActivity.this, "Wait", "Fetching");
}
@Override
protected Void doInBackground(Void... params) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_1);
// request.addProperty("dt","");
for (int i = 0; i < OnLineApplication.mParserResults.size(); i++) {
request.addProperty("CompanyID", 30);
request.addProperty("Question", OnLineApplication.mParserResults.get(i).getQuestion());
request.addProperty("Answer", OnLineApplication.mParserResults.get(i).getAnswer());
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION_1, envelope);
mSoapObjectCompanyDetailResponse = (SoapObject) envelope.bodyIn;
Object re = null;
re = envelope.getResponse();
Log.i("myApp", mSoapObjectCompanyDetailResponse.toString());
System.out.println("re " + mSoapObjectCompanyDetailResponse.toString());
// mStringCompanyID=re.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
}
}
我的XML WSDL服務如下。
<wsdl:types>
<s:element name="insertResultUser">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="dt">
<s:complexType>
<s:sequence>
<s:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.w3.org/2001/XMLSchema" processContents="lax"/>
<s:any minOccurs="1" namespace="urn:schemas-microsoft-com:xml-diffgram-v1" processContents="lax"/>
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="insertResultUserResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="insertResultUserResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:portType>
<wsdl:operation name="insertResultUser">
<wsdl:input message="tns:insertResultUserSoapIn"/>
<wsdl:output message="tns:insertResultUserSoapOut"/>
</wsdl:operation>
</wsdl:portType>
如上所述,我需要將以下數據結構傳遞給Web服務。
dt=anyType{DocumentElement=anyType{questions=anyType{CompanyID=1; Question=what is android?; Answer=OS; };
questions=anyType{CompanyID=1; Question=what is android?; Answer=OS; };
questions=anyType{CompanyID=1; Question=what is android?; Answer=OS; };
questions=anyType{CompanyID=1; Question=what is android?; Answer=OS; };
questions=anyType{CompanyID=1; Question=what is android?; Answer=OS; }; }; }; }; }
當我運行上面的代碼時,我無法將Arraylist數據發布到服務器。 在我的onCreate方法中,我能夠打印我的Arraylist值。 我怎么解決這個問題?
試試這段代碼:
SoapObject request = new SoapObject(Wsdl_Target_NameSpace,
Method_Name);
for (int i = 0; i < Property_Key.size(); i++) {
request.addProperty(Property_Key.get(i), Property_Value.get(i));
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = null;
androidHttpTransport = new HttpTransportSE(Url_location);
androidHttpTransport.call(Soap_Action, envelope);
SoapObject results = (SoapObject) envelope.bodyIn;
Vector response = (Vector) envelope.getResponse();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.