Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: reading instagram api

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default reading instagram api

    Step by step I'm getting closer. My api from instagram get read out in the run section when I check it, but then in the end it says: at data of type org.json.JSONObject cannot be converted to JSONArray
    Anyone what is going wrong?

    If someone can help me with this, I would be really gratefull because I'm new to api reading and I'm also building an app where the data would be loaded in a gridview probably with the retrofit library.
    This is one of the biggest threshold right now that is holding me back to contineu with my app building.

    buttonParse.setonclickListener(new View.onclickListener() {
                @Override
                public void onclick(View v) {
                    jsonParse();
                }
            });
     
    private void jsonParse() {
            String url = "https://api.instagram.com/v1/users/self/?access_token=2086453534.15c5d2e.8a4c8d77992444459faf6e7f98005a4e\n";
     
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                JSONArray jsonArray = response.getJSONArray("data");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject data = jsonArray.getJSONObject(i);
     
                                    String firstName = data.getString("id");
                                    String username = data.getString("username");
                                    String profile_picture = data.getString("profile_picture");
     
                                    mTextViewResult.append(firstName + ", " + username + ", " + profile_picture + "\n\n");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onerrorResponse(VolleyError error) {
                    error.printStackTrace();
                }
            });
            mQueue.add(request);
        }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: reading instagram api

    data of type org.json.JSONObject cannot be converted to JSONArray
    At What statement does that error message happen?
    Can you post the error log that shows where the code was executing?

    api reading
    Can you post the link to the API doc you are having problems with?

    --- Update ---

    Also posted here: https://www.dreamincode.net/forums/t...api-instagram/
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading instagram api

    There is something wrong with
    this line: new Response.Listener<JSONObject>() {
    this line: JSONArray jsonArray = response.getJSONArray("data");

    If I'm right I need to turn the jsonarray in jsonobject something like that?


    ERROR:
    W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
    at org.json.JSONObject.getJSONArray(JSONObject.java:5 88)
    at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:97)
    at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:93)
    at com.android.volley.toolbox.JsonRequest.deliverResp onse(JsonRequest.java:83)
    at com.android.volley.ExecutorDelivery$ResponseDelive ryRunnable.run(ExecutorDelivery.java:106)
    at android.os.Handler.handleCallback(Handler.java:751 )
    at android.os.Handler.dispatchMessage(Handler.java:95 )
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.jav a:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:776)

    Logcat:
    As you can see it is reading the api if I'm right, https://imgur.com/qeYKGtv
    but it is not displaying the strings on the screen.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: reading instagram api

      JSONArray jsonArray = response.getJSONArray("data");
    Do you have a link to the API doc that defines how the getJSONArray method is defined and what it does?
    Your code tries to assign what it returns to a JSONArray object. What is the method defined to return?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading instagram api

    You mean the information that I try to read out? I use postman, I put my instagram API and then I get this.

    {
    "data": {
    "id": "2086453534",
    "username": "izildoo",
    "profile_picture": "https://scontent.cdninstagram.com/vp/879f020320782550b2d6bd01446d3d30/5DC4795B/t51.2885-19/s150x150/21688515_131769624114093_2099668670446829568_n.jpg ?_nc_ht=scontent.cdninstagram.com",
    "full_name": "Izildo Pimentel",
    "bio": "",
    "website": "",
    "is_business": false,
    "counts": {
    "media": 12,
    "follows": 50,
    "followed_by": 35
    }
    },
    "meta": {
    "code": 200
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: reading instagram api

    No, I was asking about the API doc for the class that contains the getJSONArray method. For example like this link to the String class's concat method:
    https://docs.oracle.com/javase/8/doc...a.lang.String-

    That API doc shows the concat method takes a String as argument and returns a String.

    To see what class object is returned, print out what is returned by the getClass method:
       System.out.println( response.getJSONArray("data").getClass()); // show class
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2019
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading instagram api

    A little bit hard for me to understand what you looking for, I'm a little bit noob srry for that.
    I tried to read out with the systemln but I don't saw it back it the logcat or run.

    Anyway right click I went to the supermethod where to code is documented, idk if thats what you asking for:
    package com.android.volley;
     
    /**
     * Encapsulates a parsed response for delivery.
     *
     * @param <T> Parsed type of this response
     */
    public class Response<T> {
     
        /** Callback interface for delivering parsed responses. */
        public interface Listener<T> {
            /** Called when a response is received. */
            void onResponse(T response);
        }
     
        /** Callback interface for delivering error responses. */
        public interface ErrorListener {
            /**
             * Callback method that an error has been occurred with the
             * provided error code and optional user-readable message.
             */
            void onErrorResponse(VolleyError error);
        }
     
        /** Returns a successful response containing the parsed result. */
        public static <T> Response<T> success(T result, Cache.Entry cacheEntry) {
            return new Response<T>(result, cacheEntry);
        }
     
        /**
         * Returns a failed response containing the given error code and an optional
         * localized message displayed to the user.
         */
        public static <T> Response<T> error(VolleyError error) {
            return new Response<T>(error);
        }
     
        /** Parsed response, or null in the case of error. */
        public final T result;
     
        /** Cache metadata for this response, or null in the case of error. */
        public final Cache.Entry cacheEntry;
     
        /** Detailed error information if <code>errorCode != OK</code>. */
        public final VolleyError error;
     
        /** True if this response was a soft-expired one and a second one MAY be coming. */
        public boolean intermediate = false;
     
        /**
         * Returns whether this response is considered successful.
         */
        public boolean isSuccess() {
            return error == null;
        }
     
     
        private Response(T result, Cache.Entry cacheEntry) {
            this.result = result;
            this.cacheEntry = cacheEntry;
            this.error = null;
        }
     
        private Response(VolleyError error) {
            this.result = null;
            this.cacheEntry = null;
            this.error = error;
        }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: reading instagram api

    what you looking for
    I am looking for the API documentation for the getJSONArray method. Did you see the link I provided for the concat method?
    I am looking for a link like that that refers to the API doc for the getJSONArray method.

    Did you try what I suggested: Add a statement to the code to print out what data type is returned by the getJSONArray method?
                       public void onResponse(JSONObject response) {
                            System.out.println("class="+ response.getJSONArray("data").getClass()); // show returned class
                            try {
                                JSONArray jsonArray = response.getJSONArray("data");
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2019
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading instagram api

    This is what is printed out, the same thing like before:

    at org.json.JSON.typeMismatch(JSON.java:100)
    at org.json.JSONObject.getJSONArray(JSONObject.java:5 88)
    at com.izildo.android.navigationapp.MainActivity$2$ov erride.onResponse(MainActivity.java:110)
    at com.izildo.android.navigationapp.MainActivity$2$ov erride.access$dispatch(MainActivity.java)
    at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java)
    at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:93)
    at com.android.volley.toolbox.JsonRequest.deliverResp onse(JsonRequest.java:83)
    at com.android.volley.ExecutorDelivery$ResponseDelive ryRunnable.run(ExecutorDelivery.java:106)
    at android.os.Handler.handleCallback(Handler.java:751 )
    at android.os.Handler.dispatchMessage(Handler.java:95 )
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.jav a:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:776)

    Link: https://developer.android.com/refere...a.lang.String)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: reading instagram api

    The posted error message is missing the first lines that show what the error was.

    What was printed by this statement that I asked you to add?
                      System.out.println("class="+ response.getJSONArray("data").getClass()); // show returned class

    Thanks for the link to the API doc. The API doc for the method says the method:
    Throws JSONException if the mapping doesn't exist or is not a JSONArray.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jul 2019
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading instagram api

    The error that I gave you above.

    What in my code do I need to change to make it work?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: reading instagram api

    It would help if you copied ALL of the error message and pasted it here all together, not in separate posts.
    Also be sure to add the print statement I suggested that shows the class of the object returned by the get method.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jul 2019
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading instagram api

    The Logcat:

    2019-07-12 14:50:12.757 1600-1778/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 9909694 , only wrote 9909360
    2019-07-12 14:50:12.891 30452-30452/com.izildo.android.navigationapp W/System.err: org.json.JSONException: Value {"id":"2086453534","username":"izildoo","profile_p icture":"https:\/\/scontent.cdninstagram.com\/vp\/879f020320782550b2d6bd01446d3d30\/5DC4795B\/t51.2885-19\/s150x150\/21688515_131769624114093_2099668670446829568_n.jpg ?_nc_ht=scontent.cdninstagram.com","full_name":"Iz ildo Pimentel","bio":"","website":"","is_business":fals e,"counts":{"media":12,"follows":50,"followed_by": 35}} at data of type org.json.JSONObject cannot be converted to JSONArray
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:5 88)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:96)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:92)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResp onse(JsonRequest.java:83)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.android.volley.ExecutorDelivery$ResponseDelive ryRunnable.run(ExecutorDelivery.java:106)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at android.os.Handler.handleCallback(Handler.java:751 )
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95 )
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at android.os.Looper.loop(Looper.java:154)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at android.app.ActivityThread.main(ActivityThread.jav a:6119)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:886)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:776)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp I/System.out: under here is the method!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: org.json.JSONException: Value {"id":"2086453534","username":"izildoo","profile_p icture":"https:\/\/scontent.cdninstagram.com\/vp\/879f020320782550b2d6bd01446d3d30\/5DC4795B\/t51.2885-19\/s150x150\/21688515_131769624114093_2099668670446829568_n.jpg ?_nc_ht=scontent.cdninstagram.com","full_name":"Iz ildo Pimentel","bio":"","website":"","is_business":fals e,"counts":{"media":12,"follows":50,"followed_by": 35}} at data of type org.json.JSONObject cannot be converted to JSONArray
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:5 88)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:110)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:92)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResp onse(JsonRequest.java:83)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at com.android.volley.ExecutorDelivery$ResponseDelive ryRunnable.run(ExecutorDelivery.java:106)
    2019-07-12 14:50:12.892 30452-30452/com.izildo.android.navigationapp W/System.err: at android.os.Handler.handleCallback(Handler.java:751 )
    2019-07-12 14:50:12.893 30452-30452/com.izildo.android.navigationapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95 )
    2019-07-12 14:50:12.893 30452-30452/com.izildo.android.navigationapp W/System.err: at android.os.Looper.loop(Looper.java:154)
    2019-07-12 14:50:12.893 30452-30452/com.izildo.android.navigationapp W/System.err: at android.app.ActivityThread.main(ActivityThread.jav a:6119)
    2019-07-12 14:50:12.893 30452-30452/com.izildo.android.navigationapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
    2019-07-12 14:50:12.893 30452-30452/com.izildo.android.navigationapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:886)
    2019-07-12 14:50:12.893 30452-30452/com.izildo.android.navigationapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:776)

    Run Time:

    W/System.err: org.json.JSONException: Value {"id":"2086453534","username":"izildoo","profile_p icture":"https:\/\/scontent.cdninstagram.com\/vp\/879f020320782550b2d6bd01446d3d30\/5DC4795B\/t51.2885-19\/s150x150\/21688515_131769624114093_2099668670446829568_n.jpg ?_nc_ht=scontent.cdninstagram.com","full_name":"Iz ildo Pimentel","bio":"","website":"","is_business":fals e,"counts":{"media":12,"follows":50,"followed_by": 35}} at data of type org.json.JSONObject cannot be converted to JSONArray
    W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
    at org.json.JSONObject.getJSONArray(JSONObject.java:5 88)
    at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:96)
    at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:92)
    at com.android.volley.toolbox.JsonRequest.deliverResp onse(JsonRequest.java:83)
    at com.android.volley.ExecutorDelivery$ResponseDelive ryRunnable.run(ExecutorDelivery.java:106)
    at android.os.Handler.handleCallback(Handler.java:751 )
    at android.os.Handler.dispatchMessage(Handler.java:95 )
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.jav a:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:776)
    I/System.out: under here is the method!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    W/System.err: org.json.JSONException: Value {"id":"2086453534","username":"izildoo","profile_p icture":"https:\/\/scontent.cdninstagram.com\/vp\/879f020320782550b2d6bd01446d3d30\/5DC4795B\/t51.2885-19\/s150x150\/21688515_131769624114093_2099668670446829568_n.jpg ?_nc_ht=scontent.cdninstagram.com","full_name":"Iz ildo Pimentel","bio":"","website":"","is_business":fals e,"counts":{"media":12,"follows":50,"followed_by": 35}} at data of type org.json.JSONObject cannot be converted to JSONArray
    at org.json.JSON.typeMismatch(JSON.java:100)
    at org.json.JSONObject.getJSONArray(JSONObject.java:5 88)
    at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:110)
    at com.izildo.android.navigationapp.MainActivity$2.on Response(MainActivity.java:92)
    at com.android.volley.toolbox.JsonRequest.deliverResp onse(JsonRequest.java:83)
    at com.android.volley.ExecutorDelivery$ResponseDelive ryRunnable.run(ExecutorDelivery.java:106)
    at android.os.Handler.handleCallback(Handler.java:751 )
    W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95 )
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.jav a:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:776)

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: reading instagram api

    Where is the print out from the print statement I asked you to add to the code? I don't see it in the logcat.

    What statements are at line 92 and at line 110 and at line 96?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jul 2019
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: reading instagram api

    You see at I/System: under here is the method!!!!!!!!!!!!!!!!!!!!!
    I specifically made it this way so you can this that under that line is the method print you asked for.

    Look carefully a little bit at the mid/bottomside.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,324
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: reading instagram api

    under that line is the method print you asked for.
    Does that mean the print statement is causing the exception?

    The API doc for the getJSONArray method says the method:
    Throws JSONException if the mapping doesn't exist or is not a JSONArray.
    That says the getJSONArray method will throw the exception if the mapping does not exist or is not a JSONArray..

    --- Update ---

    What statements are at line 92 and at line 110 and at line 96?
    Please post the code with labels showing those lines.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Is this code an API? Beginner learning to write an API and decoupling.
    By Learning2Java in forum Java Theory & Questions
    Replies: 3
    Last Post: April 22nd, 2014, 12:59 PM
  2. java api
    By mgbako in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 26th, 2013, 06:50 AM
  3. In need of API
    By Flamz in forum Java Theory & Questions
    Replies: 1
    Last Post: April 7th, 2013, 08:45 AM
  4. Best 3D API
    By tyb97 in forum Java SE APIs
    Replies: 2
    Last Post: August 3rd, 2012, 04:48 PM
  5. Difference between Speech API and Sound API
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: October 22nd, 2009, 12:22 AM