Calling Asp.net web API from Android Studio View

2019-09-06 22:53发布

问题:

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());
        }
}

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();
        }
    }

回答1:

By default C# Web Api isn't accessible out side the localhost i.e., in your LAN network. What you have to do is goto your project path and inside your project folder there is one folder called .vs which is hidden by default (you can see that by changing your Folder and Search options settings).

Now open .vs folder and goto config folder and there open applicationhost.xml file using any text editor.

After opening find out the following line

<bindings>
   <binding protocol="http" bindingInformation="*:6040:localhost" />
</bindings>

And update above line like this

<bindings>
   <binding protocol="http" bindingInformation="*:6040:localhost" />
   <binding protocol="http" bindingInformation="*:6040:*" />
</bindings>

Where, 6040 is your project's port address. Save and exit from editor. Now this allows you to access the Web Api throughout your LAN connection. (some times you have to start Visual Studio with Admin Privileges).

Now in your Mobile Phone open any browser and type the address like this

http://169.254.80.80:6040

If you get some response from your api it works perfectly.



回答2:

You could check if that is your public IP, or check if it is accessible outside your domain, as your device should be outside your domain, also check if you have granted internet access in your manifest.



回答3:

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