Skip to main content

Complex Dao Class

1 ) Demo Dao
package com.example.discusit.demoblogapitest.dao;

import android.content.Context;
import android.util.Log;

import com.example.discusit.demoblogapitest.model.DemoModel;
import com.example.discusit.demoblogapitest.webservices.WebFields;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class DemoDao implements OnDemoAPITestListener 
{
    private Context mActivity;
    private String status,message,data,id, name,country,city,mobile,android;

    public DemoDao(Context context)
    {
        this.mActivity=context;
    }
    @Override    public ArrayList<DemoModel> getDemoData(Context context, JSONObject jsonObject)
    {
        ArrayList<DemoModel> mArrDemoData = new ArrayList<DemoModel>();
        try        {
//            for(int i=0; i<jsonObject.length(); i++)//            {                status=jsonObject.getString(WebFields.TESTAPI.RESPONSE_STATUS);
                message=jsonObject.getString(WebFields.TESTAPI.RESPONSE_MESSAGE);
                data=jsonObject.getString(WebFields.TESTAPI.RESPONSE_ARR_DATA);
                JSONArray jsonArray1 = new JSONArray(data);

                for(int j=0;j<jsonArray1.length();j++)
                {
                    JSONObject jsonDoc = jsonArray1.getJSONObject(j);
                    id = jsonDoc.getString(WebFields.TESTAPI.RESPONSE_ID);
                    name = jsonDoc.getString(WebFields.TESTAPI.RESPONSE_NAME);
                    country = jsonDoc.getString(WebFields.TESTAPI.RESPONSE_COUNTRY);
                    city = jsonDoc.getString(WebFields.TESTAPI.RESPONSE_CITY);
                    mobile=jsonDoc.getString(WebFields.TESTAPI.RESPONSE_ARR_MOBILE);
                    JSONArray jsonArrayMobile = new JSONArray(mobile);
                    Log.e("Length:>>>>>>>>>>", String.valueOf(jsonArrayMobile.length()));
                    for (int m=0;m<jsonArrayMobile.length();m++)
                    {
                        JSONObject jsonAndroid = jsonArrayMobile.getJSONObject(m);
                        android=jsonAndroid.getString(WebFields.TESTAPI.RESPONSE_ANDROID);
                        DemoModel demoModel  = new DemoModel(status,message,id,name,country,city,android);
                        mArrDemoData.add(demoModel);
                    }
                }
                Log.e("Size:>>>>>>>>>>", String.valueOf(mArrDemoData.size()));
//            }
        } catch (Exception exception)
        {
            Log.e("ERROR:>>>>>>>>>>", String.valueOf(exception));
        }
        return mArrDemoData;
    }
}

Response
--------------------------------------------------------------------------------------------------------------------------
{
    "status": "true",
    "message": "Data fetched successfully!",
    "data": [
        {
            "id": "1",
            "name": "Roger Federer",
            "country": "Switzerland",
            "city": "Basel",
            "mobile": [
                {
                    "android": "Samsung"
                }
            ]
        },
        {
            "id": "2",
            "name": "Rafael Nadal",
            "country": "Spain",
            "city": "Madrid",
            "mobile": [
                {
                    "android": "One PLus"
                }
            ]
        },
        {
            "id": "3",
            "name": "Novak Djokovic",
            "country": "Serbia",
            "city": "Monaco",
            "mobile": [
                {
                    "android": "Redmi"
                }
            ]
        },
        {
            "id": "4",
            "name": "Andy Murray",
            "country": "United Kingdom",
            "city": "London",
            "mobile": [
                {
                    "android": "Gionee"
                }
            ]
        },
        {
            "id": "5",
            "name": "Maria Sharapova",
            "country": "Russia",
            "city": "Moscow",
            "mobile": [
                {
                    "android": "RealMe"
                }
            ]
        },
        {
            "id": "6",
            "name": "Caroline Wozniacki",
            "country": "Denmark",
            "city": "Odense",
            "mobile": [
                {
                    "android": "Oppo"
                }
            ]
        },
        {
            "id": "7",
            "name": "Eugenie Bouchard",
            "country": "Canada",
            "city": " Montreal",
            "mobile": [
                {
                    "android": "Vivo"
                }
            ]
        },
        {
            "id": "8",
            "name": "Ana Ivanovic",
            "country": "Serbia",
            "city": "Belgrade",
            "mobile": [
                {
                    "android": "Nokia"
                }
            ]
        }
    ]
}


