1

JsonParse.Java Trying to get the json array object from URL but it is throwing Parsing ERROR
and CountriedBean is my POJO class.
parse_json just has ListView and parse_json_view has 4 TextView's

Can Anyone please help me out................

private List<CountriesBean> cntries= new ArrayList<CountriesBean>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.parse_json);

            RequestQueue queues = Volley.newRequestQueue(JsonParse.this);

            JsonObjectRequest myRequest = new JsonObjectRequest(Request.Method.GET,
                    "https://github.com/SaiNitesh/REST-Web-services/blob/master/RESTfulWS/json_file.json",
                    null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try{
                                Log.i("myCountries**", "response:" +response);
                            JSONArray countryItems = response.getJSONArray("countryItems");

                            Log.i("myTagx", "response:" +countryItems);

                                for(int i=0; i<countryItems.length();i++){
                                    JSONObject temp= countryItems.getJSONObject(i);

                                    String nm = temp.getString("nm");
                                    String cty = temp.getString("cty");
                                    String hse = temp.getString("hse");
                                    String yrs = temp.getString("yrs");

                                    cntries.add(new CountriesBean(nm, cty, hse, yrs));
                                }
                            } catch(JSONException e){
                                Log.i("myTag",e.toString());
                            }
                        }
                    }, new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Log.i("myTag", "Error:"+error.toString());
                }
            });

            queues.add(myRequest);

            ArrayAdapter<CountriesBean> adapter=new customAdapter();

            ListView myFirstListView = (ListView) (findViewById(R.id.myCountriesView));
            myFirstListView.setAdapter(adapter);


            myFirstListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                //WebView myListViewbrowser = (WebView) findViewById(R.id.myListViewbrowser);
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Toast.makeText(JsonParse.this,"My List View Item",Toast.LENGTH_SHORT).show();
                }
            });


        }

        private class customAdapter extends ArrayAdapter<CountriesBean>{
            public customAdapter() {
                super(JsonParse.this, R.layout.parse_json_view, cntries);

            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                /*getView method is like "for loop"
                For First time convertView=null, then it gets 1st list, then it returns(return convertView) the first list,
                then the convertView will be null
                After that it starts from here again for 2nd list and it continues*/
                if(convertView == null){
                    convertView = getLayoutInflater().inflate(R.layout.parse_json_view, parent,false);

                }


                TextView countryName = (TextView) convertView.findViewById(R.id.name);
                TextView countryCity = (TextView) convertView.findViewById(R.id.city);
                TextView countryHouse = (TextView) convertView.findViewById(R.id.house);

                CountriesBean myCurrentctries = cntries.get(position);


                countryName.setText(myCurrentctries.getNm());
                countryCity.setText(myCurrentctries.getCty());
                countryHouse.setText( myCurrentctries.getHse() );

                return convertView;
            }


        }
1
  • Please post error log also Commented Dec 1, 2016 at 4:02

1 Answer 1

5

The response of the url https://github.com/SaiNitesh/REST-Web-services/blob/master/RESTfulWS/json_file.json is an HTML page, so you get that exception.

Please use https://raw.githubusercontent.com/SaiNitesh/REST-Web-services/master/RESTfulWS/json_file.json instead.

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

2 Comments

How about if i want to upload the image like JSON file ? How can i show it as raw data ?
I think you should create a new question for your new requirement, or you can search fist in S.O, many links available (one of them is stackoverflow.com/questions/16797468/…), but you also need to clarify, download or upload

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.