Rest client logging for AndroidAnnotations - android

I am attempting to use AndroidAnnotation's rest client to access a web service. I am receiving the following error:
org.springframework.http.converter.HttpMessageNotReadableException:
Could not read JSON: Unexpected character ('f' (code 102)):
was expecting double-quote to start field name
How can I make the rest client log the actual response it received? I can't imagine why my web service is returning this response, but I can't debug it unless I can see the full response. Do I have to set some kind of option at the level of the Spring framework?
I would also like to see the body of the request I am sending.
Thanks for your help!

Here we see that AndroidAnnotations is a wrapper around the Spring Android RestTemplate Module. The code for the RestTemplate is here. So we can find out which TAG is used for logging:
private static final String TAG = "RestTemplate";
Are you not able to see log entries for this TAG? Which converter / extractor are you using? Please post the call stack.
In the wiki they recommend to use a Interceptor for logging request / response. So you could implement your own interceptor like:
public class LoggingInterceptor implements ClientHttpRequestInterceptor {
#Override
public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
logRequest(request);
ClientHttpResponse response = execution.execute(request, data);
logResponse(response);
return response;
}
private void logRequest(HttpRequest request) {
// log it
}
private void logResponse(ClientHttpResponse response) {
// log it
}
}
You enable the interceptor in the #Rest annotation (field interceptors).

While I do not use AndroidAnnotations and cannot answer your question directly, I would like to propose an alternative solution. You could use a great little utility program called Fiddler. It can do wonders for debugging networking activity, whether it be requests, responses, HTTP headers or practically anything else that would matter in a REST API communication.
You can find a full tutorial on how to setup your environment to use Fiddler here, but to name a few crucial steps (credit goes to the linked page, you can also find helpful pictures there)
Setup Fiddler:
Click menu Tools | Fiddler Options, then select the Connections tab
Make note of the “Fiddler listens on” port (normally it’s 8888)
Make sure the check box for “Allow remote computer to connect” is checked
Switch to the HTTPS tab
Make sure the check boxes for “Capture HTTPS Connects” and “Decrypt HTTPS traffic” are both checked
Restart Fiddler
Make note of the PC’s IP address Close non essential apps on the Windows PC (to minimize web traffic being routed through Fiddler)
Setup your device:
Tap on Settings, then Wi-Fi
Find the network on which you’re connected (normally the first one listed), then tap and hold
Choose Modify network from the pop-up
Scroll down and enable “Show advanced options”
Change “Proxy settings” to Manual
Under “Proxy host name” enter the Windows PC IP address from above
Under “Proxy port” enter the Fiddler port from above (usually 8888)
Tap Save and wait a moment for the network to reconnect
Now you will see all the needed details for your REST API calls which makes debugging much easier.

Related

Generate mock service as HTTP endpoint

I am using this example login to build the RESTful part in my application. However, I do not have a server connected. What I want to do is to create a mock service, using an HTTP generator as Mocky (check it). Does anyone know how can this be achieved? Thanks a lot!
Sorry for being so long to answer (little bit busy that week). Well, I hope you haven't given up meanwhile...
So below is the procedure to set up a RESTful mock service :
You can download the Opensource version of SOAP UI here. It's enough for making a simple mock service
Create a new empty Project
Right click on the project and click on "New REST mock service"
Right click on MockService1 and click on "Add new mock action"
Then double-click on /api/v1/user and Add a new mock response (right-click / New Mock response)
It's done. You just need to double-click on "REST MockService1" and run the service (green button)
You can configure the path, port and host :
Once running, you're WebService can be called using this HTTP Request : GET http://localhost:8080/api/v1/users
Much more information on the WebSite : https://www.soapui.org/rest-testing-mocking/rest-service-mocking.html
You can for example, define some algorithmes to make your webservice more intelligent and test every case (for example if user is = 1 return 200 OK {user} otherwise return 404 ERROR NOT FOUND, ...)

