How to call ASP.Net Web API from Android Studio?

Tan Woon How

He is a learner and know little around .Net and Java. He learned and share if you see his mistake... feel free to comment and laugh at him... and also please help to correct him.

You may also like...

  • Mohammad Etedali

    How can I set timeout for Retrofit, sometime I received timeout connection error

  • malathi puttaiah

    I am working on sql server 2016, visual studio 2017, .net core api for webapi. android studio,gennymotion emulator for webapiclient.Followed your steps, but i get http error 503 service unavailable. Can you demonstrate coding for this setup.
    I am not getting it from months.

  • Pingback: How to call ASP.Net Web API from Android Studio? | Web API()

  • nyapz

    Can we put a custom parameters in this method? @POST(“/institute/Students”)
    public void addStudent(@Body Student student,Callback callback);
    And if possible, how?.. for ex. @POST(“/api/sms”)
    void addSMS(@Path(“mobileno”) String mobileno, @Path(“message”) String message,Callback callback);.. thanks in advance..

    • Tan Woon How

      Hi,
      Yes, is possible. If i’m not get you wrong. You could add parameter in the method like so
      void addSMS(@Field(“id”) String id,Callback callback);

      • nyapz

        Hi,

        Thank you for this information! It really helps me.

  • Χ—Χ Χ” Χ§ΧΧ”ΧŸ

    this is the message I get when i’m debuging
    java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 1149) after 10000ms

    • Tan Woon How

      Ok. I guess you choose iis express running your server. Then you need to make sure you read this http://instinctcoder.com/how-to-debug-asp-net-web-from-android-studio-with-retrofit/.

      But if you use Retrofit 2.0 then refer to this https://www.dropbox.com/s/muh55cef0fleswc/WebAPIClient_Retrofit2.rar?dl=0

      • Χ—Χ Χ” Χ§ΧΧ”ΧŸ

        I’m actually using that app of yours with the retrofit2

        • Tan Woon How

          Are you able to browse the same url from your emulator browser? And make sure able to browse from pc browser also.

          Turn off your firewall and anti virus and try. Let me know the result. Cheers

          • Χ—Χ Χ” Χ§ΧΧ”ΧŸ

            I browse on genymotion and my server is running I really don’t know whats the problem

          • Tan Woon How

            Hmmm… This can be very hard to know what is going on unless i have a look on your environment. What is the URL you browse and you put in the code (RestService.java)?

          • Χ—Χ Χ” Χ§ΧΧ”ΧŸ

            http://localhost:1149/api/GetOffice
            when I browse in my regular bowser it works

          • Tan Woon How

            May be you trying to get a big data from server causing the timeout?

          • Χ—Χ Χ” Χ§ΧΧ”ΧŸ

            finally I got the solution!
            when you browse on genymotion you have to make the url 10.0.3.2

          • Tan Woon How

            10.0.3.2???? First time i heard of this? you mean 10.0.2.2.

          • Χ—Χ Χ” Χ§ΧΧ”ΧŸ

            I actually mean 10.0.3.2 thats only for genymotion emulator

          • Tan Woon How

            Thanks for letting me know, i just tried myself and yes is 10.0.3.2 for localhost. I usually switch between Android Emulator and Genymotion. And it seem like i seldom use Genymotion :). Since Android Emulator 2.0, i really seldom use Genymotion.

          • Χ—Χ Χ” Χ§ΧΧ”ΧŸ

            anyway, I have a new problem the app stopes when it comes to TextVeiw.setText

          • Tan Woon How

            what is the error?

  • Χ—Χ Χ” Χ§ΧΧ”ΧŸ

    Im using your app as the base to my project and it’s gr8!
    but it fails to connect to the api don’t know why

  • Norman

    Another semester, and I find myself here again for a refresher πŸ™‚
    I just have a question however. This time I have an existing MVC5 app (not web service or web api)

    Is it possible for me to utilise these existing controllers instead of creating a Web API app just to recreate all of this existing functionality? Thanks and keep up the great work!

    • Tan Woon How

      Yes is possible . It actually work the same. Remember to decorate your function with http get or post attribute

  • Me

    great job Tan. I spent 2 days on HttpUrlConnection without concrete success until i got here. Thanks!

    • Tan Woon How

      My site must must be in low ranking. took you 2 days to reach me πŸ˜€

  • advanex 123

    Hi Sir,

    I already created the app.
    1) I can retrieve the data from my SQL Server.
    2) I can Add Student but after I click “SAVE” button, system prompt “New Student Inserted” but new student did not update on the server.

    How should i do it, please advice.
    Thanks

  • Ivan Martin

    amazing guide
    all of my method works on azure server
    but i have 1 problem. when i try to enter new entry, the id is 0 and i cannot add new entry if the id is not 0? so if there is an entry with id=0 then i have error which said 409 error

    • Tan Woon How

      make sure your id column is set to identity in the server

      • Ivan Martin

        I used azure as my server, can i set the identity after i publish it to azure?

        • Tan Woon How

          Yes you can as long as you able to conect to database you able to change it. Use Alter table syntax.

          • Ivan Martin

            may i know how to get all the data without using the listview? for example delete a data using email without generate the listview and click on the item. if java, i normally collect all the data and put it on the array list and then if i want to search the id i just simply search it using the row number the email had and then generate the delete

          • Tan Woon How

            Yes, you could do it, Android is just subset of Java. If java could do, then you could do this in Android also.

  • Zeeshan

    Thanks for amazing guide……….
    I post my data android to server but for
    Android app i m using emulator i used 10.0.0.2 it works but doesnt work on mobile i used on mobile my network ip but not working
    How can i access from android mobiile?

    • Tan Woon How

      Make sure you are able to browse from another machine browser before try from phone.

      Temporary turn off the firewall and see will that help.

      Also you need to deploy it to iis not IISExpress.

      Good luck

      • Zeeshan

        i deployed on iis but on the phone its give me retrofit 192.19……. is not connectd

        • Tan Woon How

          Hmmm….i’m not sure, have you tried to debug from your device and see what is the error. Are you able to browse the URL from another machine?

          • Zeeshan

            NO i am able to browse from another machine browser and also phone browser….only able to browse my machine where i deployed

          • Tan Woon How

            Base on your description, mostly like you’ve configured the wrong end point in your InstituteService.java or RestService.java.
            Please double check what you have configure

            1. InstituteService.java

            //i.e. http://localhost/api/institute/Students
            @GET(“/institute/Students”) <———————————————————Start with "/"
            public void getStudent(Callback<List> callback);

            2. In RestService.java, private static final String URL = “http://10.0.2.2/api/”; <–End with "/"

            is nessecary that machine connected with LAN?
            You mean connect with cable? Or do you mean you want to publish it outside of your LAN? Is doesn't matter where you host it, as long as you manage to browse with the configured URL from you phone/machine then it should be able to access from your code.

          • Zeeshan

            1.this is my interface

            public interface IEmployeeGps {
            @POST(“/api/Employee”)
            public void AddEmployee(@Body Gps emp, Callback callback);

            }

            2.Restservice.java

            public class RestService {
            private static final String URL = “http://10.0.2.2:802/”;

            i did this and the url only accesable on my machine but not another machine….

          • Tan Woon How

            I don’t mean to ask you to set this IP(10.0.2.2) in RestService.java “http://PUT_YOUR_IP:802/”;

            I assume you had already try with your IP address but still failed when try this app on your phone. Not much i think can help now. As i see, it should be running by now.

            You need to debug your code from your device. Set a break point and see what error return. and make sure you have set full log in RestService.java, when setting up the restAdapter.

            .setLogLevel(retrofit.RestAdapter.LogLevel.FULL) //Take note this should be set only in development.

            Let me know what is the error you get.

          • Zeeshan

            I find 1 thing that i am using same network and my machine and phone both connected with same wifi
            but my pc ip is different and phone ip is different so i mean thats why mobile is not able to browse my service url.

            Am i right? and both should have the same ip address is it? to acces on mobile

          • Tan Woon How

            When you check you network configuration you should get something like this on your machine
            IP address:192.168.0.188
            subnet mask:255.255.255.0
            default gateway:192.168.0.1

            All devices (Com, Laptop, Phone) should have different IP but same subnet mask and default gateway.

            Did you manage to get error in the logcat window?

          • Zeeshan

            05-20 15:28:48.353 23357-23684/com.example.zeeshanhassan.GpsI/System.out: [CDS]connect[/192.168.0.2:802] tm:90

            05-20 15:28:48.355 23357-23684/com.example.zeeshanhassan.GpsD/Posix: [Posix_connect Debug]Process com.example.zeeshanhassan.imagewservice :802

            this error occur when i connect….coonection timeout

          • Tan Woon How

            Need more. Did you see any error message?

            Could you paste everything?

            Beside that could you use port more than 3 digits?for example:8081

          • Zeeshan

            error from logcat file

          • Tan Woon How

            Ok. Please change port number 802 to 8020. And try again.

          • Zeeshan

            I tried too many port i changed it but same error occur…

            if i am not able to browse from another machine my url it means that its not work in my code.so now what i can try to…..run on real device

          • Tan Woon How

            Is not code problem. Is your machine.

            Things i will do this happened
            1. Off firewall
            2. Off antivirus
            3. Grant full permission to that folder

          • Zeeshan

            i did all the things that you said but error is same..

          • Tan Woon How

            Hmmm..is hard for me to give advise if the problem that is not from the code. Which windows are you using ?

            4. Off UAC see that helps but is not advice to do so just see it able to get through

          • Zeeshan

            Window 8.1

          • Tan Woon How

            Hmm..If you don’t mind to let me remote in your machine to have a look but not not i could help. I’m not sure what else got you stuck. So if you ok, you could install TeamViewer if your machine don’t have and send me id and password to admin[a]instinctcoder[dot]com.

            Cheers :).

          • Zeeshan

            Thanks bro…….i am able to browse my url from mobile and another machine.now my app is running on mobile. the error i got i used post method so dont need to include api/controller just write ip:port and able to browse and also it was firewall problem……

            Very very special thank bro for ur kind response πŸ™‚

          • Tan Woon How

            Damn…i been treated like this 2 times in this week from 2 different guy. ….hahaa. hapy for you have solved it and i learned too. Should ask the basic question first. :). Cheers.

  • Nitay Tubi

    Hi,
    It was very helpful to read your article, good job πŸ™‚
    I have a really weird problem.
    I published my webapi into IIS, and browsed to it from my emulator browser by using ip 10.0.2.2 as you mentioned.
    But, when I am trying to execute httprequest from my java code (By HttpClient object and not retrofit), I receive the “Connection refused” error.
    How is it posible that the emulator browser does access the my webapi and the java code doesn’t ?
    Thanks a lot πŸ™‚

    • Tan Woon How

      HI Nitay,
      Is hard for me to give you advise what causing you the error. So is that possible for you to share some portion of the code so that i could have a look.

      Beside, I think most likely you have configure wrong endpoint in your interface. (For my example is > InstituteService.java)

      So please make sure you have correct end point, and for testing purpose just call the endpoint with no parameter first and try again.

      Beside there is another you could debug the code into Visual Studio environment from Android Studio which you does not require to deploy your Web into IIS, have a look on this post http://instinctcoder.com/how-to-debug-asp-net-web-from-android-studio-with-retrofit/

      Good Luck.

      • Nitay Tubi

        Hi Tan,
        Thanks for the quick reply.
        Actually, I don’t use retrofit so I don’t have an interface, but here is a piece of my code:

        —————————————————————————————————————————

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);
        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());
        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader(“Accept”, “application/json”);
        httpPostRequest.setHeader(“Content-type”, “application/json”);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
        HttpResponse response = httpclient.execute(httpPostRequest);

        —————————————————————————————————————————

        The error occurred in the execute command.
        Im sure that the URL is correct, because it works in browser as i said, so its not the problem.
        Thanks again πŸ™‚

        • Tan Woon How

          Hi,
          I don’t see problem with your code. I guess is the URL you passed in causing the issue?
          Beside is there a reason you put this line –>StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());

          • Nitay Tubi

            Hi,
            Let’s forget my problem !
            I was refactoring my entire code in order to use retrofit like in your post.
            Now, everything seems to work very well, my app receives data from server with and without parameters and sends data to server as well. I can also debug it and it’s very simple.
            Retrofit really makes our life easier as you said, I used to have many problems with the connection beetween android and webapi, json converts, debugging and more.
            Your post solved me everything.
            Thank you so much for your help, this post is one of the best posts i have ever read and thanks to you i gonna keep visit this site.
            You are doing a great job, keep it up πŸ™‚
            Nitay

          • Tan Woon How

            What!!you give up on your own code. That’s a wise choice…lol. i used to have this issue before bu using retrofit all this problem gone from me long time ago.

            Beside there is a retrofit version 2 beta mau be you should considered change to 2 as the havw more feature that version 1 don’t have. I will update to when they have stable version. https://www.dropbox.com/s/muh55cef0fleswc/WebAPIClient_Retrofit2.rar?dl=0

  • Pingback: Google()

  • iS_Tom

    Hi. Using your example, I would like to send to the “login” server to check there is a user or not. If so, you get the answer that the user found.
    Here is some code:

    // Code in LoginActivity, login is of type String.
    restClient.getService (). findLogin (Login, new Callback ()
    // Code in UploadServise
    @GET ( “/ Login”)
    void findLogin (@Path ( “Login”) String Login, Callback callback);

    But what error: “error: retrofit.RetrofitError: UploadService.findLogin: URL” / login “does not contain” {Login} “(parameter # 1).”

    Please help πŸ™‚

    • Tan Woon How

      Hi
      You need to check your server code. Do you have Login (String Login) function in the server?

      • iS_Tom

        Thank you for your responsiveness) Problem solved)

  • Zahoor Ahmad

    Great Post… Thanks alot. For Begginers perspective, it is one of the great post to take a start… many thanks and keep it up…

    • Tan Woon How

      Seriously!!! Your comment make me alive to continue blogging!!! Thanks πŸ™‚

      I’m happy you find it useful.

  • Russell Esmedilla

    I found some issue, upon converting datetime using Gson. I tried this tutorial and everything is working fine. But when my API is returning a datetime, Gson failed to convert. Is there any solution for this. The error is, retrofit.converter.COnversionException: com.google.gson.JsonException: 2016-02-27-T13:12:13.19

    • Tan Woon How

      To be honest. i really hate deal with date type,

      The easiest way
      if I have any option i will change it to string type. Meaning when return from server format it to 2016-02-17 13:12:13.19 (Or any format you preferred) then you have no headache at client. But is still depend on your app, whether it is support multi timezone or not.

      The longest way
      You need to add your own customdateadpter to reigster the registerTypeAdapter, for example

      Gson localGson = gsonBuilder().create();
      restAdapter = new retrofit.RestAdapter.Builder()
      .setEndpoint(URL)
      .setConverter(new GsonConverter(localGson))
      .setLogLevel(retrofit.RestAdapter.LogLevel.FULL)
      .build();

      public static GsonBuilder gsonBuilder() {
      return new GsonBuilder()
      .registerTypeAdapter(Date.class, new MyCustomDateAdapter());
      }ο»Ώ

      public static class MyCustomDateAdapter{
      //Your logic of converting goes here
      }

      Hope it’s help

  • Russell Esmedilla

    Hi, this is really a very helpful post. Can you make the same using the Retrofit 2.

    Thank you.

    • Tan Woon How

      Hi,
      Thanks for dropping by, i haven’t look into Retrofit 2 yet, but could you tell me what is the major different?

    • Tan Woon How

      OK, just did a a quick look on the API, it seem much easier to make async call now, and cancellation during ongoing transaction…. Thanks for your questions, make me aware of new version! I’ll put in my list. Stay tuned. I’m 6 months late on this new version !!!

      • Russell Esmedilla

        Thank you. I will be waiting for your next post.

        • Tan Woon How

          Hi Russel

          I did a quick update on the existing code to Retrofit 2.0.0.0 beta.4. I think everything is quite straight forward please get from this link https://www.dropbox.com/s/muh55cef0fleswc/WebAPIClient_Retrofit2.rar?dl=0

          • jackie

            Hi, Tan.

          • jackie

            When I was trying to get data using web api, it shows that the data didn’t convert from json to string, Could you please enlighten me on how to fix that problem?
            thanks

          • Tan Woon How

            Could you show me your code? Both server and api interface

  • Delice Kapongo

    Hi there

    Thank you for this post. I was wondering if you can assist me in deploying the API on a public host and access it in the app using real device.

    Please assist

    • Tan Woon How

      Hi
      Let me know what you need me to help πŸ™‚

  • ι™ˆε˜‰δΌŸ

    Hi, Nice tutorial bro…I am new to this field and I follow your steps, when I click get all it did not show anything and click add student it crash in android client….I can browse the website via localhost/institute/students. Can you please tell me which step I did wrong… I have also try changing the route method to http://localhost:62313/api/institute/students and it still wrong..

    01-15 08:01:49.028 2047-2063/com.example.stan.fyp D/Retrofit: java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 80) after 15000ms

    at libcore.io.IoBridge.connectErrno(IoBridge.java:169)

    at libcore.io.IoBridge.connect(IoBridge.java:122)

    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)

    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)

    at java.net.Socket.connect(Socket.java:882)

    at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174)

    at com.android.okhttp.Connection.connect(Connection.java:152)

    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)

    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)

    at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)

    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)

    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)

    at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)

    at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)

    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)

    at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)

    at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)

    at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

    at retrofit.Platform$Android$2$1.run(Platform.java:142)

    at java.lang.Thread.run(Thread.java:818)

    • Tan Woon How

      Hey Bro,

      Thanks for dropping by.

      1. When access from emulator or apps, please use 10.0.2.2, so i assume you able to browse this URL From your EMULATOR browser, http://10.0.0.2.2:62313/api/institute/students or http://10.0.2.2/api/institute/students.

      2. If you like to debug with MS Visual studio or IIS express you need to change a little in RestService.java, detail please refer to http://instinctcoder.com/how-to-debug-asp-net-web-from-android-studio-with-retrofit/

      Please make sure the above is working first before proceed and the error message, it seem to me, it’s take too long come back from server because can’t connect to your server –> ( failed to connect to )

      Cheers
      Happy trying!! πŸ™‚

      • ι™ˆε˜‰δΌŸ

        Hey bro, thanks for your kindly reply….

        I am really new here, and can you please tell me how I can use 10.0.2.2..It seems that I cannot access either address you posted…Sorry for the stupid question..

        • Tan Woon How

          Do you have this project? http://instinctcoder.com/how-easy-to-start-with-asp-net-web-api-2-0/. Run in your computer and you will have localhost URL and replace localhost with 10.0.2.2, that’s all.

          • ι™ˆε˜‰δΌŸ

            Yes I have that project and I fail to access from the Emulator in Android Studio Android version 5.1…it is a bit wired that it shows connection time out in Emulator browser …

          • Tan Woon How

            Hmmm.. do you turned on your antivirus software or firewall? i not sure whether this causing the issue you encountered. Try stop all of them and still can’t may be turned off UAC temporary see it work or not .

            Also you tried both port 80 and 62313? For port 80 you need deployed into IIS.

            Let me know if you still having the same issue.

            Cheers

          • ι™ˆε˜‰δΌŸ

            Thanks a lot Bro for your continuous updating, I fix that problem after I deployed to Microsoft Azure~
            Now I am considering changing the class “student” to “Shipment” but it won’t show the data on list, (My server side table is also Shipment). is it because the Retrofit transformation? I searched google for a long time while I really cannot figure it out=.=

          • Tan Woon How

            Great! You found the solution. I had figured out the easiest way to Debug into ASP.net environment, please feel free visit here http://instinctcoder.com/how-to-debug-asp-net-web-from-android-studio-with-retrofit/

            For front end, if you have more time, i would suggest you to use AngularJS. and please take note, currently this framework is upgrading to version 2.0 and still not stable yet. But, for me, i still learning Angular version 1.0. because i still prefer the stable version πŸ˜› in production and keep eyes on version 2. Is cool framework. The moment you started with angularJS you never want to go back to .Net MVC Razor.

            But you if you look for quick start-up, it might be good you use the .Net MVC Razor + jquery.

          • ι™ˆε˜‰δΌŸ

            Thanks a lot !!!You are right bro, angularJS is awesome and I finished my front end within a week!! (just a small project).
            While I successfully follow your tutorial to construct the Android client, I would like to add one more function that is accepting a customer signature from android as bitmap and upload it to web api. Do you have some suggestion about how to implement the backend api ? Is it better to store it in file system or format it in byte[] that could be store in SQL database…..I am quite struggle with it since I just get in touch with ASP.NET less than two month…

          • Tan Woon How

            Wtf!!??? 1 week? It’s took me 2 months!!! You must be a genius!!!lol.
            Not sure is this what you looking for ?http://instinctcoder.com/android-studio-upload-any-files-to-asp-net-web-api/

          • ι™ˆε˜‰δΌŸ

            Thanks, I just followed some nice tutorial like yours for developing AngularJS.
            For the file upload, actually I would like to upload a image that has one-to-one relation to entry of other table and get those uploaded image by id and display in web front end, thus I was wondering is it better to store the image in file system and in SQL database each entry consist of Id and location, or just convert to binary data byte[] and store in database?

          • Tan Woon How

            It’s really depend. But for your case, store location in db will be good enough.

          • Tan Woon How

            Hi,

            I replied Zeeshan Hassan below on the problem accessing URL from emulator, hope that help you too, Read below πŸ™‚

  • Zeeshan Hassan

    I have follow all steps………but i got 1 error.
    error is “404 server error” what is this i don’t understand……….

    • Tan Woon How

      Hi
      It’s server error make sure you able to browse your url from browser

      • Zeeshan Hassan

        Yes i am able to browse but when i click the add button it should shows the condition either succesful or not but it shows 500 internal server.

      • Zeeshan Hassan

        And i publish my service from visual studio to iis but database is not publish or deploy in iis.and in android studio i found a error.error is connection is failed databse doesnt exist….now what should i do i tried to many things to deploy my VS13 service but databse is not publish with application……………….

        • Tan Woon How

          Hi

          For the database you need to create manually, sorry i did not make the project to auto create the database, as i just aim to create a simple service . So please go to this link http://instinctcoder.com/how-easy-to-start-with-asp-net-web-api-2-0/, get the script from step 1 and run in sql server, if you didn’t run before.

          After that you need to check your database connection setting in Web.config file and look for this string

          I believe the below error 500 is because of the database.

          Try 1 more time :). if you still can’t i’ll make the project to able create the database for the first run. (But go to wait till Monday) πŸ™‚

          Video… no i don’t have, try search online it should be standard,try first, if you still need then i do it Monday also.:)

          • Zeeshan Hassan

            Thanx dear i am trying this……………nice to see ur brief response………

          • Tan Woon How

            Ok, that’s great, then before attempt to run from emulator/apps, try from fiddle first. and it everything work fine then you should able to call from the app.

            Go to this link again, http://instinctcoder.com/how-easy-to-start-with-asp-net-web-api-2-0/. In this post, i use fiddler to communicate with the server WEB API, so make sure you’re able to use fiddler to call the API. if fiddler work fine then it should be good to PLAY! πŸ™‚

          • Zeeshan Hassan

            yes when i use fiddler it works……but for connecting the server might be issue.i am trying,80% i understand will get result soon……….

          • Tan Woon How

            Sorry, i don’t get you, “what you mean by connecting server might be issue”?

            If you able to use fiddler to sent request mostly you are able to call it from apps.

          • Zeeshan Hassan

            when i publish my website in iis.it doesnt browse…….
            using localhost get the url.so problem is using iis.without server can i replace localhost url with 10.0.0.2 in emulator browser? i did but url failed to find the web api service page……in emulator browser

          • Tan Woon How

            Ok, i try not to confuse you which URL to use, because that end up you going to crazy :). So first, we
            stick back to this tutorial use the DEPLOYED URL. I provide you the video, hope you follow the method that usually I do for deploying WEB into IIS.

            1ST Method:
            Check from this link https://www.dropbox.com/s/oub02r8zfvano9o/WebAPI.mp4?dl=0

            If above reallyX100 can’t work for you, which i think you can i hope you try whatever possible first before come to 2nd one which involve few more step which going to confuse you further.

            2ND Method: Yes, you able to browser IIS Express’s URL FROM EMULATOR but you need to perform these step
            1. Run your VS studio 201X as Administrator
            2. go to this file C:Users[yourusername]DocumentsIISExpressconfigapplicationhost.config

            2. Find this port number 49332 (if you run using my downloaded VS project, you should have this port number, if yours different then please find your port number)

            3. Add one more line into the config and it will look like this

            4. Start your project and then try this url from EMULATOR’s browser. http://10.0.2.2/49332/institute/Students.

            It should work.

            Try again. πŸ™‚

  • Uzair Qurashi

    Thanks for the post , looks very easy to call .net web api, but when i run the app my app is crashed i dont know what’s going wrong …Please help me

    • Tan Woon How

      Hi
      Please provide me error log. At least 1 error message ….please ☺

      • Uzair Qurashi

        Sir there is no error shown …when i want to post somthing then app is closed/crashed . if u provide ur email or any other thing where i can share our code…?

        • Uzair Qurashi

          Brother i have solved this issue….just add an another library…”Gson”…Thanks

          • Tan Woon How

            Cool

  • Thanks for Post
    This post is very nice for WEBAPI with Mobile Apps
    But Some Error as following
    When Deploy On Live Server of WEBAPI
    Run Android Application on Device
    List All Student Complete Show
    Add Students Complete Work
    But When Update or Delete Student Detail one Toast Print as following

    403 ModSecurity Action

    Please Help for this

    • Tan Woon How

      Hi
      Is something to do with your server. Im not sure will this help. Could you add this lines into your web.config

  • Roberki

    Thanks for the post, i have a question, my json returns a List as property, i created the class in android studio like json structure, in android studio in my class i have a propery List, when i run the app, the app crash, when deleted this property List, and run the app again, run very well. How Retrofit work with properties List?

    • Tan Woon How

      Hi,

      I have question like you before, and i examine every possible structure/object that Retrofit could possible support, believe me it can parse almost all json (I guess), please have a look at this link for all possible JSON object’s stucture that could parse, http://instinctcoder.com/android-studio-parsing-json-using-retrofit/. Hope this answered you.

  • Norman

    Your layout xml code for steps 2 and 3 are near identical (Step 3 however excludes the TextView).
    I think step 3 was meant to show student details? not a ListView? please correct me if I am wrong.

    • Tan Woon How

      Ooppss..thanks for reporting… fixed now.

  • parastoo

    thanks. it’s realy helpful for me. would you pleas help me for login scenario with retrofit . I have a web service that it has some methode that one of them it’s login and has two input username and password..

    • Tan Woon How

      Hi parastoo
      I assume you are using HTTP Post for your login. If there is the case you could refer to the InstituteService.java’s AddStudent method. And declare an user POJO filled it up before call the service. Just like line 112 to 146 in StudentDetail.java.

      Hope that helps! Cheers.