Android How To Simulate HTTP Communication for Off

2019-04-28 04:23发布

I have an Android App which uses http communication for nearly every operation. I want to be able to demo without connection to the internet by somehow replaying the http exchange. How can this be done? So I want to somehow almost like mock objects but really mock http session so I can always demo the app on or offline. This is really a very cool thing to be able to do. Since you can demo the app easily and reliably. Does anyone know how I could do this. Replicating the whole server side is just not an options its got too much stuff. Its important not to just show screencast but the real data exchange. I just want to be able to run thru the app and replay. Maybe debug as well. Thanks

标签: android http
7条回答
祖国的老花朵
2楼-- · 2019-04-28 04:41

Simples solution lies in faking it when there is no connection. If there is a error in connection, make sure ur app throws some preset data rather than an error in connection thing.

查看更多
来,给爷笑一个
3楼-- · 2019-04-28 04:45

You could try one of the answers in this question I asked a while back. It's not exactly the same use case but they may solve your problem too.

Faking HTTP request responses for testing in Android

查看更多
Luminary・发光体
4楼-- · 2019-04-28 04:48

find a way using fiddler on pc,and android app take fiddler as proxy.So the http traffic is record. http://blog.csdn.net/grhunter/article/details/5830199

查看更多
劫难
5楼-- · 2019-04-28 04:52

I would create a "Record Mode", and a "Playback Mode" for my app.

While in Record Mode, I would write out a file each time an http request was made. The file would be named by the endpoint the request is made. The contents of the file would a collection of serialized http requests/responses broken up by line. You could then deserialize lines from this file until you find the proper request, and play back the deserialized response.

This approach would also allow you to create Record/Playback profiles, where you could record multiple different sessions (by placing the files into a different directory) and then playback from whichever profile you choose.

This whole approach could be done with a small wrapper class around the HttpClient object you are using.

查看更多
爷的心禁止访问
6楼-- · 2019-04-28 04:54
查看更多
对你真心纯属浪费
7楼-- · 2019-04-28 04:55

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

You could write a dead simple HTTP server that listens on "localhost:80" (or whatever the port is on the server you're targeting) and point your application to this host instead by factoring out the host name from requests. Your local server has a reference to the actual remote server and does the following:

  • If ONLINE, forwards the request as-is to the real server, gets the response, saves it locally either in an in-memory cache keyed by the request URL or a file named with the URL as its identifier (munged appropriately)
  • If OFFLINE, looks up a request in its (in-memory or file system) cache and returns the contents from the cache instead

This is kind of like the record/playback mode that @nicholas.hauschild says.

Now you can just run your app once when ONLINE, causing your localhost server to save away requests that it issues against the real server. Then when you run your app OFFLINE, it just returns these cached contents instead whenever the same URLs are issued.

Hope this helps.

查看更多
登录 后发表回答