How to implement Kurento Client JS with your own "Tomcat signalling server" on Android using a secure SSL connection to KMS?

So this is a two part question:
Part a: I'm trying to implement a secure connection to the KMS. From the documentation, I've understood that KMS Configuration file would need to be updated with the SSL certificate and then the HTTPS connection from the client can be made. Please let me know if there are any other steps that are involved in achieving SSL security.
Part b: From a better understanding now and from comments from a previous question I posted, Kurento Utils does not connect to KMS directly (this was an fyi and a clarification I received and I wanted documented here just in case). Now I'm trying to use Kurento Client to connect to KMS and I'm trying to understand the role of ICE/TURN/STUN servers acting as negotiators in the middle. If I were to specify my own server URL, I'm assuming that I would not need to include "freeice" and "normalice" and instead specify my own server's URL. In the code snippet below taken from the tutorial on github, I'm assuming that I would need to replace the argument for ice_servers to point to the url where my server is running? Or since this is the client, do I really need an ICE server because as said from the first statement, the utils don't connect to the KMS but the client can, right? So if I were to specify the Kurento URL for "ws_uri" parameter, then I won't need to even use ICE servers...right? I don't really understand the concept of ICE/TURN servers very well in terms of how they integrate with Kurento and hence, I would like to understand in English as to what changes would I need to make in order to get this to work. I will bang my head to write the code myself! Thanks much in advance!
`
var args = getopts(location.search,
{
default:
{
ws_uri: 'ws://' + location.hostname + ':8888/kurento',
file_uri: 'file:///tmp/recorder_demo.webm', //file to be stored in media server
ice_servers: undefined
}
});`
Answer A
Only this and nothing more... at least for KMS. On the client side, you'll need to specify the WSS port and so on.
Answer B
Your client might need a STUN/TURN server, and that's independent of where KMS is located. STUN and TURN are used in the candidate harvest process, to discover the network topology of your peer. You have two peers: KMS and your Android app, and both need to have, in their SDPs and during the negotiation, a candidate that is reachable by them (app will connect with KMS and viceversa) If both peers are on the same network, you can go without using STUN/TURN. The moment you have a NAT in between, you need at least STUN for that peer to be able to harvest candidates that have the public IP on the other side of the NAT, which is not known by the peer unless STUN is used.
TURN is used as a relay server, and it is needed in a small set of cases. If you are almost certain you are going to use TURN, you need to have that in a machine different than KMS (it makes close to no sense to have both the relay server and the media server installed together)
So the answer is yes, you are most likely going to need STUN/TURN in your clients.

Client (Mobile) how to intercept and modify http response

I am Android developer, my application uses a bunch of http REST calls and it gets responses from servers. I use Charles to inspect the data (so I have to install Charles certificate onto my device so that https traffic can be read by myself).
Are there any tools out there like Charles that will allow me to modfiy that response packet before sending to the client ??
Depending on what exactly you want to modify, Charles' inbuilt Rewrite Tool might be what you are looking for. You find it in the Tools menu. You can specify which requests shall be modified by protocol, host, port, path and query, and you have the following modification options:
Add, modify, remove headers (request and response)
Modify Host, Path, URL, Response Status
Add, modify, remove Query Parameters
Modify body (request and response)
Another option is Fiddler. Like Charles it can be configured as a proxy for android, decrypt HTTPS traffic and modify request and response.
Charles itself has the functionality. Follow the steps:
enable break points by right click on individual request or a path
Before sending the request, Charles will give you a change to edit it. See below. Click "edit request" to fill in whatever you want and click "execute" to send the request.
Before posting the result back to your mobile phone, you have a change to edit the content. See below.
You may try OWASP ZAP or Burp Suite. OWASP ZAP is completely free and provides a number of features.
See also Android : Capturing HTTP Requests with non-rooted android device.
I've recently tested HTTP Toolkit on Android emulator. It works and allows to capture and edit response from a server. Some functions are paid (in Pro version). Requires root priviledges on real devices.
You can also use Burp Suite or Fiddler.

