How To Debug ASP.Net Web From Android Studio With Retrofit?
Just a tips for guys like me ( Hybrid with half Android and half .Net), not sure why this can’t easily find on the internet or maybe this is too common knowledge everyone should know about? It’s a pain when you try to debug in totally different platform, you have no idea what had happened at the server unless you put more log to see the detail in every line, and you gonna to mess up your code, yackkkk!!! (Do you have the same experience?? That’s why you are here š ). So in this short tutorial, we just want to show you this!!! š
Watch below demo to see how it work :).
View Demo
How To Debug ASP.Net Web From Android Studio With Retrofit?
I will use project from this link to demonstrate how this work. The below code is getting the mentioned project and we modified a little in the file, RestService.java. And, these highlighted lines are your life saver code, you should keep these in your head :). (At least, save mine š ).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package instinctcoder.webapiclient; import retrofit.RequestInterceptor; /** * Created by Tan on 7/2/2015. */ public class RestService { //You need to change the IP if you testing environment is not local machine //or you may have different URL from what we have here private static final String URL = "http://10.0.2.2:49332/"; private retrofit.RestAdapter restAdapter; private InstituteService apiService; public RestService() { restAdapter = new retrofit.RestAdapter.Builder() .setEndpoint(URL) .setLogLevel(retrofit.RestAdapter.LogLevel.FULL) .setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade requestFacade) { if (URL.contains("10.0.2.2")) requestFacade.addHeader("Host", "localhost"); } }) .build(); apiService = restAdapter.create(InstituteService.class); } public InstituteService getService() { return apiService; } } |
Step By Step?
1. Firstly if you do not have the .Net project, you could get this sample project for a try. And set breakpoint in the project and run. and this will open up browser and copy the URL from browser. That’s all for server.
2.Now we need to configure the setting in the Android Studio project, or you could get the our Android Sample Code from Here, set URL in RestService.java like above code and that’s all! Click Run button.
Please feel free to comment.