Android How to simulate an HTTP connection for an offline demo

I have an Android app that uses HTTP communication for almost every operation. I want to be able to demonstrate without an internet connection, somehow renaming the http exchange. How can this be done? Therefore, I want to somehow almost mock objects, but actually mock the http session, so I can always demonstrate the application or offline. This is really a great thing to do. Since you can easily and reliably demonstrate the application. Does anyone know how I can do this. Replicating the entire server side is simply not an option in which there is too much material. It is important not just to show a screencast, but a real exchange of data. I just want to be able to run the application and repeat it. Maybe debugging. Thanks

+6
source share
7 answers

Here's a hybrid solution using similar ideas from other answers:

You can write a dead simple HTTP server that listens on "localhost: 80" (or regardless of which port is on the server you are targeting), and direct your application to that host instead, dropping the host name from the requests. On your local server there is a link to the actual remote server and the following is done:

  • If ONLINE sends the request as it is to the real server, receives a response, it saves it locally or in a cache in memory pointed to by the request URL, or a file with the URL as its identifier (marked accordingly)
  • If OFFLINE, searches for a request in the cache (in memory or file system) and returns the contents from the cache

This is similar to the recording / playback mode that @ nicholas.hauschild says.

Now you can simply launch the application once when ONLINE, as a result of which your local server will delete the requests that it issues against the real server. Then, when you launch the OFFLINE application, it simply returns this cached content, and then when the same URL is issued.

Hope this helps.

+5
source
+2
source

I would create a “Record Mode” and “Playback Mode” for my application.

In recording mode, I wrote a file every time an http request was made. The file will be called the endpoint at which the request is made. The contents of the file will contain serialized http requests / responses, broken down by line. You can then deserialize the lines from this file until you find the correct query, and release the deserialized response.

This approach will also allow you to create recording / playback profiles where you can record several different sessions (by placing files in a different directory), and then play back any profile that you choose.

This whole approach can be accomplished with a small wrapper class around the HttpClient object that you are using.

+1
source

One way is to use an HTTP proxy. Redirect all web traffic to a proxy server that can run locally on the phone. This can be done with little or no source code change.

+1
source

find a way to use fiddler on a PC, and an Android app fiddler as a proxy. So http traffic is a record. http://blog.csdn.net/grhunter/article/details/5830199

+1
source

You can try one of the answers in this question that I asked a while ago. This is not exactly the same use case, but they can also solve your problem.

Fake HTTP request responses for Android testing

0
source

Simples' solution is to fake when the connection is down. If there is a connection error, make sure the ur application has selected some pre-installed data, and not a connection error.

0
source

Source: https://habr.com/ru/post/889748/


All Articles