How to inspect HTTP requests made by WebViews

As you know, a lot of request (images, scripts, AJAX, etc.) are send when loading a single page. So I need to get all those request and inspect them.
So the question would be: How can I inspect the HTTP requests that are made when a WebView loads a page ?
I want: headers, method, status code, response, cookies.
Right now, I have:
public void onLoadResource(WebView view, String url){
Log.d("my-tag", "onLoadResource = " + url );
}
But that only shows me the URL.
The best you can get in your app is the WebViewClient.shouldInterceptRequest method, but that only has the URL. You currently can't get any of the things you've listed.
For debugging you can use Chrome DevTools if you're using Android 4.4.
Look here: https://gist.github.com/kibotu/32313b957cd01258cf67 where you get the http headers at android >=21
The easiest way is to use a proxy. I use Charles, but I'm sure there are others. On the device, go to the WiFi settings, long click the one you're connected to, select "modify network" and enable the advanced options. There you'll be abel to configure the proxy settings for the whole device.

How to debug http calls on Android devices?

I'm writing a Lovefilm client for Android, and it's not going too badly except I keep having problems with the remote calls to retrieve data from the API.
Does anyone have any tips for debugging remote calls like this? Can I tcpdump on Android or is there a native way of doing it?
For example, I'm using the Scribe-java library for OAuth to access the Lovefilm API, I can authenticate find and retrieve a list of films on the users account fine when the device is running Gingerbread, but trying to retrieve the accessToken on Froyo causes a blank response & and apparent response code of -1, I'd like to be able to see what's going on under the cvers their.
Another example I'd like to be able to the raw http for is trying to run a search, I get and IOError that says "Received authentication challenge is null"
I've used Fiddler (http-proxy for debugging http calls) with the android emulator in these cases. Just start the proxy, and start the emulator with the correct proxy address (-http-proxy ).
Fiddler is the most useful option. On the emulator #Scythe answer will work, but on a real device you will need to set the proxy in the Apache Http Client. The following code will do that:
HttpHost proxy = new HttpHost("youripaddr", 8888);
params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
If you are using https, fiddler is not so useful. In that case can enable the build in logging support in Apache Http Client. The following code does that:
Headers only:
java.util.logging.Logger apacheHeaderLog = java.util.logging.Logger.getLogger("org.apache.http.headers");
apacheHeaderLog.setLevel(java.util.logging.Level.FINEST);
Headers & Wire:
java.util.logging.Logger apacheWireLog = java.util.logging.Logger.getLogger("org.apache.http.wire");
apacheWireLog.setLevel(java.util.logging.Level.FINEST);
Note that this will have to have a java.util.logging Handler configured at finest level and the default handler is configured to log to logcat, which will filter DEBUG (finest) entries by default.
If your system can share the wi-fi connection you should be able to route packets from any device through your system and then using wireshark you can get monitor your calls or get a tcpdump.
Also , and more importantly , it would be best if you log your network calls and responses as suggested by #Matthew
Windows 7 wi-fi connection sharing : http://www.winsupersite.com/article/faqtip/windows-7-tip-of-the-week-use-wireless-hosted-networking-to-share-an-internet-connection-wirelessly.aspx
Since I always run into similar troubles and it seems a lot of people having the same issues over and over again I wrote up a quick tutorial for debugging client-server communication by using netcat and cURL.
That of course only works for the simplified case that you always 'fake' on side of the connection.
For eavesdropping you can use tools like tcpdump or Wireshark. Which will definitely be easier if you're able to run the server instance directly on your local machine.
Stetho is a great tool from FB which helps in debugging android Apps. You can have access to local data and have a check on your network using this.
http://facebook.github.io/stetho/

Categories

Resources