连接安卓和 asp.net 肥皂网络服务时出错
本文关键字:肥皂 网络服务 出错 net asp 连接 | 更新日期: 2023-09-27 18:30:49
我已经运行了我的 Web 服务,这是代码(我只是第一次尝试登录):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net.Http;
using System.Web.Http;
using MySql.Data.MySqlClient;
namespace WebService1
{
/// <summary>
/// Descripción breve de Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// Para permitir que se llame a este servicio web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string ConectarBaseDatos()
{
try
{
using (MySqlCommand cm = new MySqlCommand())
{
string connection = @"Server=localhost; Port=3306; Database=sistemadatosatletasfecovol;
Uid=****; Pwd=*****";
MySqlConnection cn = new MySqlConnection(connection);
cm.CommandType = System.Data.CommandType.StoredProcedure;
cm.CommandText = "ingresarUsuario";
cm.Parameters.Add("_email", MySqlDbType.VarChar).Value = "asdafa@gmail.com";
cm.Parameters.Add("_password", MySqlDbType.VarChar).Value = "*****";
cn.Open();
cm.Connection = cn;
cm.ExecuteNonQuery();
MySqlDataReader cr = cm.ExecuteReader();
cr.Read();
string id;
id = cr["mensaje"].ToString();
cn.Close();
return id;
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
}
}
它的工作,我实际上可以测试它并且很好,但是当我尝试将其与 andorid 连接时,我得到
Android java.net.connectexception 无法连接到
错误。
这是我的安卓代码
String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "ConectarBaseDatos";
String SOAP_ACTION = "http://tempuri.org/ConectarBaseDatos";
String URL = "http://localhost:8080/Service1.asmx";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
try
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
String result = response.getProperty(0).toString();
editText.setText(result);
}
catch(Exception e)
{
editText.setText(e.toString());
}
我正在使用本指南:http://seesharpgears.blogspot.in/2010/11/basic-ksoap-android-tutorial.html我正在自己的手机上测试它。
问题就在这里字符串 URL ="http://localhost:8080/Service1.asmx";
使用实时 IP 地址而不是本地主机在真实设备中运行,如果要签入模拟器,请将 localhost 替换为模拟器的 IP 地址 10.0.2.2。