从Android Studio视图中调用Asp.net Web API [英] Calling Asp.net web API from Android Studio View

查看:122
本文介绍了从Android Studio视图中调用Asp.net Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过android studio项目访问asp.net Web API时遇到问题.我的Web API通过实体框架与数据库连接.我想通过android商人视图通过API Merchant Controller调用商人列表.这是我的商人的HttpGet方法:

I have a problem while accessing asp.net web API through android studio project. My web API connect with the database through Entity Framework. I want to call the list of Merchants through API Merchant Controller from android merchant view. Here is my HttpGet method for Merchant:

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访问商家列表

I access list of Merchants through AsyncTask doInBackground method 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();
        }
    }

推荐答案

您应该将HttpsURLConnection更改为HttpURLConnection,因为您的URL使用协议http而不是https

you should change HttpsURLConnection to HttpURLConnection because your URL using protocol http not https

这篇关于从Android Studio视图中调用Asp.net Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