Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to run my .Net Web Api simple program on Android App. But it does not run I don't know what is going wrong. the following is the Visual Studio code:


C#
namespace MyFirstWebApi.Controllers
   {
     public class ValuesController : ApiController
      {
       // GET api/values
      public IEnumerable<string> Get()
       {
      return new string[] { "value1", "value2" };
       }

        // GET api/values/5
       public string Get(int id)
       {
      return "value";
        }

        // POST api/values
       public void Post([FromBody]string value)
      {
       }

       // PUT api/values/5
        public void Put(int id, [FromBody]string value)
       {
        }

        // DELETE api/values/5
        public void Delete(int id)
       {
       }
      }
    }

C#
I want to run my .Net Web Api simple program on Android App. But it does not run I don't know what is going wrong. the following is the Visual Studio code:

               namespace MyFirstWebApi.Controllers
     {
       public class ValuesController : ApiController
        {
         // GET api/values
        public IEnumerable<string> Get()
         {
        return new string[] { "value1", "value2" };
         }

          // GET api/values/5
         public string Get(int id)
         {
        return "value";
          }

          // POST api/values
         public void Post([FromBody]string value)
        {
         }

         // PUT api/values/5
          public void Put(int id, [FromBody]string value)
         {
          }

          // DELETE api/values/5
          public void Delete(int id)
         {
         }
        }
      }
And this is the Android stude code:

             srvr.setOnClickListener(new View.OnClickListener() {
                @Override
               public void onClick(View view) {
                  String url="http://192.168.56.1:8240/api/values";
            final RequestQueue requestQueue = 
               Volley.newRequestQueue(MainActivity.this);
            StringRequest stringRequest = new 
                  StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            srveces.setText(response);
                            requestQueue.stop();

                        }
                    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    srveces.setText("error");
                    error.printStackTrace();
                    requestQueue.stop();
                }

            });
            requestQueue.add(stringRequest);




             }

          });
I used ip but did'nt work.I used genymotion ip which 10.0.3.15 it also did'nt work. I used http://localhost:8240/api/values it also not work. It run on computer Browser correctly. But I want to show this simple text on my app textview


What I have tried:

I treid all the possible way.But still i am not getting any result.
Posted
Comments
Richard Deeming 10-Aug-16 9:12am    
"localhost" means the computer on which the code is currently running. Your .NET service isn't hosted on your Android device, so that won't work.

You need to use the correct IP address and port number for the computer where your service is hosted. If it's a private IP address, the Android device will need to be on the same network.
wiki john 10-Aug-16 9:55am    
Using ipconfig:
it show under ppp adapter ZTE Wireless Terminal:
IPv4 Address....119.154.167.103
And under Ethernet adapter VirtualBox Host-only network:
IPv4 Address.....192.168.56.1
SO which one is correct i used both of but not work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900