0

working on an android application to try and connect to "steemit.com' and return JSON data to me.

Everything has been working so far, printing the mass response from the URL to the Textview, only I am now getting no errors, and no text printed out in the screen so I assume I am using the wrong type of object or something. Perhaps the data I am trying to retrieve is not an Array? What do you all think? Here is my code.

    public class fetchdata extends AsyncTask<Void,Void,Void> {

    String data = "";
    String dataParsed = "";
    String singleParsed = "";

    @Override
    protected Void doInBackground(Void... voids) {


        {
            try {
                URL url = new URL("https://steemit.com/@curie.json");

                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
                InputStream inputStream = httpsURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                String lines = "";
                while(lines != null){
                    lines = bufferedReader.readLine();
                    data = data + lines;
                }

                JSONArray JA = new JSONArray(data);
                for (int i =0 ;i < JA.length(); i++){
                    JSONObject JO = (JSONObject) JA.get(i);
                    singleParsed = "User: " + JO.get("user") + "\n" +
                                    "Location: " + JO.get("location") + "\n" +
                                    "ID: " + JO.get("id")+"\n";

                    dataParsed = dataParsed + singleParsed;
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        followers.dataTV.setText(this.dataParsed);


    }


}

and the page I expect the TextView to display data on.

    public class followers extends AppCompatActivity {
    public static TextView dataTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_followers);

        ListView followList = (ListView)findViewById(R.id.followList);
        dataTV = (TextView)findViewById(R.id.followersTVData);

        fetchdata process = new fetchdata();
        process.execute();


    }
}

If I have not been clear, what the issue is, is that when I 'printText' using the variable 'data', there is no problems, and the bulk text is printed, however, I am now trying to break it down into bits, and it is just not printing anything when I use the variable 'dataParsed'. Any help is appreciated. Thank you in advance!

I have been asked for the response. Here it is, though rather long.

{"user":{"id":1026971,"name":"ceruleanblue","owner":{"weight_threshold":1,"account_auths":[],"key_auths":[["STM7UPr1LJMw4aAxcuiYAmad6bjjiaeDcfgSynRMrr5L6uvuSJLDJ",1]]},"active":{"weight_threshold":1,"account_auths":[],"key_auths":[["STM7qUaQCghsFZA37fTxVB4BqBBK49z35ni6pha1Kr4q4qLkrNRyH",1]]},"posting":{"weight_threshold":1,"account_auths":[["minnowbooster",1],["steemauto",1]],"key_auths":[["STM7qF27DSYNYjRu5Jayxxxpt1rtEoJLH6c1ekMwNpcDmGfsvko6z",1]]},"memo_key":"STM7wNQdNS9oPbVXscbzn7vfzjB7SwmLGQuFQNzZgatgpqvdKzWQZ","json_metadata":{"profile":{"profile_image":"https://cdn.steemitimages.com/DQmfNj7SLU1aBtV9UkJa5ZKMZPNuzR4ei5UJRA54JxFk99M/Mushrooms%20Trippy%20Art%20Fabric%20Cloth%20Rolled%20Wall%20Poster%20Print.jpg","name":"Cerulean's Chillzone","about":"IT Technician, Programmer, Day Trader, Night Toker.","location":"Ontario, Canada","cover_image":"https://cdn.steemitimages.com/DQmTwT379V7EcQ1ZkqkmJkpWyu4QXw1LzDinv9uoyixksMY/tumblr_static_tumblr_static__640.jpg"}},"proxy":"","last_owner_update":"2018-06-18T19:57:39","last_account_update":"2018-08-01T04:33:06","created":"2018-06-03T20:28:21","mined":false,"recovery_account":"steem","last_account_recovery":"1970-01-01T00:00:00","reset_account":"null","comment_count":0,"lifetime_vote_count":0,"post_count":321,"can_vote":true,"voting_power":9800,"last_vote_time":"2018-08-09T02:47:03","balance":"8.000 STEEM","savings_balance":"0.000 STEEM","sbd_balance":"1.979 SBD","sbd_seconds":"927621285","sbd_seconds_last_update":"2018-08-09T13:23:15","sbd_last_interest_payment":"2018-07-11T10:18:12","savings_sbd_balance":"0.000 SBD","savings_sbd_seconds":"2067163545","savings_sbd_seconds_last_update":"2018-07-23T08:58:48","savings_sbd_last_interest_payment":"2018-07-09T06:32:27","savings_withdraw_requests":0,"reward_sbd_balance":"0.000 SBD","reward_steem_balance":"0.000 STEEM","reward_vesting_balance":"0.000000 VESTS","reward_vesting_steem":"0.000 STEEM","vesting_shares":"167703.513691 VESTS","delegated_vesting_shares":"29412.000000 VESTS","received_vesting_shares":"0.000000 VESTS","vesting_withdraw_rate":"0.000000 VESTS","next_vesting_withdrawal":"1969-12-31T23:59:59","withdrawn":0,"to_withdraw":0,"withdraw_routes":0,"curation_rewards":182,"posting_rewards":110408,"proxied_vsf_votes":[0,0,0,0],"witnesses_voted_for":1,"last_post":"2018-08-07T12:43:42","last_root_post":"2018-08-07T12:25:39","average_bandwidth":"44620566375","lifetime_bandwidth":"1099256000000","last_bandwidth_update":"2018-08-09T13:23:15","average_market_bandwidth":3415484305,"lifetime_market_bandwidth":"237250000000","last_market_bandwidth_update":"2018-08-07T13:21:39","vesting_balance":"0.000 STEEM","reputation":"1564749115439","transfer_history":[],"market_history":[],"post_history":[],"vote_history":[],"other_history":[],"witness_votes":["guiltyparties"],"tags_usage":[],"guest_bloggers":[]},"status":"200"}null

Perhaps I have implemented this improperly?

            for (int i =0 ;i < JA.length(); i++){
                JSONObject JO = (JSONObject) JA.getJSONObject(i);
                singleParsed = "User: " + JO.get("user.id") + "\n" +
                                "Location: " + JO.get("location") + "\n" +
                                "ID: " + JO.get("id")+"\n";

                dataParsed = dataParsed + singleParsed;
            }

UPDATED FIXES, STILL BROKEN BUT FARTHER ALONG.

            String lines = "";
            while(lines != null){
                lines = bufferedReader.readLine();
                data = data + lines;
            }

            JSONObject JO = new JSONObject(data);
            String m = "";
            for (int i =0 ;i < JO.length(); i++){
//                    JSONObject JO = (JSONObject) JO.getJSONObject(i);
                singleParsed = "User: " + JO.getString("user.id") + "\n" +
                                "Location: " + JO.getString("location") + "\n" +
                                "ID: " + JO.getString("id")+"\n";

                dataParsed = dataParsed + singleParsed;

DEBUGGER BREAKS ON "singleParsed = "user:", any ideas from here?

2
  • can you post the response. Commented Aug 9, 2018 at 16:59
  • Haha you asked for it :) The response is added above. Unfortunately, the debugger shows absolutely no data being recorded during the FOR loop. Commented Aug 9, 2018 at 17:01

3 Answers 3

0

The response is a JSONObject not a JSONArray.
So, you can directly use: new JSONObject(data); in your code.

Also, as you haven't noticed, there's a null at the end after the closing brace.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought I got rid of those nulls! Thank you! Reboot!
0

I think you should parse the data with JSONObject, because the response is not an array. You should create class which contain User class and String for the status to handle the response.

Or you can use retrofit instead. http://square.github.io/retrofit/

1 Comment

I was thinking this, will try again. Thank you!
0

Try this...

    try {
            JSONObject object = new JSONObject(data);
            String user = object.getString("user");
            int id = user.getInt("id");
            String name = user.getString("name"); 
            String owner = user.getString("owner");
            int weight_threshold = owner.getInt("weight_threshold");
            JSONArray account_auths = owner.getJSONArray("account_auths");   
.....        
        } catch (Exception e) {
           e.printStackTrace();
        }

pass other objects so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.