android_guides icon indicating copy to clipboard operation
android_guides copied to clipboard

What if JSON starts with an array

Open harshipandey opened this issue 6 years ago • 4 comments

[ { "id": "1", "userName": "Admin", "fullName": "Admin", "emailID": "[email protected]", "empNo": "p001", "mobileNo": null, "grade": "W0", "gradeId": 1, "allGrades": null, "empType": 1, "empTypeName": "Regular", "allEmpTypes": null, "deptId": 0, "department": "Human Resource", "allDepartments": null, "allProjects": null, "projectsAssigned": null, "post": null, "extension": null, "seatNo": null, "dob": null, "doj": null, "gender": 0, "bloodGroup": 0, "reportingTo": null, "postedAt": null, "photo": null, "imageData": null, "status": 0 }, { "id": "2", "userName": "User", "fullName": "User", "emailID": "[email protected]", "empNo": "p002", "mobileNo": "8804354448", "grade": "W0", "gradeId": 1, "allGrades": null, "empType": 1, "empTypeName": "Regular", "allEmpTypes": null, "deptId": 0, "department": "Technical", "allDepartments": null, "allProjects": null, "projectsAssigned": null, "post": null, "extension": "980", "seatNo": null, "dob": null, "doj": null, "gender": 0, "bloodGroup": 0, "reportingTo": null, "postedAt": null, "photo": null, "imageData": null, "status": 0 }] Thank you for the code. This is my json. I want to search by username or fullname. Can anyone please help me what to write in onsuccess portion in mainactivity (according to the given code) with such type of json.

harshipandey avatar May 27 '18 14:05 harshipandey

You can override public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers,org.json.JSONArray response) Method. Source: https://loopj.com/android-async-http/doc/

So your MainActivity code may look like

    YelpClient client = YelpClientApp.getRestClient();
    client.search("food", "san francisco", new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
            try {
                ArrayList<User> businesses = User.fromJson(response);
                // Now we have an array of business objects
                // Might now create an adapter BusinessArrayAdapter<Business> to load the businesses into a list
                // You might also simply update the data in an existing array and then notify the adapter
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
            Toast.makeText(getBaseContext(), "FAIL", Toast.LENGTH_LONG).show();
        }
    });

Hope this will help

nareshkatta99 avatar May 27 '18 15:05 nareshkatta99

I did this. But not success. maybe I am doing something Wrong. Anyways I Updated my json API. thanks btw. your project is showing all the names on clicking on search. What should i do if i want only those names which are starting with say "Narrative Theory" Thanks in advance. :)

On Sun, May 27, 2018 at 8:38 PM, Naresh Katta [email protected] wrote:

You can override public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers,org.json.JSONArray response) Method. Source: https://loopj.com/android-async-http/doc/

So your MainActivity code may look like

YelpClient client = YelpClientApp.getRestClient();
client.search("food", "san francisco", new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
        try {
            ArrayList<User> businesses = User.fromJson(response);
            // Now we have an array of business objects
            // Might now create an adapter BusinessArrayAdapter<Business> to load the businesses into a list
            // You might also simply update the data in an existing array and then notify the adapter
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
        Toast.makeText(getBaseContext(), "FAIL", Toast.LENGTH_LONG).show();
    }
});

Hope this will help

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/codepath/android_guides/issues/329#issuecomment-392337910, or mute the thread https://github.com/notifications/unsubscribe-auth/Al1ezvs10BYzfa5ZmILhDParHFeGAFZiks5t2sFSgaJpZM4UPTGw .

harshipandey avatar May 28 '18 10:05 harshipandey

---------- Forwarded message ---------- From: Harshi Pandey [email protected] Date: Mon, May 28, 2018 at 3:50 PM Subject: Re: [codepath/android_guides] What if JSON starts with an array (#329) To: codepath/android_guides <reply+025d5ecec8841925c8db63ada1e59f 17fa862fbc110cef5592cf0000000117228bd292a169ce137ae330@reply.github.com>

I did this. But not success. maybe I am doing something Wrong. Anyways I Updated my json API. thanks btw. your project is showing all the names on clicking on search. What should i do if i want only those names which are starting with say "Narrative Theory" Thanks in advance. :)

On Sun, May 27, 2018 at 8:38 PM, Naresh Katta [email protected] wrote:

You can override public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers,org.json.JSONArray response) Method. Source: https://loopj.com/android-async-http/doc/

So your MainActivity code may look like

YelpClient client = YelpClientApp.getRestClient();
client.search("food", "san francisco", new JsonHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
        try {
            ArrayList<User> businesses = User.fromJson(response);
            // Now we have an array of business objects
            // Might now create an adapter BusinessArrayAdapter<Business> to load the businesses into a list
            // You might also simply update the data in an existing array and then notify the adapter
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
        Toast.makeText(getBaseContext(), "FAIL", Toast.LENGTH_LONG).show();
    }
});

Hope this will help

—Thanks You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/codepath/android_guides/issues/329#issuecomment-392337910, or mute the thread https://github.com/notifications/unsubscribe-auth/Al1ezvs10BYzfa5ZmILhDParHFeGAFZiks5t2sFSgaJpZM4UPTGw .

harshipandey avatar Jun 01 '18 08:06 harshipandey

if you are using RxJava then

    @GET(".")
    Observable<List<VehicleUnit>> getVehicles();

you can check out this repo for the network layer:

https://github.com/ir2pid/AndroidArch/blob/master/app/src/main/java/com/noisyninja/androidlistpoc/layers/network/INetworkDao.java

https://github.com/ir2pid/AndroidArch/blob/master/app/src/main/java/com/noisyninja/androidlistpoc/layers/network/NetworkModule.kt

ir2pid avatar Jun 02 '18 14:06 ir2pid