我已经运行了我的网络服务,这是代码(我只是首先尝试登录):
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 我正在用自己的手机测试它。
答案 0 :(得分:0)
问题出在这里 String URL =&#34; http://localhost:8080/Service1.asmx&#34 ;;
使用实时IP地址而不是localhost在真实设备中运行,如果要检入模拟器,请将localhost替换为10.0.2.2,这是模拟器的IP地址。