2 ) Demo Dao
import android.content.Context;
import android.util.Log;
import com.example.discusit.sqlitedatabasedemowithapicalling.model.DemoModel;
import com.example.discusit.sqlitedatabasedemowithapicalling.webservices.WebFields;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
public class DemoDao implements OnDemoListener
{
    private String All;
    private String Title;
    private String Url;
    private ArrayList<String>mArrayCategory;
    private String mainObject;
    private String articleList;
    private String category;
    private Context mActivity;
    public DemoDao(Context context)
    {
        this.mActivity=context;
    }
    @Override
    public ArrayList<DemoModel> getDemoList(Context context, JSONObject jsonObject) {
        ArrayList<DemoModel> mDemoListModel = new ArrayList<DemoModel>();
        try
        {
            mainObject=jsonObject.getString(WebFields.Main_OBJECT_LIST.RESPONSE_MAIN_JSON_OBJECT);
            JSONObject jsonObjectMain=new JSONObject(mainObject);
            All=jsonObjectMain.getString(WebFields.Main_OBJECT_LIST.RESPONSE_ALL); 
            articleList=jsonObjectMain.getString(WebFields.Main_OBJECT_LIST.RESPONSE_ARR_MAIN_ARTICLE_LIST);
            JSONArray jsonArrayArticleList=new JSONArray(articleList);
            Log.e("Size:>>>>>>>>>>>", String.valueOf(jsonArrayArticleList.length()));
            for(int i=0; i<jsonArrayArticleList.length(); i++)
            {
                JSONObject jsonObject1 = jsonArrayArticleList.getJSONObject(i);
//                JSONArray jsonArray1=jsonObject1.getJSONArray(WebFields.Main_OBJECT_LIST.RESPONSE_ARR_CATEGORIES);
                Title= jsonObject1.getString(WebFields.Main_OBJECT_LIST.RESPONSE_TITLE);
                Url = jsonObject1.getString(WebFields.Main_OBJECT_LIST.RESPONSE_URL);
//                mArrayCategory.add(jsonArray1.get(i).toString());
                DemoModel demoModel = new DemoModel(All,Title,Url,mArrayCategory);
                mDemoListModel.add(demoModel);
            }
        } catch (Exception exception) {
            Log.e("Error:>>>>>>>>>>>", String.valueOf(exception));
        }
        return mDemoListModel;
    }
}


Response
--------------------------------------------------------------------------------------------------------------------------

    "permissions": {
        "CREATE_SITE": "true"
    },
    "MainJsonObject": {
        "ALL": "true",
        "articleList": [
            {
                "title": "One",
                "url": "http://hmkcode.com/android-internet-connection-using-http-get-httpclient/",
                "categories": [
                    "Android"
                ],
                "tags": [
                    "android",
                    "httpclient",
                    "internet"
                ]
            },
            {
                "title": " Two",
                "url": "http://hmkcode.com/android-camera-taking-photos-camera/",
                "categories": [
                    "Android"
                ],
                "tags": [
                    "android",
                    "camera"
                ]
            },
            {
                "title": " Three",
                "url": "http://hmkcode.com/android-camera-taking-photos-camera/",
                "categories": [
                    "Android"
                ],
                "tags": [
                    "android",
                    "camera"
                ]
            },
            {
                "title": " Four ",
                "url": "http://hmkcode.com/android-camera-taking-photos-camera/",
                "categories": [
                    "Android"
                ],
                "tags": [
                    "android",
                    "camera"
                ]
            },
            {
                "title": " Five ",
                "url": "http://hmkcode.com/android-camera-taking-photos-camera/",
                "categories": [
                    "Android"
                ],
                "tags": [
                    "android",
                    "camera"
                ]
            }
        ]
    }
}

Comments

Popular posts from this blog

Post data with retrofit

1 ) Retrofit Client Class public class RetrofitClient { private static final String AUTH = "Basic " + Base64. encodeToString (( "belalkhan:123456" ).getBytes(), Base64. NO_WRAP ); private static final String BASE_URL = "http://simplifiedlabs.xyz/MyApi/public/" ; private static RetrofitClient mInstance ; private Retrofit retrofit ; private RetrofitClient() { OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor( new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); Request.Builder requestBuilder = original.newBuilder() .addHeader( "Authorization" , AUTH ) .meth...

Android MVVM (Model View ViewModel) Design Pattern

DataBinding Pdf Link:-   https://drive.google.com/open?id=1uL0xK9EktcEWOke5vqUqp-rgqpA1ARR3 DataBinding References : -  https://www.youtube.com/watch?v=v4XO_y3RErI Steps for applying MVVM design pattern to Android Project  1 ) Applying the changes in build.gradle (App Level) file      android {     compileSdkVersion 27     defaultConfig {         applicationId "com.credencys.databindingwithrecyclerview"         minSdkVersion 15         targetSdkVersion 27         versionCode 1         versionName "1.0"         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"     }     buildTypes {         release {             minifyEnabled false             proguardFiles ge...

Complex Retrofit Class With Database

URL:-  http://www.mocky.io/v2/ 5b936d6f33000011002061d3 1 )   Create   RetrofitClient(Base URL Class) import retrofit2.GsonConverterFactory; import retrofit2.Retrofit; public class RetrofitClient { //http://www.mocky.io/v2/5b936d6f33000011002061d3 public static final String API_BASE_URL = "http://www.mocky.io/v2/" ; public static Retrofit retrofit = null ; public static Retrofit getApiClient() { if ( retrofit == null ) { retrofit = new Retrofit.Builder().baseUrl( API_BASE_URL ).addConverterFactory(GsonConverterFactory . create ()).build(); } return retrofit ; } } 2 ) Create Retrofit ApiInterface Class(End URL Class) import com.example.discusit.complexretrofitdemo.model.Data; import retrofit2.Call; import retrofit2.http. GET ; public interface ApiInterface { @GET ( "5b936d6f33000011002061d3" ) Call<Data> getData(); } 3 ) Create Require Model Clas...