我在通过asp.net studio项目访问android web API时遇到问题。我的web API通过Entity Framework与数据库连接。我想在android商户视图中通过商户控制器接口调用商户列表。以下是我的Merchant的HttpGet方法:
public class MerchantController : ApiController
{
private DostiCardDBEntities merchantEntities = new DostiCardDBEntities();
[HttpGet]
public HttpResponseMessage listOfMerchant() {
return Request.CreateResponse(HttpStatusCode.OK, merchantEntities.MerchantTables.ToList());
}
}I通过AsyncTask doInBackground方法i-e访问商户列表
private class ExecuteTask extends AsyncTask<String, Integer, String>{
String jsonText = "";
HttpsURLConnection connection;
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL("http://169.254.80.80:6040/api/Merchant");
connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream inputStream = connection.getInputStream();
int byteCharacter;
while ((byteCharacter = inputStream.read()) != -1){
char c = (char) byteCharacter;
jsonText += c;
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
connection.disconnect();
}
return null;
}
@Override
protected void onPostExecute(String s) {
Toast.makeText(getApplicationContext(), jsonText, Toast.LENGTH_LONG).show();
}
}发布于 2017-02-12 17:03:54
默认情况下,C# Web不能从localhost外部访问,也就是在局域网中。你所要做的就是进入你的项目路径,在你的项目文件夹中有一个名为.vs的文件夹,这个文件夹默认是隐藏的(你可以通过改变你的文件夹和搜索选项设置来看到它)。
现在打开.vs文件夹并转到config文件夹,在那里使用任何文本编辑器打开applicationhost.xml文件。
打开后,查找以下行
<bindings>
<binding protocol="http" bindingInformation="*:6040:localhost" />
</bindings>并像这样更新上面的行
<bindings>
<binding protocol="http" bindingInformation="*:6040:localhost" />
<binding protocol="http" bindingInformation="*:6040:*" />
</bindings>其中,6040是项目的端口地址。保存并退出编辑器。现在,您可以通过LAN连接访问Web Api。(有时您必须使用管理员权限启动Visual Studio )。
现在,在您的移动电话中,打开任何浏览器并键入地址,如下所示
http://169.254.80.80:6040如果你从你的api中得到一些响应,它就能完美地工作。
发布于 2017-11-22 20:20:10
您可以检查这是否是您的公共IP,或者检查它是否可以在您的域外访问,因为您的设备应该在您的域外,还可以检查您的清单中是否已授予互联网访问权限。
发布于 2018-01-02 03:33:44
您应该将HttpsURLConnection更改为HttpURLConnection,因为您的URL使用的是协议http而不是https。
https://stackoverflow.com/questions/42184846
复制相似问题