Wednesday 7 September 2016

Android Library- Parse JSON into Recycler View using just one line of code!


This repository will help Android developers to make connection with HTTP clients and put JSON data into Recycler View in just 1 simple function call. No need to establish any Async Task or set up any Adapters for Recycler View by yourself.
 All you need to give is the url of the JSON file and your xml layout about how to parameters should appear.
The repository is uploaded on Bintray and has been compiled for Maven and JCentre.


Click below link to see video tutorial




 Download and use the library now:

 Maven:
        <dependency>
        <groupId>com.dbconnection.subham.dblibrary</groupId>
        <artifactId>dblibrary</artifactId>
        <version>1.0</version>
        <type>pom</type>
        </dependency>


 Gradle:

compile 'com.dbconnection.subham.dblibrary:dblibrary:1.0'


Tutorial :

Consider we have a JSON available at any of our servers. We now want this data should be shown in our Recycler View in Android.
Lets look at sample JSON. This has been hosted at my servers. You can use yours. All you need is url of that JSON document.

{
 "result": [
  {
   "company": "Microsoft",
   "news": "Placement session for October 2015 is soon going to start. Please stay in touch. All the very best for your future endeavours.",
   "likes": "6",
   "url": "http://hello45.esy.es/PhotoUpload/uploads/32.png",
   "dp": "http://hello45.esy.es/DpUpload/uploads/2.png"
  },
  {
   "company": "Dhruv",
   "news": "Guys Ive been placed at AMAZON. Thanks to Placement Assured for their support and cooperation. Gonna miss you all. Bye!!!",
   "likes": "7",
   "url": "http://hello45.esy.es/PhotoUpload/uploads/34.png",
   "dp": "http://hello45.esy.es/DpUpload/uploads/dhruv.png"
  },
  {
   "company": "pa",
   "news": "final evaluation close Pull up your socks",
   "likes": "0",
   "url": "http://hello45.esy.es/PhotoUpload/uploads/31.png",
   "dp": "http://hello45.esy.es/DpUpload/uploads/01.png"
  },
  {
   "company": "Sgupta",
   "news": "check 123",
   "likes": "6",
   "url": "http://hello45.esy.es/PhotoUpload/uploads/31.png",
   "dp": "http://hello45.esy.es/DpUpload/uploads/subham.png"
  },
  {
   "company": "pa",
   "news": "Placement Assured wishes you All the best for your Placement Season",
   "likes": "98",
   "url": "http://hello45.esy.es/DpUpload/uploads/01.png",
   "dp": "http://hello45.esy.es/DpUpload/uploads/01.png"
  },
  
 ]
}


1. Open your Android Studio and start with new Project.
2. Go to build.Gradle file and add the following dependency:
   compile 'com.dbconnection.subham.dblibrary:dblibrary:1.0'
3. Sync the project with Gradle Files.
4. In your MainActivity.java File use the following code:


 public class MainActivity extends Activity {  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     View v = new MyView(this);  
     setContentView(v);  
     String[] tagnames={"company","news","url"}; //These are the keys of the JSON ARRay  
     String[] tagtype={"text","text","image"}; //This is type of value, it can be text or image  
     String arrayname="result";// This is the name of array of JSON File  
      MyView.setinputdata("http://hello45.esy.es/PhotoUpload/getAllImages.php",arrayname,tagnames,tagtype,R.layout.list_card_view);  
 //As you see you need to pass only URL, Name of result array,names of key in JSON key value pairs,And a layout file to see the results.Make sure while making layout files you give id to each elements as p1,p2,p3..... as specifie in tagnames.  
   }  
 }  


As you can see in the above code, only 5 parameters need to be passed to the function. These are:

  • URL: Enter the url where JSON is being returned from.
  • tagnames: These show the keys in the key value pair of JSON. List here those keys that you wish to show in your Recycler View.
  • tagtypes: This is for Recycler view to understand whether it should display a TextView or an ImageView. Hence corresponding to the tagnames values enter the tagntypes. Only options possible are "text" or "image". Make sure there order is same i..e as shown in above code company corresponds to text,news to text and url to image.
  • arrayname: It is the name of the final array in JSON . For eg in JSON showed above it is "result". 
  • Layout File: Make an XML file for the layout of how you want to display items of recycler view. I have provided you with one sample file. All you need is to set TextViews and ImageViews corresponding to no of parameters in tagname array. While making the layout File keep in mind to name the elements ids as p1,p2,p3... etc only. For eg in XML File, TextView corresponding to company should have id as p1,TextView corresponding to news should have id as p2, and Imageview corresponding to url should have id p3.
5. Make sure to add Internet permissions in Android XML File.
6. That's all Run your Project and BINGO it works!!!.

You can find the reference to XML File here.


 <?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical" android:layout_width="match_parent"  
   android:layout_height="wrap_content">  
   <android.support.v7.widget.CardView  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content">  
     <LinearLayout  
       android:orientation="vertical"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content" >  
       <TextView  
         android:layout_width="fill_parent"  
         android:layout_height="wrap_content"  
         android:id="@+id/p1"  
         android:textColor="#ff342511"  
         android:textSize="20dp"  
         android:text="Paper name"  
         android:textStyle="bold" />  
       <TextView  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:textStyle="bold"  
         android:text="New Text"  
         android:id="@+id/p2"  
         android:textColor="#ffffb54b" />  
       <TextView  
         android:layout_width="wrap_content"  
         android:layout_height="wrap_content"  
         android:textStyle="bold"  
         android:text="New Text"  
         android:id="@+id/p3"  
         android:textColor="#ff100920" />  
     </LinearLayout>  
   </android.support.v7.widget.CardView>  
 </LinearLayout>  



GITHUB CODE:

Visit:  https://github.com/subhamG98/Android-Parse-JSON-in-one-line-function


License:



Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

1 comment: