I know, there are much posts and answers on this topic and it seems, I have read them all... But I just want to know how to do an easy https request in my Android app. Is there a easy class which make such an request?
I want to call something like
String response = new EasyHttpRequest().execute(myUrl).get();
in my MainActivity
For information I can't just set the response Text to an TextView in the onPostExecute() method, I have to work with this data and save it or something else.
The best library I know so far for network requests is Retrofit. It's kind of a standard for Android apps. If you want something more low level you can use Okhttp.
Both libraries can work together, and they are more than production ready. Of course, they support https and many other security features as ssl certificate pinning.
They support running the request on the same thread you are or in a background queue provided by the library, up to you if then you want to wrap it on an asyncTask, simple thread or service, those libraries leave you some freedom in that matter.
You can use this repo . it's very easy to use.
okhttp-utils
Related
I'm using AsyncHTTPClient to communicate with my server API.
I've read that I have to use services to communicate with a server.
1) Is AsyncHTTPClient already a service? Or do I still need to put all methods which use AsyncHTTPClient into my own services? Or is it OK to call AsyncHTTPClient methods directly from my Activities?
2) I can't understand how services should be properly used. Let's say I don't use AsyncHTTPClient but I have few different actions which make a call to server API (get realities, get users, post user, etc.). Do I have to put each action into a separate service? If not, how can I call different methods from one same service? All examples I have seen always show that there's just one action which is automatically being called on service start up by Activity.
3) I found another Android REST library Retrofit - does it have any advantages over AsyncHTTPClient? Do I need to put it into services?
At first, I would recommend you to use Retrofit. I've already used AsynHttp, but now I am using Retrofit, and I am very satisfied with it.
About the topics in your question:
I've read that I have to use services to communicate with a server.
Where do you read this? I don't create Service to my requests, and all of then works pretty well. The main point when creating your requests is to make them in a background thread. So if you don't use some lib like AsyncHttp or Retrofit, you have to put your requests in an AsynTask class, for example.
Is AsyncHTTPClient already a service?
No. It is a library to make asynchronous HTTP requests. In the site of the lib, they say:
All requests are made outside of your app’s main UI thread and You can also use it in Service or background thread.
That is, you can use it in a Service, they do not say you have to use it in a Service.
The example in the site teaches how to make a RestClient with this library. It is a good example. You only need to create a static class and put the methods to make your posts, gets etc
I can't understand how services should be properly used.
Services are used for long-running operations in the background. A possible way to use a Service with your lib (AsyncHttp or Retrofit) is when you want to make requests to the server, even when the user is not using your app. So you can create a Service with a timer that will call the server at the intervals defined by you.
Retrofit - does it have any advantages over AsyncHTTPClient?
Backing to Retrofit, again you don't have to put it in a Service. Only if you need this. From threir site, they say: Retrofit turns your REST API into a Java interface. This make your client very simple, and self documented because each call to your server API will be a method in this interface, and I think this is a big advantage. It is simple to use Retrofit. As I said, now I changed to Retrofit, and I can say, the lib has a good documentation and support, it has many users. So it is easy to find solutions to some bug. Other advantage is that it already has the lib GSON, which is very useful to parse the JSONs that you will receive from your server API.
But of course talking about vantages is dangerous because you can find many personal opinions. See this question and its answers. I think some answers from there can also help you with your choice.
To finish, here are some links with tutorials about Retrofit and how to create your requests with this powerful library.
Durable Android REST Clients
Consuming APIs with Retrofit
A smart way to use Retrofit
I'm developing an android app where I would like to fetch some data (mostly text) from the internet but not necesseraly from a website! I would like to have a server that allows clients to fetch some text data. What kind of server fits my goals the best? Http or maybe simply tcp? I don't know much about http so I don't know if it matches my goals and/or if it handles well a kind of text "database".
Edit:
A use case could be: people could write comments and send them to the server. Then clients could refresh their app by fetching new comments from the server. Therefore I'M asking what kind of server could best handle services and kind offre database if needed.
I like using NodeJS in combination with ExpressJS for such purposes. This combination allows you to easily work with HTTP/HTTTPS which is allowed by practically every firewall or proxy server. As of the latter reason I recommend you to use HTTP instead of an own protocol. Furhtermore, Java offers the HTTPURLConnection client which is very easy to use. Moreover, securing traffic with TLS (SSL) is very simple. In addition, NodeJS is resource efficient, runs on Windows, Linux and even on OS X.
For getting the text you can use HTTP GET request handled by the get() method of the Express instance.
This compact tutorial helped me to get familiar with Express on NodeJS.
Without knowing what your use-case is it's difficult to make a good recommendation.
With that said you may find something like https://parse.com/ suitable.
They provide an Android sdk and the 'getting started' tutorials will have you up and running in no time at all.
Can anyone explain for me or guide me through the basics that i have to know on how to interact with the server API from Android Application?
Any good website that teaches beginners because am new to this thing and am quite lost
If it is a restful API that you will be interacting with, I would highly recommend using RetroFit.
http://square.github.io/retrofit/
Square have excellent documentation and the examples should be easy to follow. If you get stuck you can always post on here under the Retrofit tag which has a fairly large following.
Volley is also an option.
Use Volley networking library http://developer.android.com/training/volley/index.html
This example shows how to make simple client-server app right now.
Here and here you can read description during making client-server app.
I found those links very useful for me when I started to learn this topic.
Hope it helps.
To get you started I've have a sample app Link which uses Github's REST and fetch the result and display it in android List. It is a basic one without any authentication. Hope this helps.
Recently android has introduced a library to parse json. It is called volley.
Volley offers the following benefits:
Automatic scheduling of network requests.
Multiple concurrent network connections.
Transparent disk and memory response caching with standard HTTP cache
coherence.
Support for request prioritization.
Cancellation request API. You can cancel a single request, or you can
set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI with
data fetched asynchronously from the network.
Debugging and tracing tools.
Tutorial which I used is Volley tutorial for json parsing. Another link is here
Hope this helps you.
I have to implement a client-server architecture where there are many android client located at different places querying the application server (running all the time (24x7)) and which will do task accordingly and reply to clients. But I don't know what exactly application server is and how to implement it and what is easiest and quickest way to implement this. I am running out of time that is why posting this question otherwise I would have gone through lot of tutorials instead.
The server code is to be written in php/c#/java.
Any quick help is highly appreciated.
You may have look at Google App Engine its easy to setup, hosted on google environment. Which might fulfill your need. In this case you don't need to buy hosting plans etc. etc. You will get yourappname.appspot.com URL to use app server.
Hope it helps!
Note:
Appengine supports Python and Java
You can implement it by knowing how the application servers work.
First of all, you should create a class that implements the HttpHandler, in handle method you can get header and body of the message.
Secondly, you should choose how to handle the request in HttpHandler. One possible approach can be using of ThreadPoolTaskExecutor or SimpleAsyncTaskExecutor or any task executors.
Finally, you should prepare the response messsage with exchange.getResponseHeaders() and exchange.getResponseBody()
I'm writing an Android app which sometimes needs to request data through HTTP from a REST API. I'm using the Apache DefaultHttpClient for performing requests. Is there a way to write tests for this app and "replace" DefaultHttpClient's response when running the tests so that test results are always consistent?
As an example of the things I'd like to test, one of the web services I'm accessing takes a string and performs a text search, returning a paged list of objects. I need to test the cases where the list is empty, the list fits in the first page, or the list is larger than a page and the app needs to make several requests to get the complete list.
I'm not the developer of this web API nor can modify its responses, so I can't change what it returns. For the above example, if I want to test the case where the list returned is empty, I could just search for a string which I'm sure won't return any results, but the other two cases are harder because what the service can return is always changing.
I think ideally I would have a way to get a modified DefaultHttpClient when running tests, that returns a hardcoded result for requests to a given URL instead of actually doing the network request. This way I would always get consistent results independently of the real web service's response.
I'm currently using Robotium for testing but I'm open to using other tools too.
Yes, you can definitely "fake" responses when using the HttpClient framework. It's quite convoluted, and I will have to leave most of the details up to you, but I will give you a quick overview:
Implement ClientHttpRequestFactory, mainly so you can override the createRequest() method so you can...
Return your custom implementation of ClientHttpRequest, in which you can override the execute() method so you can ...
Return your custom implementation of ClientHttpResponse in which you will finally be able to return your fake response data, e.g. getBody() can return the content of a file, you can hardcode the headers in getHeaders(), etc.
The rest is figuring out how to best tie all these shenanigans to your service layer.
You might give Charles a try for something like this. Sorta a non-code solution.
http://www.charlesproxy.com/
I use the Charles' reverse proxies and the map local tool for things like this.
What you do is point your request at your local box on the reverse proxy port. Charles in turn can be configured to provide a static hard-coded flat file but to your app it looks like a 100% genuine web service response.
There are lots of other cool things you can do with Charles - watch traffic from your android app to and from your server and breakpoints (which allows you to tweak requests and responses before they are sent and received). Definitely worth checking out.
Another option is to use Dependency Injection so that you can change the HttpClient when running the tests. Check out Guice if you are interested.
I'm guessing you are interested in writing functional tests using the standard Android Junit testing framework. So you could just implement the parts of the API you are using on your own webserver and point at that server when running your tests.
If you'd prefer your tests to be self-contained, you could implement an Http server that runs on the device. Examples of using the Http server available in the Android class library are here and here.