- Overview
- This tutorial covers making an http connection with HttpURLConnection API, processing a server's JSON response using GSON, and then translating that response into a Java object for Android consumption.
- Dependencies
- Android Studio 1.5.1
- Process
- Preparation
- GSON Library
- Add the necessary library dependency to the app module build.grade file by adding the following to the dependencies section:
compile 'com.google.code.gson:gson:2.5'- Android Studio automatically checks the version number against available repositories in order to provide the option to update the library upon download.
- Re-sync Gradle in order to finalize library installation.
- Add the necessary library dependency to the app module build.grade file by adding the following to the dependencies section:
- Java Object
- Create a model class with attributes matching the expected JSON object's attributes. The expected JSON objects will have labeled attributes. Each label will be preceded by a corresponding value. It is necessary to label the Java objects with an identical naming convention as the expected JSON object's attribute labels. For purposes of this tutorial, we will create a simple object:
public class MyObject(){
String name;
public MyObject(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
- Create a model class with attributes matching the expected JSON object's attributes. The expected JSON objects will have labeled attributes. Each label will be preceded by a corresponding value. It is necessary to label the Java objects with an identical naming convention as the expected JSON object's attribute labels. For purposes of this tutorial, we will create a simple object:
- GSON Library
- HttpURLConnection
- Open a connection, send a GET request, check the response code, read the input stream.
try {
serverUrl = new URL("http://www.use_a_desired_web_address_here.com");
HttpURLConnection httpConnection = (HttpURLConnection) serverUrl.openConnection();
httpConnection.setRequestMethod("GET");
int responseCode = httpConnection.getResponseCode();
if (responseCode == 200) {
BufferedReader responseReader = new BufferedReader(
new InputStreamReader(httpConnection.getInputStream()));
String responseLine;
StringBuilder response = new StringBuilder();
while ((responseLine = responseReader.readLine()) != null) {
response.append(responseLine);
}
responseReader.close();
} else {
Toast.makeText(mContext, "CODE: " + responseCode, Toast.LENGTH_SHORT).show();
}
} catch (MalformedURLException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
- Open a connection, send a GET request, check the response code, read the input stream.
- JSON to GSON to Java
- "StringBuilder response" now holds the received JSON server response as a string object. Luckily, GSON provides a method to convert a string of JSON objects into matching Java objects. Since we previously created a matching Java class we can now parse the JSON string into our Java objects using GSON as such:
String jsonResultString = response.toString();
Gson gson = new Gson();
MyObject[] myobjects = gson.fromJson(jsonResultString, MyObject[].class);
- The array of Java objects now holds the individual objects created from the JSON objects.
- "StringBuilder response" now holds the received JSON server response as a string object. Luckily, GSON provides a method to convert a string of JSON objects into matching Java objects. Since we previously created a matching Java class we can now parse the JSON string into our Java objects using GSON as such:
- Preparation
- Conclusion
- This tutorial covered making an http connection, processing a server's JSON response, and then parsing that response into a Java object using GSON.
- This tutorial covered making an http connection, processing a server's JSON response, and then parsing that response into a Java object using GSON.
0 Comments
|
NOTEThese tutorials are written with the intermediate developer in mind. The internet is filled with beginner tutorials on writing boiler-plate code, these tutorials will hopefully address the next level. Archives
November 2015
Categories
All
|