简体   繁体   中英

Calling Asp.net web API from Android Studio View

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 ie

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

By default C# Web Api isn't accessible out side the localhost ie, 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.

您可以检查这是您的公共IP,还是检查它是否可以在您的域之外访问(因为您的设备应该在您的域之外),还可以检查清单中是否已授予Internet访问权限。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM