I am testing an Android app which is connected to my local router. On the same WiFi network are also running some Java web services using Restlets. I verified that the Android app can hit the service filter and even receive a response from the service filter. However, it cannot seem to hit the Restlet service in question.
The full URL to the service is:
http://192.168.0.148:8080/MyApp/service/login
where 192.168.0.148 is the local IP address of the computer running the Restlet web services.
I have the service mapped in my Restlet Application class as follows:
router.attach("/login", LoginResource.class);
My web.xml file has the appropriate mappings to route anything with the pattern /service/ to the Restlet servlet.
I can successfully hit this service when testing from SOAP UI using precisely the same URL I gave above. In fact, all web services have been tested end-to-end using SOAP UI and are completely functional.
For some reason, the Android app can hit the service filter, when but when it reaches:
chain.doFilter(request, response);
the request ends up lost in space, and nothing gets returned.
I suspect that this is largely a Reslets configuration problem, though I don't know exactly what is happening here.
I got virtually no feedback, either from the SO community or from Restlets, which actually makes sense given that everything I reported in the question is correct. I decided to attach the Restlet sources to my IntelliJ IDE, and step through the code. I had seen that the Tomcat logs contained a response code of 415, and by inspecting the actual exception inside Restlets, I saw an error stating that the media type I posted was unexpected. Upon seeing this, I immediately knew what I had done wrong, which was:
not sending proper JSON to the login service
not declaring the content type to be JSON in the header of the POSt
and using Restlet services all of which expect JSON
For anyone who faces as similar problem using Restlets with JSON, the following Android code got everything working with no issues:
String endpoint = "http://192.168.0.148:8080/MyApp/service/login";
URL obj = new URL(endpoint);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
JSONObject json = new JSONObject();
json.put("username", username);
json.put("password", password);
// etc.
Related
I am not able to reach the create Method in tests_controller.rb with this code.
String newUrl = "http://10.0.2.2:3000/tests";
httpcon = (HttpURLConnection) ((new URL(newUrl).openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();
And here is my routes.rb (I use model scaffold to create the RoR app).
resources :tests
Am i wrong in routing or something. When i run this code in Android, the create method is not run at all.
It's hard to tell the reason for the failure you have. Your routes seem OK. You can check couple other things:
Is your Rails server listening on the right interface and port?
Is there any network problem between the machine your client is working on and the server?
Instead of going through the cycle of edit-compile-deploy-run of Android, simply use curl or a similar tool to try the POST request from the console.
When you pinpoint the place of the problem, then you can ask another question, or, more likely, already find the answer online.
I was building an enquiry to google's direction service, but the execute method crashed halfway, Here's the code
Uri.Builder b = Uri.parse("http://maps.googleapis.com/maps/api/directions/json").buildUpon();
b.appendQueryParameter("origin", "The University of Hong KOng");
b.appendQueryParameter("destination", "Lee Hysan Hall");
b.appendQueryParameter("sensor", "false");
b.appendQueryParameter("language", "en_US");
String finalstr = b.build().toString();
URI googledirectionservice = new URI(finalstr);
HttpGet mRequest = new HttpGet(googledirectionservice);
HttpClient mClient = new DefaultHttpClient();
response = mClient.execute(mRequest);
The url constructed is fine when I access it from my browser, but it wouldn't work accessed from the HttpClient.You can double check with
http://maps.googleapis.com/maps/api/directions/json?origin=The%20University%20of%20Hong%20KOng&destination=Lee%20Hysan%20Hall&sensor=false&language=en_US
What would be the reason? Besides, how can I make the debugging information more informative? The current ones in logcat showed nothing more than
threadid=... thread exiting with uncaught exception
try turning on logging in your client. instructions sample here or here
Access to the WIRE logs and to the HEADER logs is a must in developing networked apps. So, depending on the client you are using for your http implementation, you should google until you know how to expose the appropriate logs. If you have the logs for WIRE and for HEADERS, answers to questions like you post will be pretty much self-evident in the log details.
In my case, i just keep available an extra jar file built with my http implementation with the logs activated and when i need to debug the network, i just rebuild my app with that jar. It all depends on which http lib you have chosen.
list of libs:
loopj
volley
i am new to WCF
i have created WCF which retrieve data from database and this will execute thru asp.net application
now, i want to use this WCF in my android application
can anybody tell me what should i do ?
i have study but i got little hint, about SOAP and REST.
so which would be preferable for me ? can anybody give me hint or code ?
i need ready working code snippet
URI uri = null;
try {
uri = new URI("http://localhost:3997/AWS_WCF_Service.svc");
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpGet httpget = new HttpGet(uri + "/SayHello");
httpget.setHeader("Accept", "application/json");
httpget.setHeader("Content-type", "application/json; charset=utf-8");
HttpResponse response = null;
response = httpClient.execute(httpget);
Thank YOu
Exactly how might depend on which version of ASP.Net you're using, but you will need your Web app to expose your WCF objects using a service. If you are using 4.0, either SOAP or REST services are options (Web Service or WebAPI service). If you want a philosophical debate about their respective merits, this might be a place to start: SOAP or REST for Web Services?, but since both can return JSON, either is probably fine for your Android app.
I have .Net 3.5 myself so I've mostly been using the Web Service version (with an .asmx file), and found the following link immensely useful:
Android -- How to access data in an ASP.NET database via app?
The accepted answer on that thread got me set up with a basic Web Service and little Android app in one afternoon! (This was admittedly with me having some little knowledge of both)
If you want a RESTful service and you have .Net 4.0, I suspect you could achieve your aim by following a tutorial on the subject (like this one http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api), and then using the Android code from the link above.
(Something worth bearing in mind when working with ASP.Net services returning JSON is that the returned object will be wrapped in an object called "d" - see Why do ASP.NET JSON web services return the result in 'd'?. I haven't used XML so I don't know if there are any issues for that format.)
So basically i need my android app to connect to a web service using a url as such
"http://username:password#0.0.0.0" aka basic authentication.
obviously the username and password are checked by the web app before allowing access and otherwise doesn't allow the request.
my issue is that all the methods i try always say unauthorised (response code 401) regardless of what combination of classes and methods ive used to try and connect to the the url.
The web app in question is designed to return things only is un/pw clears otherwise it returns nothing, the web app and un/pw etc have all be checked and cleared.
so does anyone no the correct way to send a request to a url like that and have it work correctly?
android api8 btw
UPDATE
Turns out my issue is due to the web app using NTLM windows authentication which is not supported directly by androids/apache http library, investigating appropriate workarounds now
Here's some code form a really old project of mine. I used basic auth for some web service, and this worked at the time. I'm not sure if there are updated api's since then (this was Android 1.6), but it should still work.
HttpGet request = new HttpGet();
request.setURI(new URI(url));
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials(authUser, authPass);
BasicScheme scheme = new BasicScheme();
Header authorizationHeader = scheme.authenticate(credentials, request);
request.addHeader(authorizationHeader);
Basically, Basic HTTP auth is a simple hash of the user and password. The browser allows you to stuff these values in the url, but it actually does the work of adding the basic auth header to your request.
I'm writing an HTTP client in Android that connects to glassfish on my localhost and sends some json information to the server.
I use:
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials(SettingsHelper.mUser, SettingsHelper.mPwd);
client.getCredentialsProvider().setCredentials(AuthScope.ANY, cred);
for authontication, and making a put request.
The problem is sometimes, the client sends an unauthorized request before retrying and sending an authorized one, and on the second time, it pushes the json entity, before getting a proper response from the server (100 - continue). then, the server doesn't respond at all, and everything hangs.
i will note that sometimes it works.
Has anyone else experienced this problem? How might I resolve it?
finally managed to make it work with basic pre-emptive authontication.
example here