Androidでデータベース用のSOAPのWebサービスを呼び出す方法.
私の Web サービスは C# の ASP.NET にあります。
Android の SOAP Web サービスに NewDataSet と Table の値が表示されない理由 回答してください。
$ パッケージ com.example.asp_qrcode;
import java.util.Hashtable;
import java.util.Vector;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.KvmSerializable;
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.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView output;
EditText treeid;
Button ok;
String s;
private final String NAMESPACE = "tempuri.org/";
private final String URL = "http://cafe-wb.com/QRCODE/service1.asmx";
private final String SOAP_ACTION = "http://tempuri.org/SP_GET_TREE_NAME_BY_TREEID";
private final String METHOD_NAME = "SP_GET_TREE_NAME_BY_TREEID";
String resultString;
SoapObject response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output = (TextView)findViewById(R.id.text2);
treeid = (EditText)findViewById(R.id.editText1);
ok = (Button)findViewById(R.id.button1);
///////////////////////////////////////////////////////////
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
s = treeid.getText().toString();
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo treeIDProp =new PropertyInfo();
treeIDProp.setName("TREE_ID");
treeIDProp.setValue(s);
treeIDProp.setType(int.class);
request.addProperty(treeIDProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//Log.i("Envelop: ",);
try {
//androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug=true;
androidHttpTransport.call(SOAP_ACTION, envelope);
response=(SoapObject) envelope.getResponse();
//response = (SoapObject)envelope.getResponse();
resultString= androidHttpTransport.responseDump;
// resultString = response.toString();
//resultString = ((SoapObject)response.getPropertySafely("sequence")).getPropertySafely("element").toString();
//SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
Log.i("Result: ",resultString);
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
output.setText(resultString);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
私のWebサービスの結果は次のとおりです。
<DataSet xmlns="http://tempuri.org/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="TREE_ID" type="xs:int" minOccurs="0"/>
<xs:element name="PARENT_ID" type="xs:int" minOccurs="0"/>
<xs:element name="TREE_NAME" type="xs:string" minOccurs="0"/>
<xs:element name="TYPE" type="xs:string" minOccurs="0"/>
<xs:element name="DISCRIPTION" type="xs:string" minOccurs="0"/>
<xs:element name="DATE_ADDED" type="xs:dateTime" minOccurs="0"/>
<xs:element name="Page" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<TREE_ID>56</TREE_ID>
<PARENT_ID>41</PARENT_ID>
<TREE_NAME>P.T.F.E Envelope Gaskets</TREE_NAME>
<TYPE>P.T.F.E GASKETS</TYPE>
<DISCRIPTION>
<p><strong>PTFE Envelope Gaskets</strong> provided by us are largely used in various industries in different processes and purposes. We offer these PTFE Envelope Gaskets in two types namely Milled type and slit type. These gaskets can have inserts of CAF, Rubber, Corrugated steel sandwiched between CAF rings, PVC. The clients can avail the PTFE Envelope Gaskets manufactured by us in varied specifications, shapes, and sizes as per their requirements.</p> <p> </p> <p><strong> </strong></p> <p><strong> </strong></p> <p><strong> </strong></p> <p><strong>Milled Type</strong></p> <ul> <li>It is made to fit the insert and presents a flat face to the flange</li> <li>Fused End envelopes are being manufactured from Diameters of 400mm to any size with a maximum wall thickness of upto 100mm</li> </ul> <p><br /> <strong>Slit Type</strong></p> <ul> <li>The envelope in this variety does not present a full face to the flange</li> </ul> <p>It is perfectly adequate for varied applications </p>
</DISCRIPTION>
<DATE_ADDED>2013-06-10T13:54:56.41+05:30</DATE_ADDED>
<Page>1</Page>
</Table>
</NewDataSet>
</diffgr:diffgram>
</DataSet>