我有一個C#Webservice和一個Eclipse Android應用程序項目。我想從KSOAP2 2.3版web服務中獲得一個xml文件。該web服務工作,並讓我回到當前的XML文檔,它也似乎像我得到了與android應用程序的web服務的連接,但我總是會得到一個異常,說web服務無法識別HTTP標頭的值「SOAPAction」 。Java Android:使用SOAP從Webservice獲取Xml文件
的Webservice:
namespace WebService3
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// [System.Web.Script.Services.ScriptService]
class DBConnect
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
}
private void Initialize()
{
server = "localhost";
database = "eagles_db";
uid = "root";
password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
public XmlDocument abmeldungen()
{
XmlDocument doc = new XmlDocument();
XmlNode root;
XmlNode tempHeader;
XmlNode tempInhalt;
List<int> lstIDs = new List<int>();
List<string> lstNamen = new List<string>();
string ausgabe = string.Empty;
string conStr = ConfigurationManager.ConnectionStrings["dbstring"].ConnectionString;
string befehl = "Select abmeldung.cf_created, abmeldung.datum, abmeldung.grund, web_users.name From abmeldung Inner Join web_users On abmeldung.cf_user_id = web_users.id Order By abmeldung.cf_created Desc";
MySql.Data.MySqlClient.MySqlCommand cmd = new MySqlCommand(befehl, connection);
connection.Open();
MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader();
root = doc.CreateElement("Abmeldungen");
doc.AppendChild(root);
tempHeader = doc.CreateElement("Abmeldungen"); //doc.CreateElement(reader["reportTitle"].ToString());
root.AppendChild(tempHeader);
while (reader.Read())
{
tempInhalt = doc.CreateElement("Name");
tempHeader.AppendChild(tempInhalt).InnerText = reader["name"].ToString();
tempInhalt = doc.CreateElement("Grund");
tempHeader.AppendChild(tempInhalt).InnerText = reader["grund"].ToString();
tempInhalt = doc.CreateElement("datum");
tempHeader.AppendChild(tempInhalt).InnerText = reader["datum"].ToString();
tempInhalt = doc.CreateElement("erstellt");
tempHeader.AppendChild(tempInhalt).InnerText = reader["cf_created"].ToString();
}
reader.Close();
connection.Close();
return doc;
}
[WebMethod]
public XmlDocument abmeldung()
{
DBConnect con1 = new DBConnect();
return con1.abmeldungen();
}
一切工作在Firefox和我得到正確的xml文件
應用:
package de.maturaprojekt.sema;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
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.Toast;
public class Abmeldungen extends Activity {
public static String NAMESPACE = "http://tempuri.org/";
public static String SOAP_ACTION = "http://10.0.2.2:28266/Service1.asmx/abmeldung";
public static String METHOD_NAME = "abmeldung";
public static String URL = "http://10.0.2.2:28266/Service1.asmx";
public static String response;
public static String abmeldungen1()
{
//SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
return "Error";
}
public static String abmeldungen()
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
try
{
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
// SoapResult
SoapObject result = (SoapObject)envelope.getResponse(); //body.In
if (result != null)
{
response=result.toString(); //getProperty(0)
if(response.equals("false"))
{
return "Error";
//error,
}
else
{
return "IT WORKS";
// everything works correctly
}
}
else
{
return "ERROR2";
// no answer from webservice
}
}
catch (Exception exc)
{
Log.i("tom",exc.toString());
return "ERROR3";
// andere Probleme
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abmeldungen);
}
public void btn_abfragen(View view)
{
//Toast.makeText(this, "abgerufen", Toast.LENGTH_SHORT).show();
String return22 = abmeldungen();
Toast.makeText(this,return22, Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.abmeldungen, menu);
return true;
}
}
我用10.10.2.2作爲模擬的Android設備上,因爲本地主機正常的「本地主機」將是仿真設備的本地主機,10.10.2.2是正確的本地主機。
我希望我的問題不是太混亂,如果有人能幫助我,我將非常感激。謝謝
你是從哪裏在你的代碼複製肥皂行動? – kingAm
這是一個學校項目,我從一個較舊的項目獲取代碼,這與我的項目類似,他們嘗試以同樣的方式連接到Web服務。 – semaa