从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