我的需求:将我的Android设备连接到本地SQL Server数据库
我的问题:尝试连接时发生异常(“发送请求时发生错误”)
我使用的是:Visual Studio 2017和带有MVVM架构的Xamarin Forms。
什么有效:我从Postman做一个POST,它完美地工作,返回用户的令牌
我的项目使用REST API连接到数据库。 API发布为FileSystem并与IISExpress一起运行,正如我所说,Postman可以使用它
我试过了:
http://localhost:50965
http://192.168.1.106:50965(我的PC IP)
我在API Web.config上的连接字符串
<add name="DefaultConnection" connectionString="Data Source=192.168.1.106; Initial Catalog=Gesuro; Integrated Security=True; User ID = sergio; Password=xxxxxxxx" providerName="System.Data.SqlClient" />
我对API的调用:
var token = await this.apiService.GetToken(
"http://localhost:50965",
this.Email,
this.Password);
我的API方法:
public async Task<TokenResponse> GetToken(
string urlBase,
string username,
string password)
{
try
{
var client = new HttpClient();
client.BaseAddress = new Uri(urlBase);
var response = await client.PostAsync("Token",
new StringContent(string.Format(
"grant_type=password&username={0}&password={1}",
username, password),
Encoding.UTF8, "application/x-www-form-urlencoded"));
var resultJSON = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<TokenResponse>(
resultJSON);
return result;
}
catch (Exception e)
{
var pepe = e.Message;
return null;
}
}
我一直在谷歌搜索三天并尝试不同的解决方案......没有结果
我希望我对我的查询有所了解。请帮忙吗?
提前谢谢。
答案 0 :(得分:2)
尝试更改compile using Android version (Target Framework)
更改它android 6.0可能工作
我面临同样的问题,改变目标框架为我解决了这个问题。
答案 1 :(得分:0)
您还没有提供足够的详细信息,但我会猜测。 您的数据库服务器正在您的PC上运行,然后运行
$ adb reverse tcp:50965 tcp:50965
上面,然后当你访问
localhost:50965
在您的设备上,它将被重定向到您PC上的同一端口
如果您想测试reverse
是否有效,请执行
$ adb reverse tcp:3456 tcp:80
如果您在设备上打开 Chrome 时在PC上的端口80上运行网络服务器并输入
http://127.0.0.1:3456
您将看到主页