2

Im trying to get data from json. Actually it is working but i have a problem with listview.Im getting error.

Here is the error image

Here is the code; MainActivity.java

  

public class MainActivity extends AppCompatActivity {
    TextView txt;
    Places places=new Places();
    ListView listView;
    List<Places> PlacesList=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt= (TextView) findViewById(R.id.text);
        listView= (ListView) findViewById(R.id.listView);
        listView.setBackgroundColor(Color.BLACK);
        new getData().execute("https://api.foursquare.com/v2/venues/search?ll=41,29&oauth_token=01ZV1VDLE41USSRAYJUXOWNJ2MXYCAVN1I2PQFLFBCD40XYI&v=20160304");

    }
    public class getData extends AsyncTask<String,String,List <Places>> {
        HttpURLConnection conn=null;
        URL url=null;
        BufferedReader bReader=null;
        String name=null;
        @Override
        protected List<Places> doInBackground(String... params) {
            try {
                //BURADA URL'E BAGLANDIK
                url=new URL(params[0]);
                conn= (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                conn.connect();
                //VERİ OKUMAK İÇİN STREAM KULLANACAZ
                InputStream istream=conn.getInputStream();
                bReader= new BufferedReader(new InputStreamReader(istream));
                //VERİ DEPOLAMAK İÇİN StringBuffer
                StringBuffer sBuffer=new StringBuffer();
                String line="";
                while((line=bReader.readLine())!=null){
                    sBuffer.append(line);
                }
                String JSONFinal=sBuffer.toString();
                JSONObject jsonObject=new JSONObject(JSONFinal);
                JSONArray jsonArray=jsonObject.getJSONObject("response").getJSONArray("venues");
                StringBuffer finalBuffer=new StringBuffer();
                for(int i=0;i<jsonArray.length();i++) {
                    JSONObject finalJsonObject = jsonArray.getJSONObject(i);
                    name = finalJsonObject.getString("name");
                    places.setName(name);
                    PlacesList.add(places);
                }

                return  PlacesList;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            finally {
                conn.disconnect();
            }
            return null;
        }

        @Override
        protected void onPostExecute( List<Places> PlacesList) {
            super.onPostExecute(PlacesList);
            ArrayAdapter adapter=  new ArrayAdapter<Places>(getApplicationContext(),android.R.layout.simple_list_item_1,PlacesList);
            listView.setAdapter(adapter);

        }
    }

Places.java

public class Places {
    int id,lat,lng;
    String name;



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

So what is the problem?

5
  • post your json also. Commented Mar 5, 2016 at 17:52
  • here is my json jsoneditoronline.org/?url=https://api.foursquare.com/v2/venues/… Commented Mar 5, 2016 at 17:53
  • i dont be able to see any string called name in your json. Commented Mar 5, 2016 at 17:58
  • @Azat Check my answer. Commented Mar 5, 2016 at 18:00
  • go to response>venues>0>name Commented Mar 5, 2016 at 18:01

2 Answers 2

1

It displays like that because you have passed Places object to your list view, and you haven't provided which fields to show in your list view from Places object , you should probably provide toString() method in your Places object to see your list with name or you should create your own adapter implementation

To see your listview with names

add this in your Places class

public void toString(){
return name;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Azat You can mark this as answer by clicking the right mark beside the answer if it helps you, so that it will help for others facing the same issue
Its done. I have one more question. How am i gonna show all the places. It this way all the names are same
@Azat I see you marking somebody's answer as correct answer. You should probably create your custom Adapter
0

Looks to me like you haven't overridden the toString() method of your Places class.

Add this to your Places class:

@Override
public String toString(){

 return name;
}

See if that helps. The Adapter is calling the toString() method of Places Object superclass, which is returning a hash of the Places object. That's what's being printed.

6 Comments

I have another question. It shows same name in each item of listview. So what do i need to do?
You only have one places object, it looks to me like with each iteration of your for loop you're changing the name of that single object, then adding a reference to the same object to the list.
in the for loop at the end, try Places place = new Places(); place.setName("new name"); then add that to the arraylist.
Also, you Places class is named a plural, but only handles a single place. You should remove the s from the end of the name.
Thank you so much it woks. I have some problems with english :)
|

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.