Using OkHttp to handle server GET requests and responses: Android

The data that is held for a user of an Android application can be massive. Massive enough that it makes sense not to store data locally. Just about every major app out there has it's own server with a database to keep the data stored securely. Under any case, it always makes sense to have backup, and to move your data to the cloud.

This tutorial will cover up the details on how to use an open source library - OkHttp - to send and receive notifications in the easiest way possible. 

OkHttp is an external library. Therefore the dependency should first be added to the build.gradle.
Copy this line of code and place it in the build.gradle file in the app folder (inside dependencies) . (app/build.gradle) like this:

 dependencies {  
   compile 'com.squareup.okhttp3:okhttp:3.5.0'  
 }  

Now, create a new activity and add these lines to import the library

 import okhttp3.Call;  
 import okhttp3.Callback;  
 import okhttp3.OkHttpClient;  
 import okhttp3.Request;  
 import okhttp3.Response;  

Now you are all set to start working with your cloud databases.

Sending data

This is what you need to do when sending data to a server. First, the link to perform a GET request must be taken and the data amended to fit your needs.The link must be placed in a string.

 String data = "http://xxx.somewhere.net/data.php?dataSent=3";  

 Now an OkHttp client must be created. The client is used to send and receive data. Once that is done, the actual request with the data to be sent must be created. (the Url is the GET request link)

 OkHttpClient client = new OkHttpClient();  
             Request request = new Request.Builder()  
                 .url(data)  
                 .build();  

Now all you have to do is to execute the request you created

 client.newCall(request).execute();  

And that's it. The Url shall be called. If you want to receive a response from the data you sent, it's only a few more steps. That will be discussed now.

Receiving data 

The process of receiving data from a request call is essentially an extension of the above discussed code. Everything has to be done the same way, except that the final line where the client performs the call, should be amended like this: 

 Call call = client.newCall(request);  

This not will send the call. This only encases the request. Executing this means that a response will be received automatically. The code should be ready to handle that.

Note: Remember to catch any exceptions 

  Response response = null;  
           try {  
             response = call.execute();  
           } catch (IOException e) {  
             e.printStackTrace();  
           }  

Now the code will execute the call and receive the data in a Response variable. The response variable holds all the data you were responded with and can be manipulated in multiple ways. The response may be plain text, XML, JSON, etc... and there are ways to handle all these. Quickly reading up on the API should help you convert the data into whatever format you want.

So that's all for now. If you have any questions, ask below. 

Comments

Post a Comment

Popular Posts