从Android Studio视图调用Asp.net web API
我在通过android studio项目访问asp.net web API时遇到问题。我的web API通过实体框架与数据库连接。我想从android商户视图通过API商户控制器调用商户列表。以下是我针对商户的HttpGet方法:从Android Studio视图调用Asp.net web API,android,android-studio,asp.net-web-api,Android,Android Studio,Asp.net Web Api,我在通过android studio项目访问asp.net web API时遇到问题。我的web API通过实体框架与数据库连接。我想从android商户视图通过API商户控制器调用商户列表。以下是我针对商户的HttpGet方法: public class MerchantController : ApiController { private DostiCardDBEntities merchantEntities = new DostiCardDBEntities()
public class MerchantController : ApiController
{
private DostiCardDBEntities merchantEntities = new DostiCardDBEntities();
[HttpGet]
public HttpResponseMessage listOfMerchant() {
return Request.CreateResponse(HttpStatusCode.OK, merchantEntities.MerchantTables.ToList());
}
}
我通过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();
}
}
私有类ExecuteTask扩展异步任务{
字符串jsonText=“”;
httpsurl连接;
@凌驾
受保护的字符串背景(字符串…字符串){
试一试{
URL=新URL(“http://169.254.80.80:6040/api/Merchant");
connection=(HttpsURLConnection)url.openConnection();
connection.setRequestMethod(“GET”);
connection.connect();
InputStream InputStream=connection.getInputStream();
int字节字符;
而((byteCharacter=inputStream.read())!=-1){
char c=(char)bytechcharacter;
jsonText+=c;
}
}捕获(IOE异常){
e、 printStackTrace();
}
最后{
连接断开();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串s){
Toast.makeText(getApplicationContext(),jsonText,Toast.LENGTH_LONG).show();
}
}
默认情况下,C#Web Api无法从localhost
外部访问,即在LAN网络中访问。您需要做的是转到项目路径,在项目文件夹中有一个名为.vs
的文件夹,默认情况下是隐藏的(您可以通过更改文件夹和搜索选项设置来查看)
现在打开.vs
文件夹,转到config
文件夹,然后使用任何文本编辑器打开applicationhost.xml
文件
打开后,找出以下行
<bindings>
<binding protocol="http" bindingInformation="*:6040:localhost" />
</bindings>
如果您从api中得到一些响应,它将非常有效 您可以检查这是否是您的公共IP,或者检查它是否可以在您的域之外访问,因为您的设备应该在您的域之外,还可以检查您是否在清单中授予了internet访问权限 您应该将HttpsURLConnection更改为HttpURLConnection,因为您的URL使用的是协议http而不是https确切的问题是什么?您的应用程序是否崩溃、性能意外等。?请详细说明。当我使用HttpsURLConnection时,我的应用程序崩溃,当我使用HttpURLConnection或URLConnection时,onPostExecute方法Toast语句返回null@NarayanAcharyaYour endpoint is“http://..因此使用HttpsUrlConnection可能是不对的。当使用其他选项时,inputstream为null可能意味着请求未成功或未收到响应。因此,建议您在读取inputstream之前检查响应的状态。另外,在尝试此操作之前,请在浏览器中检查该端点是否有效。我是android新手。。如何检查响应的状态@Narayanacharya但你是否在浏览器中检查过该URL是否确实返回了某些内容?这是HttpUrlConnection的文档-。getResponseCode()是它拥有的方法之一。我看到您仍在使用HttpsUrlConnection。移动到HttpUrlConnection并使用getResponseCode检查它是否为200。如果是这样的话,你也许可以阅读内容。我已经做了。见下文。。。但我还是有个问题。。获取状态代码=400
我说了bindingInformation=“*:6040:*”
,它允许局域网内的所有IP地址访问Web API。在控制器中,将listOfMerchant()
更改为GetListOfMerchant()
,然后再试一次。在手机浏览器中检查此URL。我忘了提到,如果它不工作,请关闭防火墙。完全没有更改:(它不返回任何内容,状态代码为400)它返回HTTP错误400:请求主机名无效
<bindings>
<binding protocol="http" bindingInformation="*:6040:localhost" />
<binding protocol="http" bindingInformation="*:6040:*" />
</bindings>
http://169.254.80.80:6040