0
votes

I tried to parse json from a String builder but it give nothings. Here is my code

private class GetClass extends AsyncTask<String, Void, Void> {

        private final Context context;

        public GetClass(Context c){
            this.context = c;
        }

        protected void onPreExecute(){
            progress= new ProgressDialog(this.context);
            progress.setMessage("Loading");
            progress.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            try {

                final TextView outputView = (TextView) findViewById(R.id.showOutput);
                URL url = new URL("my_url");

                HttpURLConnection connection (HttpURLConnection)url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
                connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");

                int responseCode = connection.getResponseCode();

                //System.out.println("\nSending 'POST' request to URL : " + url);
                //System.out.println("Post parameters : " + urlParameters);
                //System.out.println("Response Code : " + responseCode);

                final StringBuilder output = new StringBuilder();
                //output.append(System.getProperty("line.separator") + "Request Parameters " + urlParameters);
                //output.append(System.getProperty("line.separator")  + "Response Code " + responseCode);
                //output.append(System.getProperty("line.separator")  + "Type " + "GET");
                BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line = "";
                StringBuilder responseOutput = new StringBuilder();
                //System.out.println("output===============" + br);
                while((line = br.readLine()) != null ) {
                    responseOutput.append(line);
                }
                br.close();



                output.append(responseOutput.toString());

                UsersActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            JSONObject mainObject = new JSONObject(output.toString());
                            JSONObject id = mainObject.getJSONObject("id");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        outputView.setText(output);
                        progress.dismiss();

                    }
                });


            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

//        protected void onPostExecute() {
//            progress.dismiss();
//        }

    }

==============================================

and here is my json data

[{"id":4,"name":"test","email":"[email protected]","approved":1,"admin":0,"responsible":"test","logo":"","president":"test","members":50,"mission":"test","address":"i'm testing the adress","tel":"12345678","website":"http://test.com","facebook":"http://facebook.com/test","created_at":"2016-03-17 11:45:03","updated_at":"2016-03-22 19:47:47"},{"id":5,"name":"test","email":"[email protected]","approved":1,"admin":1,"responsible":"","logo":null,"president":null,"members":null,"mission":null,"address":null,"tel":null,"website":null,"facebook":null,"created_at":"2016-03-19 08:24:50","updated_at":"2016-03-22 19:48:25"},{"id":7,"name":"test","email":"[email protected]","approved":1,"admin":0,"responsible":"","logo":null,"president":null,"members":null,"mission":null,"address":null,"tel":null,"website":null,"facebook":null,"created_at":"2016-03-19 11:30:07","updated_at":"2016-03-22 19:46:10"},{"id":11,"name":"test","email":"[email protected]","approved":1,"admin":0,"responsible":"","logo":null,"president":null,"members":null,"mission":null,"address":null,"tel":null,"website":null,"facebook":null,"created_at":"2016-03-21 15:52:18","updated_at":"2016-03-23 09:57:55"}]

2
"but it give nothings." meaning? and what do you want to achieve with your JSONObject (your json is a list not some object with an "id")? - user180100
This is a story, what is your question? - tmthydvnprt
why you are parsing heavy data on the UI thread ??? - Amit Ranjan

2 Answers

1
votes

The JSON response is a JSONArray and not a JSONObject. So do this,

JSONArray mainObject = new JSONArray(output.toString());

for (int i = 0; i < mainObject.length(); i++) {
    JSONObject object = mainObject.getJSONObject(i);
    int id = object.getInt("id");  //This will have the value of the id
}
0
votes

The first thing you have to do is getJSONArray because the result you showed has an array as root element, not a JSONObject. Then you need to iterate through the array and get each JSONObject:

JSONArray values= new JSONArray(output.toString());
for (int i = 0; i < values.size(); i++) {

               JSONObject mainObject= values.getJSONObject(i); 
               int id = mainObject.getInt("id");
}